deno.land / x / pothos@release-1713397530 / packages / plugin-relay / schema-builder.ts

schema-builder.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// @ts-nocheckimport { defaultTypeResolver, GraphQLResolveInfo } from 'https://cdn.skypack.dev/graphql?dts';import SchemaBuilder, { completeValue, createContextCache, FieldMap, FieldRef, getTypeBrand, InputObjectRef, InterfaceParam, InterfaceRef, MaybePromise, ObjectFieldsShape, ObjectParam, ObjectRef, OutputRef, PothosValidationError, SchemaTypes, verifyRef, } from '../core/index.ts';import { NodeRef } from './node-ref.ts';import { ConnectionShape, GlobalIDShape, PageInfoShape } from './types.ts';import { capitalize, resolveNodes } from './utils/index.ts';const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>;const pageInfoRefMap = new WeakMap<PothosSchemaTypes.SchemaBuilder<SchemaTypes>, ObjectRef<PageInfoShape>>();const nodeInterfaceRefMap = new WeakMap<PothosSchemaTypes.SchemaBuilder<SchemaTypes>, InterfaceRef<{}>>();export const connectionRefs = new WeakMap<PothosSchemaTypes.SchemaBuilder<SchemaTypes>, ObjectRef<ConnectionShape<SchemaTypes, unknown, boolean>>[]>();export const globalConnectionFieldsMap = new WeakMap<PothosSchemaTypes.SchemaBuilder<SchemaTypes>, ((ref: ObjectRef<ConnectionShape<SchemaTypes, unknown, boolean>>) => void)[]>();schemaBuilderProto.pageInfoRef = function pageInfoRef() { if (pageInfoRefMap.has(this)) { return pageInfoRefMap.get(this)!; } const ref = this.objectRef<PageInfoShape>("PageInfo"); pageInfoRefMap.set(this, ref); const { cursorType = "String", hasNextPageFieldOptions = {} as never, hasPreviousPageFieldOptions = {} as never, startCursorFieldOptions = {} as never, endCursorFieldOptions = {} as never, } = this.options.relayOptions; ref.implement({ ...this.options.relayOptions.pageInfoTypeOptions, fields: (t) => ({ hasNextPage: t.exposeBoolean("hasNextPage", { nullable: false, ...hasNextPageFieldOptions, }), hasPreviousPage: t.exposeBoolean("hasPreviousPage", { nullable: false, ...hasPreviousPageFieldOptions, }), startCursor: t.expose("startCursor", { nullable: true, ...(startCursorFieldOptions as {}), type: cursorType, }) as never, endCursor: t.expose("endCursor", { nullable: true, ...(endCursorFieldOptions as {}), type: cursorType, }) as never, }), }); return ref;};schemaBuilderProto.nodeInterfaceRef = function nodeInterfaceRef() { if (nodeInterfaceRefMap.has(this)) { return nodeInterfaceRefMap.get(this)!; } const ref = this.interfaceRef<{}>("Node"); nodeInterfaceRefMap.set(this, ref); ref.implement({ resolveType: (value, context, info, graphQLType) => { if (!value) { return defaultTypeResolver(value, context, info, graphQLType); } const typeBrand = getTypeBrand(value); if (typeBrand) { const type = this.configStore.getTypeConfig(typeBrand as string); return type.name; } try { if (typeof value === "object") { // eslint-disable-next-line no-underscore-dangle const typename = (value as { __typename: string; }).__typename; if (typename) { return typename; } // eslint-disable-next-line no-underscore-dangle const nodeRef = (value as { __type: OutputRef; }).__type; if (nodeRef) { const config = this.configStore.getTypeConfig(nodeRef); if (config) { return config.name; } } } } catch { // ignore } return defaultTypeResolver(value, context, info, graphQLType); }, ...this.options.relayOptions.nodeTypeOptions, fields: (t) => ({ [this.options.relayOptions?.idFieldName ?? "id"]: t.globalID({ ...this.options.relayOptions?.idFieldOptions, nullable: false, resolve: (parent) => { throw new PothosValidationError("id field not implemented"); }, }), }), }); const nodeQueryOptions = this.options.relayOptions?.nodeQueryOptions; if (nodeQueryOptions !== false) { const resolveNodeFn = nodeQueryOptions?.resolve; this.queryField("node", (t) => t.field({ nullable: true, ...this.options.relayOptions.nodeQueryOptions, type: ref as InterfaceRef<unknown>, args: { id: t.arg.globalID({ ...nodeQueryOptions?.args?.id, required: true, extensions: { relayGlobalIDAlwaysParse: true, ...nodeQueryOptions?.args?.id?.extensions, }, }), }, resolve: resolveNodeFn ? (root, args, context, info) => resolveNodeFn(root, args, context, info, (ids) => completeValue(resolveNodes(this, context, info, [args.id]), (nodes) => nodes[0])) as never : (root, args, context, info) => completeValue(resolveNodes(this, context, info, [args.id]), (nodes) => nodes[0]), }) as FieldRef<unknown>); } const nodesQueryOptions = this.options.relayOptions?.nodesQueryOptions; if (nodesQueryOptions !== false) { const resolveNodesFn = nodesQueryOptions?.resolve; this.queryField("nodes", (t) => t.field({ nullable: { list: false, items: true, }, ...this.options.relayOptions.nodesQueryOptions, type: [ref], args: { ids: t.arg.globalIDList({ ...nodesQueryOptions?.args?.ids, required: true, extensions: { relayGlobalIDAlwaysParse: true, ...nodesQueryOptions?.args?.ids?.extensions, }, }), }, resolve: resolveNodesFn ? (root, args, context, info) => resolveNodesFn(root, args as { ids: { id: string; typename: string; }[]; }, context, info, (ids) => resolveNodes(this, context, info, args.ids as { id: string; typename: string; }[])) as never : (root, args, context, info) => resolveNodes(this, context, info, args.ids as { id: string; typename: string; }[]) as never, })); } return ref;};schemaBuilderProto.node = function node(param, { interfaces, extensions, id, ...options }, fields) { verifyRef(param); const interfacesWithNode: () => InterfaceParam<SchemaTypes>[] = () => [ this.nodeInterfaceRef(), ...(typeof interfaces === "function" ? interfaces() : interfaces ?? []), ]; let nodeName!: string; const ref = this.objectType<[ ], ObjectParam<SchemaTypes>>(param, { ...(options as {}), extensions: { ...extensions, pothosParseGlobalID: id.parse, }, isTypeOf: options.isTypeOf ?? (typeof param === "function" ? (maybeNode: unknown, context: object, info: GraphQLResolveInfo) => { if (!maybeNode) { return false; } if (maybeNode instanceof (param as Function)) { return true; } const proto = Object.getPrototypeOf(maybeNode) as { constructor: unknown; }; try { if (proto?.constructor) { const config = this.configStore.getTypeConfig(proto.constructor as OutputRef); return config.name === nodeName; } } catch { // ignore } return false; } : undefined), interfaces: interfacesWithNode as () => [ ], }, fields); this.configStore.onTypeConfig(ref, (nodeConfig) => { nodeName = nodeConfig.name; this.objectField(ref, this.options.relayOptions.idFieldName ?? "id", (t) => t.globalID<{}, false, MaybePromise<GlobalIDShape<SchemaTypes>>>({ nullable: false, ...this.options.relayOptions.idFieldOptions, ...id, args: {}, resolve: (parent, args, context, info): MaybePromise<GlobalIDShape<SchemaTypes>> => // eslint-disable-next-line @typescript-eslint/no-unsafe-argument completeValue(id.resolve(parent, args, context, info), (globalId) => ({ type: nodeConfig.name, id: globalId, })), })); }); const nodeRef = new NodeRef(ref.name, { parseId: id.parse, }); this.configStore.associateRefWithName(nodeRef, ref.name); return nodeRef as never;};schemaBuilderProto.globalConnectionField = function globalConnectionField(name, field) { this.globalConnectionFields((t) => ({ [name]: field(t) }));};schemaBuilderProto.globalConnectionFields = function globalConnectionFields(fields) { const onRef = (ref: ObjectRef<ConnectionShape<SchemaTypes, unknown, boolean>>) => { this.configStore.onPrepare(() => { const config = this.configStore.getTypeConfig(ref); this.objectFields(ref, (t) => { const existingFields = this.configStore.getFields(config.name); const refs: FieldMap = {}; for (const [name, field] of Object.entries(fields(t as never))) { if (!existingFields.has(name)) { refs[name] = field; } } return refs; }); }); }; connectionRefs.get(this)?.forEach((ref) => void onRef(ref)); if (!globalConnectionFieldsMap.has(this)) { globalConnectionFieldsMap.set(this, []); } globalConnectionFieldsMap.get(this)!.push(onRef);};const mutationIdCache = createContextCache(() => new Map<string, string>());schemaBuilderProto.relayMutationField = function relayMutationField(fieldName, inputOptionsOrRef, { resolve, args, ...fieldOptions }, { name: payloadName = `${capitalize(fieldName)}Payload`, outputFields, interfaces, ...payloadOptions }) { const { relayOptions: { clientMutationIdInputOptions = {} as never, clientMutationIdFieldOptions = {} as never, mutationInputArgOptions = {} as never, }, } = this.options; const includeClientMutationId = this.options.relayOptions.clientMutationId !== "omit"; let inputRef: InputObjectRef<unknown> | null; let argName = "input"; if (!inputOptionsOrRef || inputOptionsOrRef instanceof InputObjectRef) { inputRef = inputOptionsOrRef; } else { const { name: inputName = `${capitalize(fieldName)}Input`, argName: argNameFromOptions = "input", inputFields, ...inputOptions } = inputOptionsOrRef; argName = argNameFromOptions; inputRef = this.inputType(inputName, { ...this.options.relayOptions?.defaultMutationInputTypeOptions, ...inputOptions, fields: (t) => ({ ...inputFields(t), ...(includeClientMutationId ? { clientMutationId: t.id({ ...clientMutationIdInputOptions, required: this.options.relayOptions.clientMutationId !== "optional", }), } : {}), }), }); } const payloadRef = this.objectRef<unknown>(payloadName).implement({ ...this.options.relayOptions?.defaultPayloadTypeOptions, ...payloadOptions, interfaces: interfaces as never, fields: (t) => ({ ...(outputFields as ObjectFieldsShape<SchemaTypes, unknown>)(t), ...(includeClientMutationId ? { clientMutationId: t.id({ nullable: this.options.relayOptions.clientMutationId === "optional", ...clientMutationIdFieldOptions, resolve: (parent, _args, context, info) => mutationIdCache(context).get(String(info.path.prev!.key))!, }), } : {}), }), }); this.mutationField(fieldName, (t) => t.field({ ...(fieldOptions as {}), type: payloadRef, args: { ...args, ...(inputRef ? { [argName]: t.arg({ ...(mutationInputArgOptions as {}), type: inputRef, required: true, }), } : {}), }, resolve: (root, fieldArgs, context, info) => { if (inputRef) { mutationIdCache(context).set(String(info.path.key), (fieldArgs as unknown as Record<string, { clientMutationId: string; }>)[argName] .clientMutationId); } return resolve(root, fieldArgs as never, context, info); }, })); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return { inputType: inputRef, payloadType: payloadRef, } as never;};schemaBuilderProto.connectionObject = function connectionObject({ type, name: connectionName, edgesNullable: edgesNullableField, nodeNullable, edgesField, ...connectionOptions }, edgeOptionsOrRef) { verifyRef(type); const { edgesFieldOptions: { nullable: edgesNullable = { items: true, list: false }, ...edgesFieldOptions } = {} as never, pageInfoFieldOptions = {} as never, } = this.options.relayOptions; const connectionRef = this.objectRef<ConnectionShape<SchemaTypes, unknown, false>>(connectionName); const edgeRef = edgeOptionsOrRef instanceof ObjectRef ? edgeOptionsOrRef : this.edgeObject({ name: `${connectionName.replace(/Connection$/, "")}Edge`, ...edgeOptionsOrRef, nodeNullable, type, }); const connectionFields = connectionOptions.fields as unknown as ObjectFieldsShape<SchemaTypes, ConnectionShape<SchemaTypes, unknown, false>> | undefined; const { nodesOnConnection } = this.options.relayOptions; const edgesNullableOption = edgesNullableField ?? edgesNullable; const edgeListNullable = typeof edgesNullableOption === "object" ? edgesNullableOption.list : !!edgesNullableOption; const edgeItemsNullable = typeof edgesNullableOption === "object" && "items" in (edgesNullableOption as {}) ? edgesNullableOption.items : false; this.objectType(connectionRef, { ...(this.options.relayOptions?.defaultConnectionTypeOptions as {}), ...(connectionOptions as {}), fields: (t) => ({ pageInfo: t.field({ nullable: false, ...pageInfoFieldOptions, type: this.pageInfoRef(), resolve: (parent) => parent.pageInfo, }), edges: t.field({ nullable: (edgesNullableField ?? edgesNullable) as { list: false; items: true; }, ...edgesFieldOptions, ...edgesField, type: [edgeRef], resolve: (parent) => parent.edges as [ ], }), ...(nodesOnConnection ? { nodes: t.field({ ...(typeof nodesOnConnection === "object" ? nodesOnConnection : {}), type: [type], nullable: { list: edgeListNullable, items: edgeItemsNullable ?? nodeNullable ?? this.options.relayOptions?.nodeFieldOptions?.nullable ?? false, }, resolve: (con) => completeValue(con.edges, (edges) => edges?.map((e) => e?.node) ?? (edgeListNullable ? null : [])) as never, }), } : {}), ...connectionFields?.(t as never), }), }); if (!connectionRefs.has(this)) { connectionRefs.set(this, []); } connectionRefs.get(this)!.push(connectionRef); globalConnectionFieldsMap.get(this)?.forEach((fieldFn) => void fieldFn(connectionRef)); return connectionRef as never;};schemaBuilderProto.edgeObject = function edgeObject({ type, name: edgeName, nodeNullable: nodeFieldNullable, nodeField, ...edgeOptions }) { verifyRef(type); const { cursorType = "String", cursorFieldOptions = {} as never, nodeFieldOptions: { nullable: nodeNullable = false, ...nodeFieldOptions } = {} as never, } = this.options.relayOptions; const edgeRef = this.objectRef<{ cursor: string; node: unknown; }>(edgeName); const edgeFields = edgeOptions.fields as ObjectFieldsShape<SchemaTypes, { cursor: string; node: unknown; }> | undefined; this.objectType(edgeRef, { ...(this.options.relayOptions?.defaultEdgeTypeOptions as {}), ...edgeOptions, fields: (t) => ({ node: t.field({ nullable: nodeFieldNullable ?? nodeNullable, ...nodeFieldOptions, ...nodeField, type, resolve: (parent) => parent.node as never, }), cursor: t.expose("cursor", { nullable: false, type: cursorType, ...cursorFieldOptions, }) as never, ...edgeFields?.(t), }), }); return edgeRef as never;};
pothos

Version Info

Tagged at
a year ago