graphile-utils 4.12.3 → 5.0.0-0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +28 -26
  3. package/dist/gql.d.ts +3 -0
  4. package/dist/gql.d.ts.map +1 -0
  5. package/dist/gql.js +60 -0
  6. package/dist/gql.js.map +1 -0
  7. package/dist/index.d.ts +8 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +21 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/makeAddInflectorsPlugin.d.ts +3 -0
  12. package/dist/makeAddInflectorsPlugin.d.ts.map +1 -0
  13. package/dist/makeAddInflectorsPlugin.js +42 -0
  14. package/dist/makeAddInflectorsPlugin.js.map +1 -0
  15. package/dist/makeChangeNullabilityPlugin.d.ts +15 -0
  16. package/dist/makeChangeNullabilityPlugin.d.ts.map +1 -0
  17. package/dist/makeChangeNullabilityPlugin.js +146 -0
  18. package/dist/makeChangeNullabilityPlugin.js.map +1 -0
  19. package/dist/makeExtendSchemaPlugin.d.ts +78 -0
  20. package/dist/makeExtendSchemaPlugin.d.ts.map +1 -0
  21. package/dist/makeExtendSchemaPlugin.js +654 -0
  22. package/dist/makeExtendSchemaPlugin.js.map +1 -0
  23. package/{node8plus → dist}/makePgSmartTagsPlugin.d.ts +25 -10
  24. package/dist/makePgSmartTagsPlugin.d.ts.map +1 -0
  25. package/dist/makePgSmartTagsPlugin.js +398 -0
  26. package/dist/makePgSmartTagsPlugin.js.map +1 -0
  27. package/dist/makeProcessSchemaPlugin.d.ts +6 -0
  28. package/dist/makeProcessSchemaPlugin.d.ts.map +1 -0
  29. package/dist/makeProcessSchemaPlugin.js +20 -0
  30. package/dist/makeProcessSchemaPlugin.js.map +1 -0
  31. package/dist/makeWrapPlansPlugin.d.ts +22 -0
  32. package/dist/makeWrapPlansPlugin.d.ts.map +1 -0
  33. package/dist/makeWrapPlansPlugin.js +94 -0
  34. package/dist/makeWrapPlansPlugin.js.map +1 -0
  35. package/dist/parseIdentifierParts.d.ts +2 -0
  36. package/dist/parseIdentifierParts.d.ts.map +1 -0
  37. package/{node8plus → dist}/parseIdentifierParts.js +2 -1
  38. package/dist/parseIdentifierParts.js.map +1 -0
  39. package/package.json +38 -26
  40. package/LICENSE.md +0 -24
  41. package/node8plus/fieldHelpers.d.ts +0 -13
  42. package/node8plus/fieldHelpers.js +0 -81
  43. package/node8plus/fieldHelpers.js.map +0 -1
  44. package/node8plus/gql.d.ts +0 -11
  45. package/node8plus/gql.js +0 -76
  46. package/node8plus/gql.js.map +0 -1
  47. package/node8plus/index.d.ts +0 -12
  48. package/node8plus/index.js +0 -26
  49. package/node8plus/index.js.map +0 -1
  50. package/node8plus/introspectionHelpers.d.ts +0 -7
  51. package/node8plus/introspectionHelpers.js +0 -62
  52. package/node8plus/introspectionHelpers.js.map +0 -1
  53. package/node8plus/makeAddInflectorsPlugin.d.ts +0 -7
  54. package/node8plus/makeAddInflectorsPlugin.js +0 -21
  55. package/node8plus/makeAddInflectorsPlugin.js.map +0 -1
  56. package/node8plus/makeAddPgTableConditionPlugin.d.ts +0 -8
  57. package/node8plus/makeAddPgTableConditionPlugin.js +0 -89
  58. package/node8plus/makeAddPgTableConditionPlugin.js.map +0 -1
  59. package/node8plus/makeAddPgTableOrderByPlugin.d.ts +0 -23
  60. package/node8plus/makeAddPgTableOrderByPlugin.js +0 -76
  61. package/node8plus/makeAddPgTableOrderByPlugin.js.map +0 -1
  62. package/node8plus/makeChangeNullabilityPlugin.d.ts +0 -8
  63. package/node8plus/makeChangeNullabilityPlugin.js +0 -28
  64. package/node8plus/makeChangeNullabilityPlugin.js.map +0 -1
  65. package/node8plus/makeExtendSchemaPlugin.d.ts +0 -28
  66. package/node8plus/makeExtendSchemaPlugin.js +0 -703
  67. package/node8plus/makeExtendSchemaPlugin.js.map +0 -1
  68. package/node8plus/makePgSmartTagsPlugin.js +0 -200
  69. package/node8plus/makePgSmartTagsPlugin.js.map +0 -1
  70. package/node8plus/makePluginByCombiningPlugins.d.ts +0 -2
  71. package/node8plus/makePluginByCombiningPlugins.js +0 -11
  72. package/node8plus/makePluginByCombiningPlugins.js.map +0 -1
  73. package/node8plus/makeProcessSchemaPlugin.d.ts +0 -5
  74. package/node8plus/makeProcessSchemaPlugin.js +0 -9
  75. package/node8plus/makeProcessSchemaPlugin.js.map +0 -1
  76. package/node8plus/makeWrapResolversPlugin.d.ts +0 -30
  77. package/node8plus/makeWrapResolversPlugin.js +0 -80
  78. package/node8plus/makeWrapResolversPlugin.js.map +0 -1
  79. package/node8plus/parseIdentifierParts.d.ts +0 -1
  80. package/node8plus/parseIdentifierParts.js.map +0 -1
@@ -0,0 +1,654 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeExtendSchemaPlugin = exports.EXPORTABLE = void 0;
4
+ function EXPORTABLE(factory, args) {
5
+ const fn = factory(...args);
6
+ if (!("$exporter$factory" in fn)) {
7
+ Object.defineProperties(fn, {
8
+ $exporter$args: { value: args },
9
+ $exporter$factory: { value: factory },
10
+ });
11
+ }
12
+ return fn;
13
+ }
14
+ exports.EXPORTABLE = EXPORTABLE;
15
+ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugin_${String(Math.random()).slice(2)}`) {
16
+ let graphql;
17
+ return {
18
+ name: uniquePluginName,
19
+ version: "0.0.0",
20
+ schema: {
21
+ hooks: {
22
+ build(build) {
23
+ // Extract GraphQL into the scope so that our other functions can use it.
24
+ graphql = build.graphql;
25
+ const { options } = build;
26
+ const { GraphQLEnumType, GraphQLInputObjectType, GraphQLObjectType, GraphQLScalarType, GraphQLDirective, GraphQLUnionType, GraphQLInterfaceType, } = graphql;
27
+ const { typeDefs, resolvers = {}, plans = {}, } = typeof generator === "function"
28
+ ? generator(build, options)
29
+ : generator;
30
+ const typeDefsArr = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
31
+ const mergedTypeDefinitions = typeDefsArr.reduce((definitions, typeDef) => {
32
+ if (!typeDef || typeDef.kind !== "Document") {
33
+ throw new Error("The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper, or be an array of the same.");
34
+ }
35
+ definitions.push(...typeDef.definitions);
36
+ return definitions;
37
+ }, []);
38
+ const typeExtensions = {
39
+ GraphQLSchema: {
40
+ directives: [],
41
+ types: [],
42
+ },
43
+ GraphQLInputObjectType: {},
44
+ GraphQLObjectType: {},
45
+ GraphQLInterfaceType: {},
46
+ };
47
+ const newTypes = [];
48
+ mergedTypeDefinitions.forEach((definition) => {
49
+ if (definition.kind === "EnumTypeDefinition") {
50
+ newTypes.push({
51
+ type: GraphQLEnumType,
52
+ definition,
53
+ });
54
+ }
55
+ else if (definition.kind === "ObjectTypeExtension") {
56
+ const name = getName(definition.name);
57
+ if (!typeExtensions.GraphQLObjectType[name]) {
58
+ typeExtensions.GraphQLObjectType[name] = [];
59
+ }
60
+ typeExtensions.GraphQLObjectType[name].push(definition);
61
+ }
62
+ else if (definition.kind === "InputObjectTypeExtension") {
63
+ const name = getName(definition.name);
64
+ if (!typeExtensions.GraphQLInputObjectType[name]) {
65
+ typeExtensions.GraphQLInputObjectType[name] = [];
66
+ }
67
+ typeExtensions.GraphQLInputObjectType[name].push(definition);
68
+ }
69
+ else if (definition.kind === "InterfaceTypeExtension") {
70
+ const name = getName(definition.name);
71
+ if (!typeExtensions.GraphQLInterfaceType[name]) {
72
+ typeExtensions.GraphQLInterfaceType[name] = [];
73
+ }
74
+ typeExtensions.GraphQLInterfaceType[name].push(definition);
75
+ }
76
+ else if (definition.kind === "ObjectTypeDefinition") {
77
+ newTypes.push({
78
+ type: GraphQLObjectType,
79
+ definition,
80
+ });
81
+ }
82
+ else if (definition.kind === "InputObjectTypeDefinition") {
83
+ newTypes.push({
84
+ type: GraphQLInputObjectType,
85
+ definition,
86
+ });
87
+ }
88
+ else if (definition.kind === "UnionTypeDefinition") {
89
+ newTypes.push({
90
+ type: GraphQLUnionType,
91
+ definition,
92
+ });
93
+ }
94
+ else if (definition.kind === "InterfaceTypeDefinition") {
95
+ newTypes.push({
96
+ type: GraphQLInterfaceType,
97
+ definition,
98
+ });
99
+ }
100
+ else if (definition.kind === "DirectiveDefinition") {
101
+ newTypes.push({
102
+ type: GraphQLDirective,
103
+ definition,
104
+ });
105
+ }
106
+ else if (definition.kind === "ScalarTypeDefinition") {
107
+ newTypes.push({
108
+ type: GraphQLScalarType,
109
+ definition,
110
+ });
111
+ }
112
+ else {
113
+ if (definition.kind === "TypeExtensionDefinition") {
114
+ throw new Error(`You appear to be using a GraphQL version prior to v0.12.0 which has different syntax for schema extensions (e.g. 'TypeExtensionDefinition' instead of 'ObjectTypeExtension'). Sadly makeExtendSchemaPlugin does not support versions of graphql prior to 0.12.0, please update your version of graphql.`);
115
+ }
116
+ throw new Error(`Unexpected '${definition.kind}' definition; we were expecting 'GraphQLEnumType', 'ObjectTypeExtension', 'InputObjectTypeExtension', 'ObjectTypeDefinition' or 'InputObjectTypeDefinition', i.e. something like 'extend type Foo { ... }'`);
117
+ }
118
+ });
119
+ if (!build.makeExtendSchemaPlugin) {
120
+ build.makeExtendSchemaPlugin = Object.create(null);
121
+ }
122
+ build.makeExtendSchemaPlugin[uniquePluginName] = {
123
+ typeExtensions,
124
+ newTypes,
125
+ resolvers,
126
+ plans,
127
+ };
128
+ return build;
129
+ },
130
+ init(_, build, _context) {
131
+ const { makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, newTypes, resolvers, plans, }, }, graphql: { GraphQLEnumType, GraphQLObjectType, GraphQLInputObjectType, GraphQLUnionType, GraphQLInterfaceType, GraphQLScalarType, GraphQLDirective, Kind, }, } = build;
132
+ newTypes.forEach((def) => {
133
+ if (def.type === GraphQLEnumType) {
134
+ const definition = def.definition;
135
+ // https://graphql.org/graphql-js/type/#graphqlenumtype
136
+ const name = getName(definition.name);
137
+ const description = getDescription(definition.description);
138
+ const directives = getDirectives(definition.directives);
139
+ const relevantResolver = plans[name] || resolvers[name] || {};
140
+ const values = (definition.values ?? []).reduce((memo, value) => {
141
+ const valueName = getName(value.name);
142
+ const valueDescription = getDescription(value.description);
143
+ const valueDirectives = getDirectives(value.directives);
144
+ // Value cannot be expressed via SDL, so we grab the value from the resolvers instead.
145
+ // resolvers = {
146
+ // MyEnum: {
147
+ // MY_ENUM_VALUE1: 'value1',
148
+ // MY_ENUM_VALUE2: 'value2',
149
+ // }
150
+ // }
151
+ // Ref: https://github.com/graphql/graphql-js/issues/525#issuecomment-255834625
152
+ const valueValue = relevantResolver[valueName] !== undefined
153
+ ? relevantResolver[valueName]
154
+ : valueName;
155
+ const deprecatedDirective = valueDirectives.find((d) => d.directiveName === "deprecated");
156
+ const valueDeprecationReason = deprecatedDirective?.args.reason;
157
+ return {
158
+ ...memo,
159
+ [valueName]: {
160
+ value: valueValue,
161
+ deprecationReason: valueDeprecationReason,
162
+ description: valueDescription,
163
+ directives: valueDirectives,
164
+ },
165
+ };
166
+ }, {});
167
+ const scope = scopeFromDirectives(directives);
168
+ build.registerEnumType(name, scope, () => ({ values, description }), uniquePluginName);
169
+ }
170
+ else if (def.type === GraphQLObjectType) {
171
+ const definition = def.definition;
172
+ // https://graphql.org/graphql-js/type/#graphqlobjecttype
173
+ const name = getName(definition.name);
174
+ const description = getDescription(definition.description);
175
+ const interfaces = getInterfaces(definition.interfaces, build);
176
+ const directives = getDirectives(definition.directives);
177
+ const scope = {
178
+ __origin: `makeExtendSchemaPlugin`,
179
+ directives,
180
+ ...scopeFromDirectives(directives),
181
+ };
182
+ build.registerObjectType(name, scope, null, () => ({
183
+ interfaces,
184
+ fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, resolvers, plans, fieldsContext, build),
185
+ ...(description
186
+ ? {
187
+ description,
188
+ }
189
+ : null),
190
+ }), uniquePluginName);
191
+ }
192
+ else if (def.type === GraphQLInputObjectType) {
193
+ const definition = def.definition;
194
+ // https://graphql.org/graphql-js/type/#graphqlinputobjecttype
195
+ const name = getName(definition.name);
196
+ const description = getDescription(definition.description);
197
+ const directives = getDirectives(definition.directives);
198
+ const scope = {
199
+ __origin: `makeExtendSchemaPlugin`,
200
+ directives,
201
+ ...scopeFromDirectives(directives),
202
+ };
203
+ build.registerInputObjectType(name, scope, () => ({
204
+ fields: ({ Self }) => getInputFields(Self, definition.fields, build),
205
+ ...(description
206
+ ? {
207
+ description,
208
+ }
209
+ : null),
210
+ }), uniquePluginName);
211
+ }
212
+ else if (def.type === GraphQLUnionType) {
213
+ const definition = def.definition;
214
+ // https://graphql.org/graphql-js/type/#graphqluniontype
215
+ const name = getName(definition.name);
216
+ const description = getDescription(definition.description);
217
+ const directives = getDirectives(definition.directives);
218
+ const scope = {
219
+ __origin: `makeExtendSchemaPlugin`,
220
+ directives,
221
+ ...scopeFromDirectives(directives),
222
+ };
223
+ const resolveType = resolvers[name]?.__resolveType;
224
+ build.registerUnionType(name, scope, () => ({
225
+ types: () => {
226
+ if (Array.isArray(definition.types)) {
227
+ return definition.types.map((typeAST) => {
228
+ if (typeAST.kind !== "NamedType") {
229
+ throw new Error("Only support unions of named types");
230
+ }
231
+ return getType(typeAST, build);
232
+ });
233
+ }
234
+ else {
235
+ return [];
236
+ }
237
+ },
238
+ ...(resolveType ? { resolveType } : null),
239
+ ...(description ? { description } : null),
240
+ }), uniquePluginName);
241
+ }
242
+ else if (def.type === GraphQLInterfaceType) {
243
+ const definition = def.definition;
244
+ // https://graphql.org/graphql-js/type/#graphqluniontype
245
+ const name = getName(definition.name);
246
+ const description = getDescription(definition.description);
247
+ const directives = getDirectives(definition.directives);
248
+ const scope = {
249
+ __origin: `makeExtendSchemaPlugin`,
250
+ directives,
251
+ ...scopeFromDirectives(directives),
252
+ };
253
+ const resolveType = resolvers[name]?.__resolveType;
254
+ build.registerInterfaceType(name, scope, () => ({
255
+ ...(resolveType ? { resolveType } : null),
256
+ ...(description ? { description } : null),
257
+ fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, {}, // Interface doesn't need resolvers
258
+ {}, // Interface doesn't need resolvers
259
+ fieldsContext, build),
260
+ ...(description
261
+ ? {
262
+ description,
263
+ }
264
+ : null),
265
+ }), uniquePluginName);
266
+ }
267
+ else if (def.type === GraphQLScalarType) {
268
+ const definition = def.definition;
269
+ const name = getName(definition.name);
270
+ const description = getDescription(definition.description);
271
+ const directives = getDirectives(definition.directives);
272
+ const scope = {
273
+ __origin: `makeExtendSchemaPlugin`,
274
+ directives,
275
+ ...scopeFromDirectives(directives),
276
+ };
277
+ build.registerScalarType(name, scope, () => ({
278
+ description,
279
+ serialize: EXPORTABLE(() => (value) => String(value), []),
280
+ parseValue: EXPORTABLE(() => (value) => String(value), []),
281
+ parseLiteral: EXPORTABLE((Kind) => (ast) => {
282
+ if (ast.kind !== Kind.STRING) {
283
+ throw new Error("Can only parse string values");
284
+ }
285
+ return ast.value;
286
+ }, [Kind]),
287
+ }), uniquePluginName);
288
+ }
289
+ else if (def.type === GraphQLDirective) {
290
+ const definition = def.definition;
291
+ // https://github.com/graphql/graphql-js/blob/3c54315ab13c6b9d337fb7c33ad7e27b92ca4a40/src/type/directives.js#L106-L113
292
+ const name = getName(definition.name);
293
+ const description = getDescription(definition.description);
294
+ const locations = definition.locations.map(getName);
295
+ const args = getArguments(definition.arguments, build);
296
+ // Ignoring isRepeatable and astNode for now
297
+ // TODO: should we add hooks for directives?
298
+ const directive = new GraphQLDirective({
299
+ name,
300
+ locations,
301
+ args,
302
+ ...(description ? { description } : null),
303
+ });
304
+ typeExtensions.GraphQLSchema.directives.push(directive);
305
+ }
306
+ else {
307
+ throw new Error(`We have no code to build an object of type '${def.type}'; it should not have reached this area of the code.`);
308
+ }
309
+ });
310
+ return _;
311
+ },
312
+ GraphQLSchema(schema, build, _context) {
313
+ const { makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
314
+ return {
315
+ ...schema,
316
+ directives: [
317
+ ...(schema.directives || build.graphql.specifiedDirectives || []),
318
+ ...typeExtensions.GraphQLSchema.directives,
319
+ ],
320
+ types: [
321
+ ...(schema.types || []),
322
+ ...typeExtensions.GraphQLSchema.types,
323
+ ],
324
+ };
325
+ },
326
+ GraphQLObjectType_fields(fields, build, context) {
327
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, resolvers, plans }, }, } = build;
328
+ const { Self } = context;
329
+ if (typeExtensions.GraphQLObjectType[Self.name]) {
330
+ const newFields = typeExtensions.GraphQLObjectType[Self.name].reduce((memo, extension) => {
331
+ const moreFields = getFields(Self, extension.fields, resolvers, plans, context, build);
332
+ return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
333
+ }, {});
334
+ return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
335
+ }
336
+ else {
337
+ return fields;
338
+ }
339
+ },
340
+ GraphQLObjectType_interfaces(interfaces, build, context) {
341
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
342
+ const { Self } = context;
343
+ if (typeExtensions.GraphQLObjectType[Self.name]) {
344
+ const newInterfaces = typeExtensions.GraphQLObjectType[Self.name].reduce((memo, extension) => {
345
+ const moreInterfaces = getInterfaces(extension.interfaces, build);
346
+ return extend(memo, moreInterfaces, `Adding interfaces from ${uniquePluginName}`);
347
+ }, {});
348
+ return extend(interfaces, newInterfaces, `Adding interfaces from ${uniquePluginName}`);
349
+ }
350
+ else {
351
+ return interfaces;
352
+ }
353
+ },
354
+ GraphQLInputObjectType_fields(fields, build, context) {
355
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
356
+ const { Self } = context;
357
+ if (typeExtensions.GraphQLInputObjectType[Self.name]) {
358
+ const newFields = typeExtensions.GraphQLInputObjectType[Self.name].reduce((memo, extension) => {
359
+ const moreFields = getInputFields(Self, extension.fields, build);
360
+ return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
361
+ }, {});
362
+ return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
363
+ }
364
+ else {
365
+ return fields;
366
+ }
367
+ },
368
+ GraphQLInterfaceType_fields(fields, build, context) {
369
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
370
+ const { Self } = context;
371
+ if (typeExtensions.GraphQLInterfaceType[Self.name]) {
372
+ const newFields = typeExtensions.GraphQLInterfaceType[Self.name].reduce((memo, extension) => {
373
+ const moreFields = getFields(Self, extension.fields, {}, // No resolvers for interfaces
374
+ {}, // No resolvers for interfaces
375
+ context, build);
376
+ return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
377
+ }, {});
378
+ return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
379
+ }
380
+ else {
381
+ return fields;
382
+ }
383
+ },
384
+ GraphQLInterfaceType_interfaces(interfaces, build, context) {
385
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
386
+ const { Self } = context;
387
+ if (typeExtensions.GraphQLInterfaceType[Self.name]) {
388
+ const newInterfaces = typeExtensions.GraphQLInterfaceType[Self.name].reduce((memo, extension) => {
389
+ const moreInterfaces = getInterfaces(extension.interfaces, build);
390
+ return extend(memo, moreInterfaces, `Adding interfaces from ${uniquePluginName}`);
391
+ }, {});
392
+ return extend(interfaces, newInterfaces, `Adding interfaces from ${uniquePluginName}`);
393
+ }
394
+ else {
395
+ return interfaces;
396
+ }
397
+ },
398
+ },
399
+ },
400
+ };
401
+ function getName(name) {
402
+ if (name && name.kind === "Name" && name.value) {
403
+ return name.value;
404
+ }
405
+ throw new Error("Could not extract name from AST");
406
+ }
407
+ function getDescription(desc) {
408
+ if (!desc) {
409
+ return null;
410
+ }
411
+ else if (desc.kind === "StringValue") {
412
+ return desc.value;
413
+ }
414
+ else {
415
+ throw new Error(`AST issue, we weren't expecting a description of kind '${desc.kind}' - PRs welcome!`);
416
+ }
417
+ }
418
+ function getType(type, build) {
419
+ if (type.kind === "NamedType") {
420
+ const Type = build.getTypeByName(getName(type.name));
421
+ if (!Type) {
422
+ throw new Error(`Could not find type named '${getName(type.name)}'.`);
423
+ }
424
+ return Type;
425
+ }
426
+ else if (type.kind === "NonNullType") {
427
+ return new build.graphql.GraphQLNonNull(getType(type.type, build));
428
+ }
429
+ else if (type.kind === "ListType") {
430
+ return new build.graphql.GraphQLList(getType(type.type, build));
431
+ }
432
+ else {
433
+ throw new Error(`We don't support AST type definition of kind '${type.kind}' yet... PRs welcome!`);
434
+ }
435
+ }
436
+ function getInterfaces(interfaces, build) {
437
+ if (!interfaces)
438
+ return [];
439
+ return interfaces.map((i) => build.getTypeByName(i.name.value));
440
+ }
441
+ function getValue(value, inType) {
442
+ const type = inType && graphql.isNonNullType(inType) ? inType.ofType : inType;
443
+ if (value.kind === "BooleanValue") {
444
+ return !!value.value;
445
+ }
446
+ else if (value.kind === "StringValue") {
447
+ return value.value;
448
+ }
449
+ else if (value.kind === "IntValue") {
450
+ return parseInt(value.value, 10);
451
+ }
452
+ else if (value.kind === "FloatValue") {
453
+ return parseFloat(value.value);
454
+ }
455
+ else if (value.kind === "EnumValue") {
456
+ if (!type) {
457
+ throw new Error("We do not support EnumValue arguments in directives at this time");
458
+ }
459
+ const enumValueName = value.value;
460
+ const enumType = graphql.isEnumType(type)
461
+ ? type
462
+ : null;
463
+ if (!enumType) {
464
+ throw new Error(`Tried to interpret an EnumValue for non-enum type ${type}`);
465
+ }
466
+ const values = enumType.getValues();
467
+ const enumValue = values.find((v) => v.name === enumValueName);
468
+ return enumValue ? enumValue.value : undefined;
469
+ }
470
+ else if (value.kind === "NullValue") {
471
+ return null;
472
+ }
473
+ else if (value.kind === "ListValue") {
474
+ // This is used in directives, so we cannot assume the type is known.
475
+ const childType = type && graphql.isListType(type) ? type.ofType : null;
476
+ return value.values.map((value) => getValue(value, childType));
477
+ }
478
+ else {
479
+ throw new Error(`Value kind '${value.kind}' not supported yet. PRs welcome!`);
480
+ }
481
+ }
482
+ function getDirectives(directives) {
483
+ return (directives || []).reduce((directivesList, directive) => {
484
+ if (directive.kind !== "Directive") {
485
+ throw new Error(`Unexpected '${directive.kind}', we were expecting 'Directive'`);
486
+ }
487
+ const name = getName(directive.name);
488
+ const value = (directive.arguments || []).reduce((argumentValues, arg) => {
489
+ if (arg.kind === "Argument") {
490
+ const argName = getName(arg.name);
491
+ const argValue = getValue(arg.value);
492
+ if (argumentValues[name]) {
493
+ throw new Error(`Argument '${argName}' of directive '${name}' must only be used once.`);
494
+ }
495
+ argumentValues[argName] = argValue;
496
+ }
497
+ else {
498
+ throw new Error(`Unexpected '${arg.kind}', we were expecting 'Argument'`);
499
+ }
500
+ return argumentValues;
501
+ }, Object.create(null));
502
+ directivesList.push({ directiveName: name, args: value });
503
+ return directivesList;
504
+ }, []);
505
+ }
506
+ function getArguments(args, build) {
507
+ if (args && args.length) {
508
+ return args.reduce((memo, arg) => {
509
+ if (arg.kind === "InputValueDefinition") {
510
+ const name = getName(arg.name);
511
+ const type = getType(arg.type, build);
512
+ const description = getDescription(arg.description);
513
+ let defaultValue;
514
+ if (arg.defaultValue) {
515
+ defaultValue = getValue(arg.defaultValue, type);
516
+ }
517
+ memo[name] = {
518
+ type,
519
+ ...(defaultValue != null ? { defaultValue } : null),
520
+ ...(description ? { description } : null),
521
+ };
522
+ }
523
+ else {
524
+ throw new Error(`Unexpected '${arg.kind}', we were expecting an 'InputValueDefinition'`);
525
+ }
526
+ return memo;
527
+ }, {});
528
+ }
529
+ return {};
530
+ }
531
+ function getFields(SelfGeneric, fields, resolvers, plans, { fieldWithHooks, }, build) {
532
+ if (!build.graphql.isNamedType(SelfGeneric)) {
533
+ throw new Error("getFields only supports named types");
534
+ }
535
+ const Self = SelfGeneric;
536
+ if (fields && fields.length) {
537
+ return fields.reduce((memo, field) => {
538
+ if (field.kind === "FieldDefinition") {
539
+ const description = getDescription(field.description);
540
+ const fieldName = getName(field.name);
541
+ const args = getArguments(field.arguments, build);
542
+ const type = getType(field.type, build);
543
+ const directives = getDirectives(field.directives);
544
+ const scope = {
545
+ fieldName,
546
+ /*
547
+ ...(typeScope.pgIntrospection &&
548
+ typeScope.pgIntrospection.kind === "class"
549
+ ? {
550
+ pgFieldIntrospection: typeScope.pgIntrospection,
551
+ }
552
+ : null),
553
+ ...(typeScope.isPgRowConnectionType && typeScope.pgIntrospection
554
+ ? {
555
+ isPgFieldConnection: true,
556
+ pgFieldIntrospection: typeScope.pgIntrospection,
557
+ }
558
+ : null),
559
+ */
560
+ fieldDirectives: directives,
561
+ ...scopeFromDirectives(directives),
562
+ };
563
+ const deprecatedDirective = directives.find((d) => d.directiveName === "deprecated");
564
+ const deprecationReason = deprecatedDirective?.args.reason;
565
+ const functionToResolveObject = (functionOrResolveObject) => typeof functionOrResolveObject === "function"
566
+ ? { resolve: functionOrResolveObject }
567
+ : functionOrResolveObject;
568
+ /*
569
+ * We accept a resolver function directly, or an object which can
570
+ * define 'resolve', 'subscribe' and other relevant methods.
571
+ */
572
+ const possiblePlan = plans[Self.name]?.[fieldName];
573
+ const possibleResolver = resolvers[Self.name]
574
+ ? resolvers[Self.name][fieldName]
575
+ : null;
576
+ const resolver = possibleResolver &&
577
+ (typeof possibleResolver === "object" ||
578
+ typeof possibleResolver === "function")
579
+ ? possibleResolver
580
+ : null;
581
+ const rawResolversSpec = resolver
582
+ ? functionToResolveObject(resolver)
583
+ : null;
584
+ const fieldSpecGenerator = () => {
585
+ const resolversSpec = rawResolversSpec;
586
+ return {
587
+ type: type,
588
+ args,
589
+ ...(deprecationReason
590
+ ? {
591
+ deprecationReason,
592
+ }
593
+ : null),
594
+ ...(description
595
+ ? {
596
+ description,
597
+ }
598
+ : null),
599
+ ...resolversSpec,
600
+ ...(possiblePlan
601
+ ? { plan: possiblePlan }
602
+ : null),
603
+ };
604
+ };
605
+ return build.extend(memo, {
606
+ [fieldName]: fieldWithHooks(scope, fieldSpecGenerator),
607
+ }, `Adding '${fieldName}' to '${Self.name}' from '${uniquePluginName}'`);
608
+ }
609
+ else {
610
+ throw new Error(`AST issue: expected 'FieldDefinition', instead received '${field.kind}'`);
611
+ }
612
+ }, {});
613
+ }
614
+ return {};
615
+ }
616
+ function getInputFields(_Self, fields, build) {
617
+ if (fields && fields.length) {
618
+ return fields.reduce((memo, field) => {
619
+ if (field.kind === "InputValueDefinition") {
620
+ const description = getDescription(field.description);
621
+ const fieldName = getName(field.name);
622
+ const type = getType(field.type, build);
623
+ const defaultValue = field.defaultValue
624
+ ? getValue(field.defaultValue, type)
625
+ : undefined;
626
+ memo[fieldName] = {
627
+ type,
628
+ defaultValue,
629
+ ...(description
630
+ ? {
631
+ description,
632
+ }
633
+ : null),
634
+ };
635
+ }
636
+ else {
637
+ throw new Error(`AST issue: expected 'FieldDefinition', instead received '${field.kind}'`);
638
+ }
639
+ return memo;
640
+ }, {});
641
+ }
642
+ return {};
643
+ }
644
+ }
645
+ exports.makeExtendSchemaPlugin = makeExtendSchemaPlugin;
646
+ function scopeFromDirectives(directives) {
647
+ return {
648
+ ...directives
649
+ .filter((d) => d.directiveName === "scope")
650
+ .map((d) => d.args)
651
+ .reduce((memo, a) => Object.assign(memo, a), Object.create(null)),
652
+ };
653
+ }
654
+ //# sourceMappingURL=makeExtendSchemaPlugin.js.map