| 634 | * grouped by, so the transport doesn't resolve it again. |
| 635 | */ |
| 636 | const makeResolver = <A extends KeyRequest<any, any, any, any>>( |
| 637 | method: OperationMethod<any>, |
| 638 | batchKey: ((context: Context.Context<any>) => unknown) | undefined, |
| 639 | runAll: (entries: Arr.NonEmptyArray<Request.Entry<A>>) => Effect.Effect<void, Request.Error<A>, any> |
| 640 | ): RequestResolver.RequestResolver<A> => { |
| 641 | const resolve = internalGraphQL.rawExecutors.get(method)?.url |
| 642 | const resolveUrl = resolve?.({ kind: method.operation.kind, operationName: method.operation.name }) |
| 643 | // Arrays are compared structurally, so equal keys share a batch. |
| 644 | const keyOf = (entry: Request.Entry<A>, url: string | undefined) => [url, batchKey?.(entry.context)] as const |
| 645 | const urlOf = (entry: Request.Entry<A>) => |
| 646 | entry.request.url !== undefined || resolveUrl === undefined |
| 647 | ? Effect.succeed(entry.request.url) |
| 648 | : runIn(resolveUrl, entry.context) |
| 649 | const resolver = RequestResolver.makeGrouped<A, unknown>({ |
| 650 | key: (entry) => keyOf(entry, entry.request.url), |
| 651 | resolver: (entries) => |
| 652 | Effect.contextWith((current: Context.Context<never>) => { |
| 653 | const span = batchSpan(current) |
| 654 | return Effect.flatMap(Effect.forEach(entries, urlOf, { concurrency: "unbounded" }), (urls) => { |
| 655 | const batches = MutableHashMap.empty< |
| 656 | readonly [string | undefined, unknown], |
| 657 | Arr.NonEmptyArray<Request.Entry<A>> |
| 658 | >() |
| 659 | entries.forEach((entry, i) => { |
| 660 | const key = keyOf(entry, urls[i]) |
| 661 | const batch = MutableHashMap.get(batches, key) |
| 662 | if (batch._tag === "Some") batch.value.push(entry) |
| 663 | else MutableHashMap.set(batches, key, [entry]) |
| 664 | }) |
| 665 | return Effect.forEach(batches, ([[url], entries]) => { |
| 666 | let context: Context.Context<any> = entries[0].context |
| 667 | if (url !== undefined) context = Context.add(context, internalGraphQL.ResolvedUrl, { resolve, url }) |
| 668 | if (span !== undefined) context = Context.add(context, Tracer.ParentSpan, span) |
| 669 | return runIn(runAll(entries), context) |
| 670 | }, { concurrency: "unbounded", discard: true }) |
| 671 | }) |
| 672 | }) |
| 673 | }) |
| 674 | if (resolveUrl !== undefined) urlResolvers.set(resolver.batchKey, resolveUrl) |
| 675 | return resolver |
| 676 | } |
| 677 | |
| 678 | // Replaces, rather than merges into, the batch fiber's context, which may |
| 679 | // belong to a lookup with other services or `Context.Reference` values. |