deno.land / x / pothos@release-1713397530 / packages / core / config-store.ts

config-store.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
// @ts-nocheck/* eslint-disable @typescript-eslint/no-redundant-type-constituents *//* eslint-disable node/no-callback-literal */import { GraphQLBoolean, GraphQLFloat, GraphQLID, GraphQLInt, GraphQLScalarType, GraphQLString, } from 'https://cdn.skypack.dev/graphql?dts';import { PothosError, PothosSchemaError } from './errors.ts';import BaseTypeRef from './refs/base.ts';import BuiltinScalarRef from './refs/builtin-scalar.ts';import FieldRef from './refs/field.ts';import InputTypeRef from './refs/input.ts';import InputFieldRef from './refs/input-field.ts';import InputListRef from './refs/input-list.ts';import ListRef from './refs/list.ts';import OutputTypeRef from './refs/output.ts';import type { ConfigurableRef, FieldMap, GraphQLFieldKind, InputFieldMap, InputRef, InputType, InputTypeParam, InterfaceParam, ObjectParam, OutputRef, OutputType, PothosFieldConfig, PothosObjectTypeConfig, PothosTypeConfig, SchemaTypes, TypeParam, } from './types/index.ts';import { unwrapListParam } from './utils/index.ts';export default class ConfigStore<Types extends SchemaTypes> { typeConfigs = new Map<string, PothosTypeConfig>(); private fieldRefs = new WeakMap<FieldRef | InputFieldRef, (name: string, parentField: string | undefined, typeConfig: PothosTypeConfig) => PothosFieldConfig<Types>>(); private fields = new Map<string, Map<string, PothosFieldConfig<Types>>>(); private pendingActions: (() => void)[] = []; private refsToName = new Map<ConfigurableRef<Types>, string>(); private scalarsToRefs = new Map<string, BuiltinScalarRef<unknown, unknown>>(); private fieldRefsToConfigs = new Map<FieldRef | InputFieldRef, PothosFieldConfig<Types>[]>(); private pendingFields = new Map<FieldRef | InputFieldRef, InputType<Types> | OutputType<Types>>(); private pendingRefResolutions = new Map<ConfigurableRef<Types>, ((config: PothosTypeConfig) => void)[]>(); private fieldRefCallbacks = new Map<FieldRef | InputFieldRef, ((config: PothosFieldConfig<Types>) => void)[]>(); private pending = true; constructor() { const scalars: GraphQLScalarType[] = [ GraphQLID, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean, ]; scalars.forEach((scalar) => { const ref = new BuiltinScalarRef(scalar); this.scalarsToRefs.set(scalar.name, ref); this.refsToName.set(ref, scalar.name); }); } hasConfig(typeParam: InputType<Types> | OutputType<Types>) { if (typeof typeParam === "string") { return this.typeConfigs.has(typeParam); } return this.refsToName.has(typeParam); } addUnionTypes(typeName: string, unionTypes: ObjectParam<Types>[] | (() => ObjectParam<Types>[])) { this.onPrepare(() => { const typeConfig = this.getTypeConfig(typeName); if (typeConfig.graphqlKind !== "Union") { throw new PothosSchemaError(`Can not add types to ${typeName} because it is a ${typeConfig.kind}`); } typeConfig.types = [ ...typeConfig.types, ...((typeof unionTypes === "function" ? unionTypes() : unionTypes) as ObjectParam<SchemaTypes>[]), ]; }); } addInterfaces(typeName: string, interfaces: InterfaceParam<Types>[] | (() => InterfaceParam<Types>[])) { this.onPrepare(() => { const typeConfig = this.getTypeConfig(typeName); if ((typeConfig.graphqlKind !== "Object" && typeConfig.graphqlKind !== "Interface") || typeConfig.kind === "Query" || typeConfig.kind === "Mutation" || typeConfig.kind === "Subscription") { throw new PothosSchemaError(`Can not add interfaces to ${typeName} because it is a ${typeConfig.kind}`); } typeConfig.interfaces = [ ...typeConfig.interfaces, ...((typeof interfaces === "function" ? interfaces() : interfaces) as InterfaceParam<SchemaTypes>[]), ]; }); } addFieldRef(ref: FieldRef | InputFieldRef, // We need to be able to resolve the types kind before configuring the field typeParam: InputTypeParam<Types> | TypeParam<Types>, args: InputFieldMap, getConfig: (name: string, parentField: string | undefined, typeConfig: PothosTypeConfig) => PothosFieldConfig<Types>) { if (this.fieldRefs.has(ref)) { throw new PothosSchemaError(`FieldRef ${String(ref)} has already been added to config store`); } const typeRefOrName = unwrapListParam(typeParam); const argRefs = Object.keys(args).map((argName) => { const argRef = args[argName]; argRef.fieldName = argName; argRef.argFor = ref; return argRef; }); const checkArgs = () => { for (const arg of argRefs) { if (this.pendingFields.has(arg)) { const unresolvedArgType = this.pendingFields.get(arg)!; this.pendingFields.set(ref, unresolvedArgType); this.onTypeConfig(unresolvedArgType, checkArgs); return; } } this.pendingFields.delete(ref); this.fieldRefs.set(ref, getConfig); }; if (this.hasConfig(typeRefOrName) || typeRefOrName instanceof BaseTypeRef || this.scalarsToRefs.has(typeRefOrName as string)) { checkArgs(); } else { this.pendingFields.set(ref, typeRefOrName); this.onTypeConfig(typeRefOrName, () => { checkArgs(); }); } } createFieldConfig<T extends GraphQLFieldKind>(ref: FieldRef | InputFieldRef, name: string, typeConfig: PothosTypeConfig, parentField?: string, kind?: T): Extract<PothosFieldConfig<Types>, { graphqlKind: T; }> { if (!this.fieldRefs.has(ref)) { if (this.pendingFields.has(ref)) { throw new PothosSchemaError(`Missing implementation for ${this.describeRef(this.pendingFields.get(ref)!)} used in field ${name} of ${typeConfig.name}`); } throw new PothosSchemaError(`Missing definition for ${String(ref)}`); } const config = this.fieldRefs.get(ref)!(name, parentField, typeConfig); if (kind && config.graphqlKind !== kind) { throw new PothosError(`Expected ref for field named ${name} to resolve to a ${kind} type, but got ${config.graphqlKind}`); } return config as Extract<PothosFieldConfig<Types>, { graphqlKind: T; }>; } associateRefWithName(ref: ConfigurableRef<Types>, name: string) { if (!this.typeConfigs.has(name)) { throw new PothosSchemaError(`${name} has not been implemented yet`); } this.refsToName.set(ref, name); if (this.pendingRefResolutions.has(ref)) { const cbs = this.pendingRefResolutions.get(ref)!; this.pendingRefResolutions.delete(ref); cbs.forEach((cb) => void cb(this.typeConfigs.get(name)!)); } } addTypeConfig(config: PothosTypeConfig, ref?: ConfigurableRef<Types>) { const { name } = config; if (this.typeConfigs.has(name)) { throw new PothosSchemaError(`Duplicate typename: Another type with name ${name} already exists.`); } this.typeConfigs.set(config.name, config); if (ref) { this.associateRefWithName(ref, name); } if (this.pendingRefResolutions.has(name as ConfigurableRef<Types>)) { const cbs = this.pendingRefResolutions.get(name as ConfigurableRef<Types>)!; this.pendingRefResolutions.delete(name as ConfigurableRef<Types>); cbs.forEach((cb) => void cb(config)); } } getTypeConfig<T extends PothosTypeConfig["kind"]>(ref: ConfigurableRef<Types> | string, kind?: T) { let config: PothosTypeConfig; if (typeof ref === "string") { if (!this.typeConfigs.has(ref)) { throw new PothosSchemaError(`Type ${String(ref)} has not been implemented`); } config = this.typeConfigs.get(ref)!; } else if (this.refsToName.has(ref)) { config = this.typeConfigs.get(this.refsToName.get(ref)!)!; } else if (ref instanceof ListRef || ref instanceof InputListRef) { throw new PothosSchemaError(`Expected a base type but got a ${ref.kind} of ${String(ref.listType)}`); } else { throw new PothosSchemaError(`Ref ${String(ref)} has not been implemented`); } if (kind && config.graphqlKind !== kind) { throw new PothosSchemaError(`Expected ref to resolve to a ${kind} type, but got ${config.kind}`); } return config as Extract<PothosTypeConfig, { kind: T; }>; } getInputTypeRef(ref: ConfigurableRef<Types> | string) { if (ref instanceof BaseTypeRef) { if (ref.kind !== "InputObject" && ref.kind !== "Enum" && ref.kind !== "Scalar") { throw new PothosSchemaError(`Expected ${ref.name} to be an input type but got ${ref.kind}`); } return ref as InputRef; } if (typeof ref === "string") { if (this.scalarsToRefs.has(ref)) { return this.scalarsToRefs.get(ref)!; } if (this.typeConfigs.has(ref)) { const config = this.typeConfigs.get(ref)!; if (config.graphqlKind !== "InputObject" && config.graphqlKind !== "Enum" && config.graphqlKind !== "Scalar") { throw new PothosSchemaError(`Expected ${config.name} to be an input type but got ${config.graphqlKind}`); } const newRef = new InputTypeRef(config.graphqlKind, config.name); this.refsToName.set(newRef, config.name); return newRef; } } return ref as InputType<Types>; } getOutputTypeRef(ref: ConfigurableRef<Types> | string) { if (ref instanceof BaseTypeRef) { if (ref.kind === "InputObject" || ref.kind === "InputList") { throw new PothosSchemaError(`Expected ${ref.name} to be an output type but got ${ref.kind}`); } if (ref.kind === "List") { throw new PothosSchemaError(`Expected ${ref.name} to be a base type but got a ${ref.kind}`); } return ref as OutputRef; } if (typeof ref === "string") { if (this.scalarsToRefs.has(ref)) { return this.scalarsToRefs.get(ref)!; } if (this.typeConfigs.has(ref)) { const config = this.typeConfigs.get(ref)!; if (config.graphqlKind === "InputObject") { throw new PothosSchemaError(`Expected ${config.name} to be an output type but got ${config.graphqlKind}`); } const newRef = new OutputTypeRef(config.graphqlKind, config.name); this.refsToName.set(newRef, config.name); return newRef; } } return ref as OutputType<Types>; } onTypeConfig(ref: ConfigurableRef<Types>, cb: (config: PothosTypeConfig) => void) { if (!ref) { throw new PothosSchemaError(`${String(ref)} is not a valid type ref`); } if (this.refsToName.has(ref)) { cb(this.getTypeConfig(ref)); } else if (typeof ref === "string" && this.typeConfigs.has(ref)) { cb(this.typeConfigs.get(ref)!); } else if (!this.pending) { throw new PothosSchemaError(`Ref ${String(ref)} has not been implemented`); } else if (this.pendingRefResolutions.has(ref)) { this.pendingRefResolutions.get(ref)!.push(cb); } else { this.pendingRefResolutions.set(ref, [cb]); } } onFieldUse(ref: FieldRef | InputFieldRef, cb: (config: PothosFieldConfig<Types>) => void) { if (!this.fieldRefCallbacks.has(ref)) { this.fieldRefCallbacks.set(ref, []); } this.fieldRefCallbacks.get(ref)!.push(cb); if (this.fieldRefsToConfigs.has(ref)) { this.fieldRefsToConfigs.get(ref)!.forEach((config) => void cb(config)); } } getFields<T extends GraphQLFieldKind>(name: string, kind?: T): Map<string, Extract<PothosFieldConfig<Types>, { graphqlKind: T; }>> { const typeConfig = this.getTypeConfig(name); if (!this.fields.has(name)) { this.fields.set(name, new Map()); } const fields = this.fields.get(name)!; if (kind && typeConfig.graphqlKind !== kind) { throw new PothosSchemaError(`Expected ${name} to be a ${kind} type, but found ${typeConfig.graphqlKind}`); } return fields as Map<string, Extract<PothosFieldConfig<Types>, { graphqlKind: T; }>>; } prepareForBuild() { this.pending = false; const { pendingActions } = this; this.pendingActions = []; pendingActions.forEach((fn) => void fn()); if (this.pendingRefResolutions.size > 0) { throw new PothosSchemaError(`Missing implementations for some references (${[...this.pendingRefResolutions.keys()] .map((ref) => this.describeRef(ref)) .join(", ")}).`); } } onPrepare(cb: () => void) { if (this.pending) { this.pendingActions.push(cb); } else { cb(); } } addFields(typeRef: ConfigurableRef<Types>, fields: FieldMap | InputFieldMap | (() => FieldMap | InputFieldMap)) { this.onPrepare(() => void this.onTypeConfig(typeRef, (config) => { this.buildFields(typeRef, typeof fields === "function" ? fields() : fields); })); } getImplementers(ref: ConfigurableRef<Types> | string) { const typeConfig = this.getTypeConfig(ref, "Interface"); const implementers = [...this.typeConfigs.values()].filter((type) => type.kind === "Object" && type.interfaces.find((i) => this.getTypeConfig(i).name === typeConfig.name)) as PothosObjectTypeConfig[]; return implementers; } private describeRef(ref: ConfigurableRef<Types>): string { if (typeof ref === "string") { return ref; } if (ref.toString !== {}.toString) { return String(ref); } const usedBy = [...this.pendingFields.entries()].find(([fieldRef, typeRef]) => typeRef === ref)?.[0]; if (usedBy) { return `<unnamed ref or enum: used by ${usedBy}>`; } return `<unnamed ref or enum>`; } private buildFields(typeRef: ConfigurableRef<Types>, fields: FieldMap | InputFieldMap) { Object.keys(fields).forEach((fieldName) => { const fieldRef = fields[fieldName]; fieldRef.fieldName = fieldName; if (this.pendingFields.has(fieldRef)) { this.onTypeConfig(this.pendingFields.get(fieldRef)!, () => { this.buildField(typeRef, fieldRef, fieldName); }); } else { this.buildField(typeRef, fieldRef, fieldName); } }); } private buildField(typeRef: ConfigurableRef<Types>, field: FieldRef | InputFieldRef, fieldName: string) { const typeConfig = this.getTypeConfig(typeRef); const fieldConfig = this.createFieldConfig(field, fieldName, typeConfig); const existingFields = this.getFields(typeConfig.name); if (existingFields.has(fieldName)) { throw new PothosSchemaError(`Duplicate field definition for field ${fieldName} in ${typeConfig.name}`); } if (fieldConfig.graphqlKind !== typeConfig.graphqlKind) { throw new PothosSchemaError(`${typeConfig.name}.${fieldName} was defined as a ${fieldConfig.graphqlKind} field but ${typeConfig.name} is a ${typeConfig.graphqlKind}`); } existingFields.set(fieldName, fieldConfig); if (!this.fieldRefsToConfigs.has(field)) { this.fieldRefsToConfigs.set(field, []); } this.fieldRefsToConfigs.get(field)!.push(fieldConfig); if (this.fieldRefCallbacks.has(field)) { this.fieldRefCallbacks.get(field)!.forEach((cb) => void cb(fieldConfig)); } }}
pothos

Version Info

Tagged at
a year ago