deno.land / x / pothos@release-1713397530 / packages / plugin-relay / utils / resolve-nodes.ts

resolve-nodes.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// @ts-nocheckimport { GraphQLResolveInfo } from 'https://cdn.skypack.dev/graphql?dts';import { brandWithType, createContextCache, MaybePromise, ObjectParam, OutputType, PothosValidationError, SchemaTypes, } from '../../core/index.ts';import { NodeObjectOptions } from '../types.ts';const getRequestCache = createContextCache(() => new Map<string, MaybePromise<unknown>>());export async function resolveNodes<Types extends SchemaTypes>(builder: PothosSchemaTypes.SchemaBuilder<Types>, context: object, info: GraphQLResolveInfo, globalIDs: ({ id: unknown; typename: string;} | null | undefined)[]): Promise<MaybePromise<unknown>[]> { const requestCache = getRequestCache(context); const idsByType: Record<string, Set<unknown>> = {}; const results: Record<string, MaybePromise<unknown>> = {}; globalIDs.forEach((globalID, i) => { if (globalID == null) { return; } const { id, typename } = globalID; const cacheKey = `${typename}:${id}`; if (requestCache.has(cacheKey)) { results[cacheKey] = requestCache.get(cacheKey)!; return; } // eslint-disable-next-line logical-assignment-operators idsByType[typename] = idsByType[typename] ?? new Set(); idsByType[typename].add(id); }); await Promise.all(Object.keys(idsByType).map(async (typename) => { const ids = [...idsByType[typename]]; const config = builder.configStore.getTypeConfig(typename, "Object"); const options = config.pothosOptions as NodeObjectOptions<Types, ObjectParam<Types>, [ ]>; const shouldBrandObjects = options.brandLoadedObjects ?? builder.options.relayOptions.brandLoadedObjects ?? false; const resultsForType = await resolveUncachedNodesForType(builder, context, info, ids, typename); resultsForType.forEach((val, i) => { if (shouldBrandObjects) { brandWithType(val, typename as OutputType<Types>); } results[`${typename}:${ids[i]}`] = val; }); })); return globalIDs.map((globalID) => globalID == null ? null : results[`${globalID.typename}:${globalID.id}`] ?? null);}export async function resolveUncachedNodesForType<Types extends SchemaTypes>(builder: PothosSchemaTypes.SchemaBuilder<Types>, context: object, info: GraphQLResolveInfo, ids: readonly unknown[], type: OutputType<Types> | string): Promise<unknown[]> { const requestCache = getRequestCache(context); const config = builder.configStore.getTypeConfig(type, "Object"); const options = config.pothosOptions as NodeObjectOptions<Types, ObjectParam<Types>, [ ], unknown>; if (options.loadMany) { const loadManyPromise = Promise.resolve(options.loadMany(ids as unknown[], context)); return Promise.all(ids.map((id, i) => { const entryPromise = loadManyPromise .then((results: readonly unknown[]) => results[i]) .then((result: unknown) => { requestCache.set(`${config.name}:${id}`, result); return result; }); requestCache.set(`${config.name}:${id}`, entryPromise); return entryPromise; })); } if (options.loadOne) { return Promise.all(ids.map((id) => { const entryPromise = Promise.resolve(options.loadOne!(id, context)).then((result: unknown) => { requestCache.set(`${config.name}:${id}`, result); return result; }); requestCache.set(`${config.name}:${id}`, entryPromise); return entryPromise; })); } if (options.loadManyWithoutCache) { return options.loadManyWithoutCache(ids as unknown[], context) as unknown[]; } if (options.loadWithoutCache) { return Promise.all(ids.map((id) => Promise.resolve(options.loadWithoutCache!(id, context, info)))); } throw new PothosValidationError(`${config.name} does not support loading by id`);}
pothos

Version Info

Tagged at
a year ago