MCPcopy Create free account
hub / github.com/Effect-TS/effect / findById

Function findById

packages/effect/src/graphql/GraphQLResolver.ts:372–418  ·  view source on GitHub ↗
(
  method: OperationMethod<Op>,
  options: {
    readonly variables: (keys: ReadonlyArray<K>) => GraphQL.Variables<Op>
    readonly path: Path<GraphQL.Result<Op>, P>
    readonly key: (item: Narrow<Item<GraphQL.Result<Op>, P>, T>) => K
    readonly typename?: T | undefined
  } & BatchKey<Op>
)

Source from the content-addressed store, hash-verified

370 * @since 4.0.0
371 */
372export const findById = <
373 Op extends GraphQL.Any,
374 K,
375 const P extends ReadonlyArray<string>,
376 T extends Typenames<Item<GraphQL.Result<Op>, P>> = never
377>(
378 method: OperationMethod<Op>,
379 options: {
380 readonly variables: (keys: ReadonlyArray<K>) => GraphQL.Variables<Op>
381 readonly path: Path<GraphQL.Result<Op>, P>
382 readonly key: (item: Narrow<Item<GraphQL.Result<Op>, P>, T>) => K
383 readonly typename?: T | undefined
384 } & BatchKey<Op>
385): RequestResolver.RequestResolver<
386 KeyRequest<
387 K,
388 Narrow<Item<GraphQL.Result<Op>, P>, T>,
389 GraphQL.Error<Op> | Cause.NoSuchElementError,
390 GraphQL.Services<Op>
391 >
392> => {
393 const path = options.path as ReadonlyArray<string>
394 const codec = internalGraphQL.makeCodec(method.operation)
395 return makeResolver(
396 method,
397 options.batchKey,
398 (entries) =>
399 Effect.flatMap(callBatch(method, entries, options.variables), ({ keys, targets, result }) => {
400 if (result.errors.length > 0) return batchError(codec, result)
401 const list = findList(result.data, path)
402 if (list === undefined) return completeAll(targets)
403 if (list.truncated) return Effect.fail(codec.fail(truncated(path)))
404 const pending = MutableHashMap.empty<K, Request.Entry<any>>()
405 for (let i = 0; i < keys.length; i++) MutableHashMap.set(pending, keys[i], targets[i])
406 for (const item of list.items) {
407 if (!matchesTypename(item, options.typename)) continue
408 const key = options.key(item as Narrow<Item<GraphQL.Result<Op>, P>, T>)
409 const target = MutableHashMap.get(pending, key)
410 if (target._tag === "None") continue
411 MutableHashMap.remove(pending, key)
412 target.value.completeUnsafe(Exit.succeed(item))
413 }
414 MutableHashMap.forEach(pending, (target) => target.completeUnsafe(constNoSuchElement))
415 return Effect.void
416 })
417 )
418}
419
420/**
421 * Batches calls to a generated query or mutation by repeating the operation

Callers

nothing calls this directly

Calls 15

callBatchFunction · 0.85
batchErrorFunction · 0.85
findListFunction · 0.85
completeAllFunction · 0.85
matchesTypenameFunction · 0.85
completeUnsafeMethod · 0.80
makeResolverFunction · 0.70
truncatedFunction · 0.70
failMethod · 0.65
setMethod · 0.65
keyMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected