graphile-utils 5.0.0-beta.9 → 5.0.0-rc.1

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 (45) hide show
  1. package/CHANGELOG.md +1064 -1
  2. package/README.md +87 -72
  3. package/dist/exportable.d.ts +4 -1
  4. package/dist/exportable.d.ts.map +1 -1
  5. package/dist/exportable.js +15 -4
  6. package/dist/exportable.js.map +1 -1
  7. package/dist/gql.js +1 -2
  8. package/dist/gql.js.map +1 -1
  9. package/dist/index.d.ts +42 -7
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +11 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/makeAddInflectorsPlugin.js +2 -3
  14. package/dist/makeAddInflectorsPlugin.js.map +1 -1
  15. package/dist/makeAddPgTableConditionPlugin.d.ts +32 -6
  16. package/dist/makeAddPgTableConditionPlugin.d.ts.map +1 -1
  17. package/dist/makeAddPgTableConditionPlugin.js +28 -11
  18. package/dist/makeAddPgTableConditionPlugin.js.map +1 -1
  19. package/dist/makeAddPgTableOrderByPlugin.d.ts +15 -10
  20. package/dist/makeAddPgTableOrderByPlugin.d.ts.map +1 -1
  21. package/dist/makeAddPgTableOrderByPlugin.js +43 -35
  22. package/dist/makeAddPgTableOrderByPlugin.js.map +1 -1
  23. package/dist/makeChangeNullabilityPlugin.d.ts +3 -1
  24. package/dist/makeChangeNullabilityPlugin.d.ts.map +1 -1
  25. package/dist/makeChangeNullabilityPlugin.js +98 -18
  26. package/dist/makeChangeNullabilityPlugin.js.map +1 -1
  27. package/dist/makeExtendSchemaPlugin.d.ts +55 -26
  28. package/dist/makeExtendSchemaPlugin.d.ts.map +1 -1
  29. package/dist/makeExtendSchemaPlugin.js +402 -105
  30. package/dist/makeExtendSchemaPlugin.js.map +1 -1
  31. package/dist/makePgSmartTagsPlugin.d.ts +16 -3
  32. package/dist/makePgSmartTagsPlugin.d.ts.map +1 -1
  33. package/dist/makePgSmartTagsPlugin.js +24 -18
  34. package/dist/makePgSmartTagsPlugin.js.map +1 -1
  35. package/dist/makeProcessSchemaPlugin.d.ts +3 -1
  36. package/dist/makeProcessSchemaPlugin.d.ts.map +1 -1
  37. package/dist/makeProcessSchemaPlugin.js +4 -2
  38. package/dist/makeProcessSchemaPlugin.js.map +1 -1
  39. package/dist/makeWrapPlansPlugin.d.ts +6 -3
  40. package/dist/makeWrapPlansPlugin.d.ts.map +1 -1
  41. package/dist/makeWrapPlansPlugin.js +40 -18
  42. package/dist/makeWrapPlansPlugin.js.map +1 -1
  43. package/dist/parseIdentifierParts.js +1 -2
  44. package/dist/parseIdentifierParts.js.map +1 -1
  45. package/package.json +29 -23
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makeExtendSchemaPlugin = void 0;
4
+ exports.extendSchema = extendSchema;
4
5
  const exportable_js_1 = require("./exportable.js");
5
- function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugin_${String(Math.random()).slice(2)}`) {
6
+ function extendSchema(generator, uniquePluginName = `ExtendSchemaPlugin_${String(Math.random()).slice(2)}`) {
6
7
  let graphql;
7
8
  if (typeof generator === "function" && generator.length >= 2) {
8
9
  console.trace("[DEPRECATED] Your makeExtendSchemaPlugin generator callback accepts two arguments: `(build, options)`; instead you should just use the `build` argument since `options` is just `build.options`.");
@@ -21,32 +22,42 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
21
22
  return build;
22
23
  },
23
24
  init(_, build, _context) {
24
- const { GraphQLDirective, GraphQLEnumType, GraphQLError, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, Kind, } = graphql;
25
- const { typeDefs, resolvers = Object.create(null), plans = Object.create(null), } = typeof generator === "function"
25
+ const { GraphQLDirective, GraphQLEnumType, GraphQLError, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, Kind, parse, } = graphql;
26
+ const { typeDefs, resolvers = Object.create(null), plans: rawPlans, enums, scalars, inputObjects, interfaces, unions, objects, } = typeof generator === "function"
26
27
  ? generator.length === 1
27
28
  ? generator(build)
28
29
  : /* TODO: DELETE THIS! */
29
30
  generator(build, build.options)
30
31
  : generator;
31
- const typeDefsArr = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
32
- const mergedTypeDefinitions = typeDefsArr.reduce((definitions, typeDef) => {
33
- if (!typeDef || typeDef.kind !== "Document") {
34
- throw new Error("The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper, or be an array of the same.");
35
- }
36
- definitions.push(...typeDef.definitions);
37
- return definitions;
38
- }, []);
32
+ if (typeDefs == null) {
33
+ throw new Error("The first argument to makeExtendSchemaPlugin must be an object containing a `typeDefs` field.");
34
+ }
35
+ const document = typeof typeDefs === "string"
36
+ ? parse(typeDefs)
37
+ : Array.isArray(typeDefs)
38
+ ? {
39
+ kind: graphql.Kind.DOCUMENT,
40
+ definitions: typeDefs.flatMap((t) => {
41
+ if (!t || t.kind !== "Document") {
42
+ throw new Error('The first argument to makeExtendSchemaPlugin must be an object containing a `typeDefs` field; the value for this field must be generated by the `gql` helper (`import { gql, makeExtendSchemaPlugin } from "postgraphile/utils"`), or be an array of the same.');
43
+ }
44
+ return t.definitions;
45
+ }),
46
+ }
47
+ : typeDefs;
48
+ if (!document || document.kind !== "Document") {
49
+ throw new Error('The first argument to makeExtendSchemaPlugin must be an object containing a `typeDefs` field; the value for this field must be generated by the `gql` helper (`import { gql, makeExtendSchemaPlugin } from "postgraphile/utils"`), or be an array of the same.');
50
+ }
39
51
  const typeExtensions = {
40
52
  GraphQLSchema: {
41
53
  directives: [],
42
- types: [],
43
54
  },
44
55
  GraphQLInputObjectType: {},
45
56
  GraphQLObjectType: {},
46
57
  GraphQLInterfaceType: {},
47
58
  };
48
59
  const newTypes = [];
49
- mergedTypeDefinitions.forEach((definition) => {
60
+ document.definitions.forEach((definition) => {
50
61
  if (definition.kind === "EnumTypeDefinition") {
51
62
  newTypes.push({
52
63
  type: GraphQLEnumType,
@@ -117,6 +128,196 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
117
128
  throw new Error(`Unexpected '${definition.kind}' definition; we were expecting 'GraphQLEnumType', 'ObjectTypeExtension', 'InputObjectTypeExtension', 'ObjectTypeDefinition' or 'InputObjectTypeDefinition', i.e. something like 'extend type Foo { ... }'`);
118
129
  }
119
130
  });
131
+ let plans;
132
+ if (rawPlans) {
133
+ if (objects ||
134
+ unions ||
135
+ interfaces ||
136
+ inputObjects ||
137
+ scalars ||
138
+ enums) {
139
+ throw new Error(`plans is deprecated and may not be specified alongside newer approaches`);
140
+ }
141
+ plans = rawPlans;
142
+ }
143
+ else {
144
+ // Hackily convert the new format into the old format. We'll do away with
145
+ // this in future, but for now it's the easiest way to ensure compatibility
146
+ plans = {};
147
+ const definitionNodes = document.definitions.filter((d) => d.kind === Kind.OBJECT_TYPE_DEFINITION ||
148
+ d.kind === Kind.INTERFACE_TYPE_DEFINITION ||
149
+ d.kind === Kind.UNION_TYPE_DEFINITION ||
150
+ d.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
151
+ d.kind === Kind.ENUM_TYPE_DEFINITION ||
152
+ d.kind === Kind.SCALAR_TYPE_DEFINITION ||
153
+ d.kind === Kind.OBJECT_TYPE_EXTENSION ||
154
+ d.kind === Kind.INTERFACE_TYPE_EXTENSION ||
155
+ d.kind === Kind.UNION_TYPE_EXTENSION ||
156
+ d.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION ||
157
+ d.kind === Kind.ENUM_TYPE_EXTENSION ||
158
+ d.kind === Kind.SCALAR_TYPE_EXTENSION);
159
+ const assertLocation = (typeName, expectedLocation) => {
160
+ const nodes = definitionNodes.filter((n) => n.name.value === typeName);
161
+ const node = nodes[0];
162
+ if (!node) {
163
+ throw new Error(`You detailed '${expectedLocation}.${typeName}', but the '${typeName}' type is not defined or extended in your 'typeDefs'.`);
164
+ }
165
+ const [description, attr, names = []] = (() => {
166
+ if (node.kind === Kind.OBJECT_TYPE_EXTENSION ||
167
+ node.kind === Kind.OBJECT_TYPE_DEFINITION) {
168
+ return [
169
+ "an object type",
170
+ "objects",
171
+ nodes
172
+ .flatMap((n) => n.fields?.map((f) => f.name.value))
173
+ .filter(isNotNullish),
174
+ ];
175
+ }
176
+ else if (node.kind === Kind.INTERFACE_TYPE_EXTENSION ||
177
+ node.kind === Kind.INTERFACE_TYPE_DEFINITION) {
178
+ return [
179
+ "an interface type",
180
+ "interfaces",
181
+ nodes
182
+ .flatMap((n) => n.fields?.map((f) => f.name.value))
183
+ .filter(isNotNullish),
184
+ ];
185
+ }
186
+ else if (node.kind === Kind.UNION_TYPE_EXTENSION ||
187
+ node.kind === Kind.UNION_TYPE_DEFINITION) {
188
+ return ["a union type", "unions"];
189
+ }
190
+ else if (node.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION ||
191
+ node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
192
+ return [
193
+ "an input object type",
194
+ "inputObjects",
195
+ nodes
196
+ .flatMap((n) => n.fields?.map((f) => f.name.value))
197
+ .filter(isNotNullish),
198
+ ];
199
+ }
200
+ else if (node.kind === Kind.SCALAR_TYPE_EXTENSION ||
201
+ node.kind === Kind.SCALAR_TYPE_DEFINITION) {
202
+ return ["a scalar type", "scalars"];
203
+ }
204
+ else if (node.kind === Kind.ENUM_TYPE_EXTENSION ||
205
+ node.kind === Kind.ENUM_TYPE_DEFINITION) {
206
+ return [
207
+ "an enum type",
208
+ "enums",
209
+ nodes
210
+ .flatMap((n) => n.values?.map((f) => f.name.value))
211
+ .filter(isNotNullish),
212
+ ];
213
+ }
214
+ else {
215
+ throw new Error(`Type ${typeName} not understood`);
216
+ }
217
+ })();
218
+ if (expectedLocation !== attr) {
219
+ throw new Error(`You defined '${typeName}' under '${expectedLocation}', but it is ${description} so it should be defined under '${attr}'.`);
220
+ }
221
+ return names;
222
+ };
223
+ for (const [typeName, spec] of Object.entries(objects ?? {})) {
224
+ const fields = assertLocation(typeName, "objects");
225
+ const o = {};
226
+ plans[typeName] = o;
227
+ const { plans: planResolvers = {}, ...rest } = spec;
228
+ for (const [key, val] of Object.entries(rest)) {
229
+ assertNotDunder(`objects.${typeName}`, key);
230
+ o[`__${key}`] = val;
231
+ }
232
+ for (const [key, val] of Object.entries(planResolvers)) {
233
+ if (!fields.includes(key)) {
234
+ throw new Error(`Object type '${typeName}' field '${key}' was not defined in this plugin.`);
235
+ }
236
+ o[key] = val;
237
+ }
238
+ }
239
+ for (const [typeName, spec] of Object.entries(inputObjects ?? {})) {
240
+ const fields = assertLocation(typeName, "inputObjects");
241
+ const o = {};
242
+ plans[typeName] = o;
243
+ const { plans: planResolvers = {}, ...rest } = spec;
244
+ for (const [key, val] of Object.entries(rest)) {
245
+ assertNotDunder(`inputObjects.${typeName}`, key);
246
+ o[`__${key}`] = val;
247
+ }
248
+ for (const [key, val] of Object.entries(planResolvers)) {
249
+ if (!fields.includes(key)) {
250
+ throw new Error(`Input object type '${typeName}' field '${key}' was not defined in this plugin.`);
251
+ }
252
+ o[key] = val;
253
+ }
254
+ }
255
+ for (const [typeName, spec] of Object.entries(unions ?? {})) {
256
+ assertLocation(typeName, "unions");
257
+ const o = {};
258
+ plans[typeName] = o;
259
+ for (const [key, val] of Object.entries(spec)) {
260
+ assertNotDunder(`unions.${typeName}`, key);
261
+ o[`__${key}`] = val;
262
+ }
263
+ }
264
+ for (const [typeName, spec] of Object.entries(interfaces ?? {})) {
265
+ const fields = assertLocation(typeName, "interfaces");
266
+ const o = {};
267
+ plans[typeName] = o;
268
+ const { fields: fieldConfigs = {}, ...rest } = spec;
269
+ for (const [key, val] of Object.entries(rest)) {
270
+ assertNotDunder(`interfaces.${typeName}`, key);
271
+ o[`__${key}`] = val;
272
+ }
273
+ for (const [key, val] of Object.entries(fieldConfigs)) {
274
+ if (!fields.includes(key)) {
275
+ throw new Error(`Interface type '${typeName}' field '${key}' was not defined in this plugin.`);
276
+ }
277
+ o[key] = val;
278
+ }
279
+ }
280
+ for (const [typeName, spec] of Object.entries(scalars ?? {})) {
281
+ assertLocation(typeName, "scalars");
282
+ if (spec instanceof GraphQLScalarType) {
283
+ plans[typeName] = spec;
284
+ }
285
+ else {
286
+ const { plan, serialize, extensions, parseValue, parseLiteral, ...rest } = spec;
287
+ const o = {
288
+ plan,
289
+ serialize,
290
+ extensions,
291
+ parseValue,
292
+ parseLiteral,
293
+ };
294
+ plans[typeName] = o;
295
+ for (const [key, val] of Object.entries(rest)) {
296
+ assertNotDunder(`scalars.${typeName}`, key);
297
+ o[`__${key}`] = val;
298
+ }
299
+ }
300
+ }
301
+ for (const [typeName, spec] of Object.entries(enums ?? {})) {
302
+ const enumValues = assertLocation(typeName, "enums");
303
+ const o = {};
304
+ plans[typeName] = o;
305
+ const { values = {}, ...rest } = spec;
306
+ if ("plans" in rest) {
307
+ throw new Error(`Enum type '${typeName}' cannot have field plans, please use 'values'.`);
308
+ }
309
+ for (const [key, val] of Object.entries(rest)) {
310
+ assertNotDunder(`enums.${typeName}`, key);
311
+ o[`__${key}`] = val;
312
+ }
313
+ for (const [key, val] of Object.entries(values)) {
314
+ if (!enumValues.includes(key)) {
315
+ throw new Error(`Enum type '${typeName}' value '${key}' was not defined in this plugin.`);
316
+ }
317
+ o[key] = val;
318
+ }
319
+ }
320
+ }
120
321
  build.makeExtendSchemaPlugin[uniquePluginName] = {
121
322
  typeExtensions,
122
323
  newTypes,
@@ -130,7 +331,9 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
130
331
  const name = getName(definition.name);
131
332
  const description = getDescription(definition.description);
132
333
  const directives = getDirectives(definition.directives);
133
- const relevantResolver = plans[name] || resolvers[name] || {};
334
+ const relevantResolver = (plans[name] ??
335
+ resolvers[name] ??
336
+ {});
134
337
  const values = (definition.values ?? []).reduce((memo, value) => {
135
338
  const valueName = getName(value.name);
136
339
  const valueDescription = getDescription(value.description);
@@ -158,7 +361,12 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
158
361
  },
159
362
  };
160
363
  }, Object.create(null));
161
- const scope = scopeFromDirectives(directives);
364
+ const scope = {
365
+ __origin: `makeExtendSchemaPlugin`,
366
+ directives,
367
+ ...scopeFromDirectives(directives),
368
+ ...plans[name]?.__scope,
369
+ };
162
370
  build.registerEnumType(name, scope, () => ({ values, description }), uniquePluginName);
163
371
  }
164
372
  else if (def.type === GraphQLObjectType) {
@@ -166,19 +374,27 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
166
374
  // https://graphql.org/graphql-js/type/#graphqlobjecttype
167
375
  const name = getName(definition.name);
168
376
  const description = getDescription(definition.description);
169
- const interfaces = getInterfaces(definition.interfaces, build);
170
377
  const directives = getDirectives(definition.directives);
378
+ const p = (plans[name] ?? {});
171
379
  const scope = {
172
380
  __origin: `makeExtendSchemaPlugin`,
173
381
  directives,
174
382
  ...scopeFromDirectives(directives),
383
+ ...p.__scope,
175
384
  };
176
385
  build.registerObjectType(name, scope, () => ({
177
- interfaces,
178
- fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, resolvers, plans, fieldsContext, build),
179
- ...(description
386
+ interfaces: () => getInterfaces(definition.interfaces, build),
387
+ fields: (fieldsContext) => getFields(build, fieldsContext, fieldsContext.Self, definition.fields, resolvers, plans),
388
+ ...(description ? { description } : null),
389
+ ...(p.__isTypeOf ? { isTypeOf: p.__isTypeOf } : null),
390
+ ...(p.__assertStep || p.__planType
180
391
  ? {
181
- description,
392
+ extensions: {
393
+ grafast: {
394
+ assertStep: p.__assertStep,
395
+ planType: p.__planType,
396
+ },
397
+ },
182
398
  }
183
399
  : null),
184
400
  }), uniquePluginName);
@@ -193,14 +409,11 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
193
409
  __origin: `makeExtendSchemaPlugin`,
194
410
  directives,
195
411
  ...scopeFromDirectives(directives),
412
+ ...plans[name]?.__scope,
196
413
  };
197
414
  build.registerInputObjectType(name, scope, () => ({
198
- fields: ({ Self }) => getInputFields(Self, definition.fields, build),
199
- ...(description
200
- ? {
201
- description,
202
- }
203
- : null),
415
+ fields: (context) => getInputFields(build, context, definition.fields, plans),
416
+ ...(description ? { description } : null),
204
417
  }), uniquePluginName);
205
418
  }
206
419
  else if (def.type === GraphQLUnionType) {
@@ -209,12 +422,21 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
209
422
  const name = getName(definition.name);
210
423
  const description = getDescription(definition.description);
211
424
  const directives = getDirectives(definition.directives);
425
+ const tp = plans[name];
426
+ const tr = resolvers[name];
427
+ if (tp && tr) {
428
+ throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
429
+ }
430
+ const t = tp ?? tr;
212
431
  const scope = {
213
432
  __origin: `makeExtendSchemaPlugin`,
214
433
  directives,
215
434
  ...scopeFromDirectives(directives),
435
+ ...t?.__scope,
216
436
  };
217
- const resolveType = resolvers[name]?.__resolveType;
437
+ const resolveType = t?.__resolveType;
438
+ const toSpecifier = t?.__toSpecifier;
439
+ const planType = t?.__planType;
218
440
  build.registerUnionType(name, scope, () => ({
219
441
  types: () => {
220
442
  if (Array.isArray(definition.types)) {
@@ -230,6 +452,8 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
230
452
  }
231
453
  },
232
454
  ...(resolveType ? { resolveType } : null),
455
+ ...(toSpecifier ? { toSpecifier } : null),
456
+ ...(planType ? { planType } : null),
233
457
  ...(description ? { description } : null),
234
458
  }), uniquePluginName);
235
459
  }
@@ -239,18 +463,27 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
239
463
  const name = getName(definition.name);
240
464
  const description = getDescription(definition.description);
241
465
  const directives = getDirectives(definition.directives);
466
+ const tp = plans[name];
467
+ const tr = resolvers[name];
468
+ if (tp && tr) {
469
+ throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
470
+ }
471
+ const t = tp ?? tr;
242
472
  const scope = {
243
473
  __origin: `makeExtendSchemaPlugin`,
244
474
  directives,
245
475
  ...scopeFromDirectives(directives),
476
+ ...t?.__scope,
246
477
  };
247
- const resolveType = resolvers[name]?.__resolveType;
478
+ const resolveType = t?.__resolveType;
479
+ const toSpecifier = t?.__toSpecifier;
480
+ const planType = t?.__planType;
248
481
  build.registerInterfaceType(name, scope, () => ({
249
482
  ...(resolveType ? { resolveType } : null),
483
+ ...(toSpecifier ? { toSpecifier } : null),
484
+ ...(planType ? { planType } : null),
250
485
  ...(description ? { description } : null),
251
- fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, Object.create(null), // Interface doesn't need resolvers
252
- Object.create(null), // Interface doesn't need resolvers
253
- fieldsContext, build),
486
+ fields: (fieldsContext) => getFields(build, fieldsContext, fieldsContext.Self, definition.fields, resolvers, plans),
254
487
  ...(description
255
488
  ? {
256
489
  description,
@@ -267,34 +500,42 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
267
500
  __origin: `makeExtendSchemaPlugin`,
268
501
  directives,
269
502
  ...scopeFromDirectives(directives),
503
+ ...plans[name]?.__scope,
270
504
  };
505
+ const possiblePlan = plans[name];
506
+ const possibleResolver = resolvers[name];
507
+ if (possiblePlan && possibleResolver) {
508
+ throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
509
+ }
510
+ const rawConfig = possiblePlan ?? possibleResolver;
511
+ const config = rawConfig
512
+ ? rawConfig instanceof GraphQLScalarType
513
+ ? (0, exportable_js_1.EXPORTABLE)((rawConfig) => rawConfig.toConfig(), [rawConfig])
514
+ : rawConfig
515
+ : null;
271
516
  build.registerScalarType(name, scope, () => ({
272
517
  description,
273
- serialize: (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
274
- parseValue: (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
275
- parseLiteral: (0, exportable_js_1.EXPORTABLE)((GraphQLError, Kind, name) => (ast) => {
276
- if (ast.kind !== Kind.STRING) {
277
- throw new GraphQLError(`${name} can only parse string values`);
278
- }
279
- return ast.value;
280
- }, [GraphQLError, Kind, name]),
518
+ astNode: definition,
519
+ extensions: config?.extensions,
520
+ specifiedByURL: config?.specifiedByURL,
521
+ serialize: config?.serialize
522
+ ? (0, exportable_js_1.EXPORTABLE)((config) => config.serialize, [config])
523
+ : (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
524
+ parseValue: config?.parseValue
525
+ ? (0, exportable_js_1.EXPORTABLE)((config) => config.parseValue, [config])
526
+ : (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
527
+ parseLiteral: config?.parseLiteral
528
+ ? (0, exportable_js_1.EXPORTABLE)((config) => config.parseLiteral, [config])
529
+ : (0, exportable_js_1.EXPORTABLE)((GraphQLError, Kind, name) => (ast) => {
530
+ if (ast.kind !== Kind.STRING) {
531
+ throw new GraphQLError(`${name} can only parse string values`);
532
+ }
533
+ return ast.value;
534
+ }, [GraphQLError, Kind, name]),
281
535
  }), uniquePluginName);
282
536
  }
283
537
  else if (def.type === GraphQLDirective) {
284
- const definition = def.definition;
285
- // https://github.com/graphql/graphql-js/blob/3c54315ab13c6b9d337fb7c33ad7e27b92ca4a40/src/type/directives.js#L106-L113
286
- const name = getName(definition.name);
287
- const description = getDescription(definition.description);
288
- const locations = definition.locations.map(getName);
289
- const args = getArguments(definition.arguments, build);
290
- // Ignoring isRepeatable and astNode for now
291
- const directive = new GraphQLDirective({
292
- name,
293
- locations,
294
- args,
295
- ...(description ? { description } : null),
296
- });
297
- typeExtensions.GraphQLSchema.directives.push(directive);
538
+ typeExtensions.GraphQLSchema.directives.push(def.definition);
298
539
  }
299
540
  else {
300
541
  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.`);
@@ -303,16 +544,37 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
303
544
  return _;
304
545
  },
305
546
  GraphQLSchema(schema, build, _context) {
306
- const { makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
547
+ const { graphql: { GraphQLDirective }, inflection, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, newTypes }, }, } = build;
548
+ const newDirectives = [];
549
+ for (const definition of typeExtensions.GraphQLSchema.directives) {
550
+ // https://github.com/graphql/graphql-js/blob/3c54315ab13c6b9d337fb7c33ad7e27b92ca4a40/src/type/directives.js#L106-L113
551
+ const name = getName(definition.name);
552
+ const description = getDescription(definition.description);
553
+ const locations = definition.locations.map(getName);
554
+ const args = getArguments(definition.arguments, build);
555
+ // Ignoring isRepeatable and astNode for now
556
+ const directive = new GraphQLDirective({
557
+ name,
558
+ locations,
559
+ args,
560
+ ...(description ? { description } : null),
561
+ });
562
+ newDirectives.push(directive);
563
+ }
307
564
  return {
308
565
  ...schema,
309
566
  directives: [
310
- ...(schema.directives || build.graphql.specifiedDirectives || []),
311
- ...typeExtensions.GraphQLSchema.directives,
567
+ ...(schema.directives || build.graphql.specifiedDirectives),
568
+ ...newDirectives,
312
569
  ],
313
570
  types: [
314
571
  ...(schema.types || []),
315
- ...typeExtensions.GraphQLSchema.types,
572
+ ...[
573
+ build.getTypeByName(inflection.builtin("Query")),
574
+ build.getTypeByName(inflection.builtin("Mutation")),
575
+ build.getTypeByName(inflection.builtin("Subscription")),
576
+ ...newTypes.map(({ definition }) => build.getTypeByName(definition.name.value)),
577
+ ].filter((_) => _ != null),
316
578
  ],
317
579
  };
318
580
  },
@@ -321,7 +583,7 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
321
583
  const { Self } = context;
322
584
  if (typeExtensions.GraphQLObjectType[Self.name]) {
323
585
  const newFields = typeExtensions.GraphQLObjectType[Self.name].reduce((memo, extension) => {
324
- const moreFields = getFields(Self, extension.fields, resolvers, plans, context, build);
586
+ const moreFields = getFields(build, context, Self, extension.fields, resolvers, plans);
325
587
  return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
326
588
  }, Object.create(null));
327
589
  return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
@@ -345,11 +607,11 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
345
607
  }
346
608
  },
347
609
  GraphQLInputObjectType_fields(fields, build, context) {
348
- const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
610
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, plans }, }, } = build;
349
611
  const { Self } = context;
350
612
  if (typeExtensions.GraphQLInputObjectType[Self.name]) {
351
613
  const newFields = typeExtensions.GraphQLInputObjectType[Self.name].reduce((memo, extension) => {
352
- const moreFields = getInputFields(Self, extension.fields, build);
614
+ const moreFields = getInputFields(build, context, extension.fields, plans);
353
615
  return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
354
616
  }, Object.create(null));
355
617
  return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
@@ -359,13 +621,11 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
359
621
  }
360
622
  },
361
623
  GraphQLInterfaceType_fields(fields, build, context) {
362
- const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions }, }, } = build;
624
+ const { extend, makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, resolvers, plans }, }, } = build;
363
625
  const { Self } = context;
364
626
  if (typeExtensions.GraphQLInterfaceType[Self.name]) {
365
627
  const newFields = typeExtensions.GraphQLInterfaceType[Self.name].reduce((memo, extension) => {
366
- const moreFields = getFields(Self, extension.fields, Object.create(null), // No resolvers for interfaces
367
- Object.create(null), // No resolvers for interfaces
368
- context, build);
628
+ const moreFields = getFields(build, context, Self, extension.fields, resolvers, plans);
369
629
  return extend(memo, moreFields, `Adding fields from ${uniquePluginName}`);
370
630
  }, Object.create(null));
371
631
  return extend(fields, newFields, `Adding fields from ${uniquePluginName}`);
@@ -412,7 +672,7 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
412
672
  if (type.kind === "NamedType") {
413
673
  const Type = build.getTypeByName(getName(type.name));
414
674
  if (!Type) {
415
- throw new Error(`Could not find type named '${getName(type.name)}'.`);
675
+ throw new Error(`${uniquePluginName} could not find type named '${getName(type.name)}'.`);
416
676
  }
417
677
  return Type;
418
678
  }
@@ -507,8 +767,12 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
507
767
  if (arg.defaultValue) {
508
768
  defaultValue = getValue(arg.defaultValue, type);
509
769
  }
770
+ const argDirectives = getDirectives(arg.directives);
771
+ const deprecatedDirective = argDirectives.find((d) => d.directiveName === "deprecated");
772
+ const deprecationReason = deprecatedDirective?.args.reason;
510
773
  memo[name] = {
511
774
  type,
775
+ deprecationReason,
512
776
  ...(defaultValue != null ? { defaultValue } : null),
513
777
  ...(description ? { description } : null),
514
778
  };
@@ -521,7 +785,8 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
521
785
  }
522
786
  return {};
523
787
  }
524
- function getFields(SelfGeneric, fields, resolvers, plans, context, build) {
788
+ function getFields(build, context, SelfGeneric, fields, resolvers, plans) {
789
+ const { graphql: { getNullableType, getNamedType }, } = build;
525
790
  const { fieldWithHooks } = context;
526
791
  const isRootSubscription = "isRootSubscription" in context.scope && context.scope.isRootSubscription;
527
792
  if (!build.graphql.isNamedType(SelfGeneric)) {
@@ -536,25 +801,6 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
536
801
  const args = getArguments(field.arguments, build);
537
802
  const type = getType(field.type, build);
538
803
  const directives = getDirectives(field.directives);
539
- const scope = {
540
- fieldName,
541
- /*
542
- ...(typeScope.pgIntrospection &&
543
- typeScope.pgIntrospection.kind === "class"
544
- ? {
545
- pgFieldIntrospection: typeScope.pgIntrospection,
546
- }
547
- : null),
548
- ...(typeScope.isPgRowConnectionType && typeScope.pgIntrospection
549
- ? {
550
- isPgFieldConnection: true,
551
- pgFieldIntrospection: typeScope.pgIntrospection,
552
- }
553
- : null),
554
- */
555
- fieldDirectives: directives,
556
- ...scopeFromDirectives(directives),
557
- };
558
804
  const deprecatedDirective = directives.find((d) => d.directiveName === "deprecated");
559
805
  const deprecationReason = deprecatedDirective?.args.reason;
560
806
  /*
@@ -563,24 +809,18 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
563
809
  * other relevant methods.
564
810
  */
565
811
  const possiblePlan = plans[Self.name]?.[fieldName];
812
+ build.exportNameHint(possiblePlan, `${Self.name}_${fieldName}_plan`);
566
813
  const possibleResolver = resolvers[Self.name]?.[fieldName];
814
+ build.exportNameHint(possibleResolver, `${Self.name}_${fieldName}_resolver`);
567
815
  if (possiblePlan && possibleResolver) {
568
816
  throw new Error(`You must set only plans.${Self.name}.${fieldName} or resolvers.${Self.name}.${fieldName} - not both!`);
569
817
  }
570
818
  const spec = possiblePlan ?? possibleResolver;
571
819
  const fieldSpecGenerator = () => {
572
820
  return {
573
- ...(deprecationReason
574
- ? {
575
- deprecationReason,
576
- }
577
- : null),
578
- ...(description
579
- ? {
580
- description,
581
- }
582
- : null),
583
- ...(typeof spec === "function"
821
+ ...(deprecationReason ? { deprecationReason } : null),
822
+ ...(description ? { description } : null),
823
+ ...(build.graphql.isObjectType(Self) && typeof spec === "function"
584
824
  ? {
585
825
  [possiblePlan
586
826
  ? isRootSubscription
@@ -595,6 +835,42 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
595
835
  args,
596
836
  };
597
837
  };
838
+ const nullableType = getNullableType(type);
839
+ const namedType = getNamedType(type);
840
+ const typeScope = build.scopeByType.get(namedType);
841
+ const scope = {
842
+ fieldDirectives: directives,
843
+ // Guess a codec and resource
844
+ ...(typeScope && "pgCodec" in typeScope && typeScope.pgCodec
845
+ ? {
846
+ pgFieldCodec: typeScope.pgCodec,
847
+ // First guess at a resource; may be overwritten
848
+ pgFieldResource: Object.values(build.input.pgRegistry.pgResources).find((r) => r.codec === typeScope.pgCodec &&
849
+ !r.isUnique &&
850
+ !r.parameters &&
851
+ !r.isVirtual &&
852
+ !r.isList &&
853
+ !r.codec.polymorphism),
854
+ }
855
+ : null),
856
+ // Guess (more accurately) a resource
857
+ ...(typeScope &&
858
+ "pgTypeResource" in typeScope &&
859
+ typeScope.pgTypeResource
860
+ ? { pgFieldResource: typeScope.pgTypeResource }
861
+ : null),
862
+ // Guess if it's a connection
863
+ ...(nullableType === namedType &&
864
+ typeScope &&
865
+ "isConnectionType" in typeScope
866
+ ? { isPgFieldConnection: typeScope.isConnectionType }
867
+ : null),
868
+ // Allow user to overwrite
869
+ ...scopeFromDirectives(directives),
870
+ ...(typeof spec === "object" && spec !== null ? spec.scope : null),
871
+ // fieldName always wins
872
+ fieldName,
873
+ };
598
874
  return build.extend(memo, {
599
875
  [fieldName]: fieldWithHooks(scope, fieldSpecGenerator),
600
876
  }, `Adding '${fieldName}' to '${Self.name}' from '${uniquePluginName}'`);
@@ -606,25 +882,37 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
606
882
  }
607
883
  return {};
608
884
  }
609
- function getInputFields(_Self, fields, build) {
885
+ function getInputFields(build, context, fields, plans) {
886
+ const { Self } = context;
610
887
  if (fields && fields.length) {
611
888
  return fields.reduce((memo, field) => {
612
889
  if (field.kind === "InputValueDefinition") {
613
890
  const description = getDescription(field.description);
614
891
  const fieldName = getName(field.name);
615
892
  const type = getType(field.type, build);
893
+ const directives = getDirectives(field.directives);
894
+ if (!build.graphql.isInputType(type)) {
895
+ throw new Error(`${Self.name}.${fieldName} must use an input type`);
896
+ }
616
897
  const defaultValue = field.defaultValue
617
898
  ? getValue(field.defaultValue, type)
618
899
  : undefined;
619
- memo[fieldName] = {
620
- type,
621
- defaultValue,
622
- ...(description
623
- ? {
624
- description,
625
- }
900
+ const spec = plans[Self.name]?.[fieldName];
901
+ const scope = {
902
+ fieldDirectives: directives,
903
+ // Allow user to overwrite
904
+ ...scopeFromDirectives(directives),
905
+ ...(typeof spec === "object" && spec !== null
906
+ ? spec.scope
626
907
  : null),
908
+ // fieldName always wins
909
+ fieldName,
627
910
  };
911
+ memo[fieldName] = context.fieldWithHooks(scope, {
912
+ type,
913
+ defaultValue,
914
+ ...(description ? { description } : null),
915
+ });
628
916
  }
629
917
  else {
630
918
  throw new Error(`AST issue: expected 'FieldDefinition', instead received '${field.kind}'`);
@@ -635,7 +923,6 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
635
923
  return {};
636
924
  }
637
925
  }
638
- exports.makeExtendSchemaPlugin = makeExtendSchemaPlugin;
639
926
  function scopeFromDirectives(directives) {
640
927
  return {
641
928
  ...directives
@@ -644,4 +931,14 @@ function scopeFromDirectives(directives) {
644
931
  .reduce((memo, a) => Object.assign(memo, a), Object.create(null)),
645
932
  };
646
933
  }
934
+ function isNotNullish(v) {
935
+ return v != null;
936
+ }
937
+ /** @deprecated Renamed to 'extendSchema' */
938
+ exports.makeExtendSchemaPlugin = extendSchema;
939
+ function assertNotDunder(loc, key) {
940
+ if (key.startsWith("__")) {
941
+ throw new Error(`Key ${JSON.stringify(key)} defined at location ${loc} shouldn't start with __`);
942
+ }
943
+ }
647
944
  //# sourceMappingURL=makeExtendSchemaPlugin.js.map