graphile-utils 4.12.2 → 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 +33 -24
  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
@@ -1,703 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fieldHelpers_1 = require("./fieldHelpers");
4
- // TODO:v5: Remove
5
- const recurseDataGeneratorsWorkaroundFieldByType = new Map();
6
- function makeExtendSchemaPlugin(generator, uniqueId = String(Math.random()).substr(2)) {
7
- let graphql;
8
- return (builder, schemaOptions) => {
9
- // Add stuff to the schema
10
- builder.hook("build", build => {
11
- // Extract GraphQL into the scope so that our other functions can use it.
12
- graphql = build.graphql;
13
- const { addType } = build;
14
- const { GraphQLEnumType, GraphQLInputObjectType, GraphQLObjectType, GraphQLScalarType, GraphQLDirective, GraphQLUnionType, GraphQLInterfaceType, } = graphql;
15
- const { typeDefs, resolvers = {} } = typeof generator === "function"
16
- ? generator(build, schemaOptions)
17
- : generator;
18
- const typeDefsArr = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
19
- const mergedTypeDefinitions = typeDefsArr.reduce((definitions, typeDef) => {
20
- if (!typeDef || typeDef.kind !== "Document") {
21
- throw new Error("The first argument to makeExtendSchemaPlugin must be generated by the `gql` helper, or be an array of the same.");
22
- }
23
- definitions.push(...typeDef.definitions);
24
- return definitions;
25
- }, []);
26
- const typeExtensions = {
27
- GraphQLSchema: {
28
- directives: [],
29
- types: [],
30
- },
31
- GraphQLInputObjectType: {},
32
- GraphQLObjectType: {},
33
- GraphQLInterfaceType: {},
34
- };
35
- const newTypes = [];
36
- mergedTypeDefinitions.forEach(definition => {
37
- if (definition.kind === "EnumTypeDefinition") {
38
- newTypes.push({
39
- type: GraphQLEnumType,
40
- definition,
41
- });
42
- }
43
- else if (definition.kind === "ObjectTypeExtension") {
44
- const name = getName(definition.name);
45
- if (!typeExtensions.GraphQLObjectType[name]) {
46
- typeExtensions.GraphQLObjectType[name] = [];
47
- }
48
- typeExtensions.GraphQLObjectType[name].push(definition);
49
- }
50
- else if (definition.kind === "InputObjectTypeExtension") {
51
- const name = getName(definition.name);
52
- if (!typeExtensions.GraphQLInputObjectType[name]) {
53
- typeExtensions.GraphQLInputObjectType[name] = [];
54
- }
55
- typeExtensions.GraphQLInputObjectType[name].push(definition);
56
- }
57
- else if (definition.kind === "InterfaceTypeExtension") {
58
- const name = getName(definition.name);
59
- if (!typeExtensions.GraphQLInterfaceType[name]) {
60
- typeExtensions.GraphQLInterfaceType[name] = [];
61
- }
62
- typeExtensions.GraphQLInterfaceType[name].push(definition);
63
- }
64
- else if (definition.kind === "ObjectTypeDefinition") {
65
- newTypes.push({
66
- type: GraphQLObjectType,
67
- definition,
68
- });
69
- }
70
- else if (definition.kind === "InputObjectTypeDefinition") {
71
- newTypes.push({
72
- type: GraphQLInputObjectType,
73
- definition,
74
- });
75
- }
76
- else if (definition.kind === "UnionTypeDefinition") {
77
- newTypes.push({
78
- type: GraphQLUnionType,
79
- definition,
80
- });
81
- }
82
- else if (definition.kind === "InterfaceTypeDefinition") {
83
- newTypes.push({
84
- type: GraphQLInterfaceType,
85
- definition,
86
- });
87
- }
88
- else if (definition.kind === "DirectiveDefinition") {
89
- newTypes.push({
90
- type: GraphQLDirective,
91
- definition,
92
- });
93
- }
94
- else if (definition.kind === "ScalarTypeDefinition") {
95
- // TODO: add validation
96
- const name = getName(definition.name);
97
- if (resolvers[name]) {
98
- const gqlScalarType = resolvers[name];
99
- if (gqlScalarType.name !== name) {
100
- throw new Error(`Names for scalar type do not match; schema: '${name}' vs type: '${String(gqlScalarType.name)}'`);
101
- }
102
- addType(gqlScalarType);
103
- }
104
- else {
105
- // Create a string type
106
- newTypes.push({
107
- type: GraphQLScalarType,
108
- definition,
109
- });
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
- return build.extend(build, {
120
- [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions,
121
- [`ExtendSchemaPlugin_${uniqueId}_newTypes`]: newTypes,
122
- [`ExtendSchemaPlugin_${uniqueId}_resolvers`]: resolvers,
123
- });
124
- });
125
- builder.hook("init", (_, build, _context) => {
126
- const { newWithHooks, [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions, // ONLY use this for GraphQLSchema in this hook
127
- [`ExtendSchemaPlugin_${uniqueId}_newTypes`]: newTypes, [`ExtendSchemaPlugin_${uniqueId}_resolvers`]: resolvers, graphql: { GraphQLEnumType, GraphQLObjectType, GraphQLInputObjectType, GraphQLUnionType, GraphQLInterfaceType, GraphQLScalarType, GraphQLDirective, Kind, }, } = build;
128
- newTypes.forEach(({ type, definition }) => {
129
- if (type === GraphQLEnumType) {
130
- // https://graphql.org/graphql-js/type/#graphqlenumtype
131
- const name = getName(definition.name);
132
- const description = getDescription(definition.description);
133
- const directives = getDirectives(definition.directives);
134
- const relevantResolver = resolvers[name] || {};
135
- const values = definition.values.reduce((memo, value) => {
136
- const valueName = getName(value.name);
137
- const valueDescription = getDescription(value.description);
138
- const valueDirectives = getDirectives(value.directives);
139
- // Value cannot be expressed via SDL, so we grab the value from the resolvers instead.
140
- // resolvers = {
141
- // MyEnum: {
142
- // MY_ENUM_VALUE1: 'value1',
143
- // MY_ENUM_VALUE2: 'value2',
144
- // }
145
- // }
146
- // Ref: https://github.com/graphql/graphql-js/issues/525#issuecomment-255834625
147
- const valueValue = relevantResolver[valueName] !== undefined
148
- ? relevantResolver[valueName]
149
- : valueName;
150
- const valueDeprecationReason = valueDirectives.deprecated && valueDirectives.deprecated.reason;
151
- return Object.assign(Object.assign({}, memo), { [valueName]: {
152
- value: valueValue,
153
- deprecationReason: valueDeprecationReason,
154
- description: valueDescription,
155
- directives: valueDirectives,
156
- } });
157
- }, {});
158
- const scope = Object.assign({ directives }, (directives.scope || {}));
159
- newWithHooks(type, { name, values, description }, scope);
160
- }
161
- else if (type === GraphQLObjectType) {
162
- // https://graphql.org/graphql-js/type/#graphqlobjecttype
163
- const name = getName(definition.name);
164
- const description = getDescription(definition.description);
165
- const interfaces = getInterfaces(definition.interfaces, build);
166
- const directives = getDirectives(definition.directives);
167
- const scope = Object.assign({ __origin: `makeExtendSchemaPlugin`, directives }, (directives.scope || {}));
168
- newWithHooks(type, Object.assign({ name,
169
- interfaces, fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, resolvers, fieldsContext, build) }, (description
170
- ? {
171
- description,
172
- }
173
- : null)), scope);
174
- }
175
- else if (type === GraphQLInputObjectType) {
176
- // https://graphql.org/graphql-js/type/#graphqlinputobjecttype
177
- const name = getName(definition.name);
178
- const description = getDescription(definition.description);
179
- const directives = getDirectives(definition.directives);
180
- const scope = Object.assign({ __origin: `makeExtendSchemaPlugin`, directives }, (directives.scope || {}));
181
- newWithHooks(type, Object.assign({ name, fields: ({ Self }) => getInputFields(Self, definition.fields, build) }, (description
182
- ? {
183
- description,
184
- }
185
- : null)), scope);
186
- }
187
- else if (type === GraphQLUnionType) {
188
- // https://graphql.org/graphql-js/type/#graphqluniontype
189
- const name = getName(definition.name);
190
- const description = getDescription(definition.description);
191
- const directives = getDirectives(definition.directives);
192
- const scope = Object.assign({ __origin: `makeExtendSchemaPlugin`, directives }, (directives.scope || {}));
193
- const resolveType = resolvers[name] && resolvers[name].__resolveType;
194
- newWithHooks(type, Object.assign(Object.assign({ name, types: () => {
195
- if (Array.isArray(definition.types)) {
196
- return definition.types.map((typeAST) => {
197
- if (typeAST.kind !== "NamedType") {
198
- throw new Error("Only support unions of named types");
199
- }
200
- return getType(typeAST, build);
201
- });
202
- }
203
- } }, (resolveType ? { resolveType } : null)), (description ? { description } : null)), scope);
204
- }
205
- else if (type === GraphQLInterfaceType) {
206
- // https://graphql.org/graphql-js/type/#graphqluniontype
207
- const name = getName(definition.name);
208
- const description = getDescription(definition.description);
209
- const directives = getDirectives(definition.directives);
210
- const scope = Object.assign({ __origin: `makeExtendSchemaPlugin`, directives }, (directives.scope || {}));
211
- const resolveType = resolvers[name] && resolvers[name].__resolveType;
212
- newWithHooks(type, Object.assign(Object.assign(Object.assign(Object.assign({ name }, (resolveType ? { resolveType } : null)), (description ? { description } : null)), { fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, {}, // Interface doesn't need resolvers
213
- fieldsContext, build) }), (description
214
- ? {
215
- description,
216
- }
217
- : null)), scope);
218
- }
219
- else if (type === GraphQLScalarType) {
220
- const name = getName(definition.name);
221
- const description = getDescription(definition.description);
222
- const directives = getDirectives(definition.directives);
223
- const scope = Object.assign({ __origin: `makeExtendSchemaPlugin`, directives }, (directives.scope || {}));
224
- newWithHooks(type, {
225
- name,
226
- description,
227
- serialize: (value) => String(value),
228
- parseValue: (value) => String(value),
229
- parseLiteral: (ast) => {
230
- if (ast.kind !== Kind.STRING) {
231
- throw new Error("Can only parse string values");
232
- }
233
- return ast.value;
234
- },
235
- }, scope);
236
- }
237
- else if (type === GraphQLDirective) {
238
- // https://github.com/graphql/graphql-js/blob/3c54315ab13c6b9d337fb7c33ad7e27b92ca4a40/src/type/directives.js#L106-L113
239
- const name = getName(definition.name);
240
- const description = getDescription(definition.description);
241
- const locations = definition.locations.map(getName);
242
- const args = getArguments(definition.arguments, build);
243
- // Ignoring isRepeatable and astNode for now
244
- const directive = new GraphQLDirective(Object.assign({ name,
245
- locations,
246
- args }, (description ? { description } : null)));
247
- typeExtensions.GraphQLSchema.directives.push(directive);
248
- }
249
- else {
250
- throw new Error(`We have no code to build an object of type '${type}'; it should not have reached this area of the code.`);
251
- }
252
- });
253
- return _;
254
- });
255
- builder.hook("GraphQLSchema", (schema, build, _context) => {
256
- const { [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions, } = build;
257
- return Object.assign(Object.assign({}, schema), { directives: [
258
- ...(schema.directives || build.graphql.specifiedDirectives || []),
259
- ...typeExtensions.GraphQLSchema.directives,
260
- ], types: [...(schema.types || []), ...typeExtensions.GraphQLSchema.types] });
261
- });
262
- builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
263
- const { extend, [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions, [`ExtendSchemaPlugin_${uniqueId}_resolvers`]: resolvers, } = build;
264
- const { Self } = context;
265
- if (typeExtensions.GraphQLObjectType[Self.name]) {
266
- const newFields = typeExtensions.GraphQLObjectType[Self.name].reduce((memo, extension) => {
267
- const moreFields = getFields(Self, extension.fields, resolvers, context, build);
268
- return extend(memo, moreFields);
269
- }, {});
270
- return extend(fields, newFields);
271
- }
272
- else {
273
- return fields;
274
- }
275
- });
276
- builder.hook("GraphQLInputObjectType:fields", (fields, build, context) => {
277
- const { extend, [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions, } = build;
278
- const { Self } = context;
279
- if (typeExtensions.GraphQLInputObjectType[Self.name]) {
280
- const newFields = typeExtensions.GraphQLInputObjectType[Self.name].reduce((memo, extension) => {
281
- const moreFields = getInputFields(Self, extension.fields, build);
282
- return extend(memo, moreFields);
283
- }, {});
284
- return extend(fields, newFields);
285
- }
286
- else {
287
- return fields;
288
- }
289
- });
290
- try {
291
- builder.hook("GraphQLInterfaceType:fields", (fields, build, context) => {
292
- const { extend, [`ExtendSchemaPlugin_${uniqueId}_typeExtensions`]: typeExtensions, } = build;
293
- const { Self } = context;
294
- if (typeExtensions.GraphQLInterfaceType[Self.name]) {
295
- const newFields = typeExtensions.GraphQLInterfaceType[Self.name].reduce((memo, extension) => {
296
- const moreFields = getFields(Self, extension.fields, {}, // No resolvers for interfaces
297
- context, build);
298
- return extend(memo, moreFields);
299
- }, {});
300
- return extend(fields, newFields);
301
- }
302
- else {
303
- return fields;
304
- }
305
- });
306
- }
307
- catch (e) {
308
- console.warn("Please update your version of PostGraphile/Graphile Engine, the version you're using does not support the GraphQLInterfaceType:fields hook");
309
- }
310
- };
311
- function getName(name) {
312
- if (name && name.kind === "Name" && name.value) {
313
- return name.value;
314
- }
315
- throw new Error("Could not extract name from AST");
316
- }
317
- function getDescription(desc) {
318
- if (!desc) {
319
- return null;
320
- }
321
- else if (desc.kind === "StringValue") {
322
- return desc.value;
323
- }
324
- else {
325
- throw new Error(`AST issue, we weren't expecting a description of kind '${desc.kind}' - PRs welcome!`);
326
- }
327
- }
328
- function getType(type, build) {
329
- if (type.kind === "NamedType") {
330
- const Type = build.getTypeByName(getName(type.name));
331
- if (!Type) {
332
- throw new Error(`Could not find type named '${getName(type.name)}'.`);
333
- }
334
- return Type;
335
- }
336
- else if (type.kind === "NonNullType") {
337
- return new build.graphql.GraphQLNonNull(getType(type.type, build));
338
- }
339
- else if (type.kind === "ListType") {
340
- return new build.graphql.GraphQLList(getType(type.type, build));
341
- }
342
- else {
343
- throw new Error(`We don't support AST type definition of kind '${type.kind}' yet... PRs welcome!`);
344
- }
345
- }
346
- function getInterfaces(interfaces, build) {
347
- return interfaces.map(i => build.getTypeByName(i.name.value));
348
- }
349
- function getValue(value, inType) {
350
- const type = inType && graphql.isNonNullType(inType) ? inType.ofType : inType;
351
- if (value.kind === "BooleanValue") {
352
- return !!value.value;
353
- }
354
- else if (value.kind === "StringValue") {
355
- return value.value;
356
- }
357
- else if (value.kind === "IntValue") {
358
- return parseInt(value.value, 10);
359
- }
360
- else if (value.kind === "FloatValue") {
361
- return parseFloat(value.value);
362
- }
363
- else if (value.kind === "EnumValue") {
364
- if (!type) {
365
- throw new Error("We do not support EnumValue arguments in directives at this time");
366
- }
367
- const enumValueName = value.value;
368
- const enumType = graphql.isEnumType(type)
369
- ? type
370
- : null;
371
- if (!enumType) {
372
- throw new Error(`Tried to interpret an EnumValue for non-enum type ${type}`);
373
- }
374
- const values = enumType.getValues();
375
- const enumValue = values.find(v => v.name === enumValueName);
376
- return enumValue ? enumValue.value : undefined;
377
- }
378
- else if (value.kind === "NullValue") {
379
- return null;
380
- }
381
- else if (value.kind === "ListValue") {
382
- // This is used in directives, so we cannot assume the type is known.
383
- const childType = type && graphql.isListType(type) ? type.ofType : null;
384
- return value.values.map(value => getValue(value, childType));
385
- }
386
- else if (value.kind === "GraphileEmbed") {
387
- // RAW!
388
- return value.value;
389
- }
390
- else {
391
- throw new Error(`Value kind '${value.kind}' not supported yet. PRs welcome!`);
392
- }
393
- }
394
- function getDirectives(directives) {
395
- return (directives || []).reduce((directivesList, directive) => {
396
- if (directive.kind === "Directive") {
397
- const name = getName(directive.name);
398
- const value = (directive.arguments || []).reduce((argumentValues, arg) => {
399
- if (arg.kind === "Argument") {
400
- const argName = getName(arg.name);
401
- const argValue = getValue(arg.value);
402
- if (argumentValues[name]) {
403
- throw new Error(`Argument '${argName}' of directive '${name}' must only be used once.`);
404
- }
405
- argumentValues[argName] = argValue;
406
- }
407
- else {
408
- throw new Error(`Unexpected '${arg.kind}', we were expecting 'Argument'`);
409
- }
410
- return argumentValues;
411
- }, {});
412
- if (directivesList[name]) {
413
- throw new Error(`Directive '${name}' must only be used once per field.`);
414
- }
415
- directivesList[name] = value;
416
- }
417
- else {
418
- throw new Error(`Unexpected '${directive.kind}', we were expecting 'Directive'`);
419
- }
420
- return directivesList;
421
- }, {});
422
- }
423
- function getArguments(args, build) {
424
- if (args && args.length) {
425
- return args.reduce((memo, arg) => {
426
- if (arg.kind === "InputValueDefinition") {
427
- const name = getName(arg.name);
428
- const type = getType(arg.type, build);
429
- const description = getDescription(arg.description);
430
- let defaultValue;
431
- if (arg.defaultValue) {
432
- defaultValue = getValue(arg.defaultValue, type);
433
- }
434
- memo[name] = Object.assign(Object.assign({ type }, (defaultValue != null ? { defaultValue } : null)), (description ? { description } : null));
435
- }
436
- else {
437
- throw new Error(`Unexpected '${arg.kind}', we were expecting an 'InputValueDefinition'`);
438
- }
439
- return memo;
440
- }, {});
441
- }
442
- return {};
443
- }
444
- function getFields(SelfGeneric, fields, resolvers, { fieldWithHooks, }, build) {
445
- const scopeByType = build.scopeByType || new Map();
446
- if (!build.graphql.isNamedType(SelfGeneric)) {
447
- throw new Error("getFields only supports named types");
448
- }
449
- const Self = SelfGeneric;
450
- const { pgSql: sql, graphql: { isScalarType, getNamedType }, } = build;
451
- function augmentResolver(resolver, fieldContext, type) {
452
- let got = false;
453
- let val;
454
- const getRecurseDataGeneratorsWorkaroundField = () => {
455
- if (!got) {
456
- got = true;
457
- const namedType = build.graphql.getNamedType(type);
458
- val = recurseDataGeneratorsWorkaroundFieldByType.get(namedType);
459
- }
460
- return val;
461
- };
462
- const newResolver = async (parent, args, context, resolveInfo) => {
463
- const graphileHelpers = (0, fieldHelpers_1.makeFieldHelpers)(build, fieldContext, context, resolveInfo);
464
- const result = await resolver(parent, args, context, Object.assign(Object.assign({}, resolveInfo), { graphile: graphileHelpers }), graphileHelpers);
465
- const recurseDataGeneratorsWorkaroundField = getRecurseDataGeneratorsWorkaroundField();
466
- if (result != null &&
467
- !result.data &&
468
- recurseDataGeneratorsWorkaroundField) {
469
- return Object.assign(Object.assign({}, result), { data: result[recurseDataGeneratorsWorkaroundField] });
470
- }
471
- return result;
472
- };
473
- return newResolver;
474
- }
475
- if (fields && fields.length) {
476
- return fields.reduce((memo, field) => {
477
- if (field.kind === "FieldDefinition") {
478
- const description = getDescription(field.description);
479
- const fieldName = getName(field.name);
480
- const args = getArguments(field.arguments, build);
481
- const type = getType(field.type, build);
482
- const nullableType = build.graphql.getNullableType(type);
483
- const namedType = build.graphql.getNamedType(type);
484
- const typeScope = scopeByType.get(namedType) || {};
485
- const directives = getDirectives(field.directives);
486
- const scope = Object.assign(Object.assign(Object.assign(Object.assign({}, (typeScope.pgIntrospection &&
487
- typeScope.pgIntrospection.kind === "class"
488
- ? {
489
- pgFieldIntrospection: typeScope.pgIntrospection,
490
- }
491
- : null)), (typeScope.isPgRowConnectionType && typeScope.pgIntrospection
492
- ? {
493
- isPgFieldConnection: true,
494
- pgFieldIntrospection: typeScope.pgIntrospection,
495
- }
496
- : null)), { fieldDirectives: directives }), (directives.scope || {}));
497
- const deprecationReason = directives.deprecated && directives.deprecated.reason;
498
- const functionToResolveObject = (functionOrResolveObject) => typeof functionOrResolveObject === "function"
499
- ? { resolve: functionOrResolveObject }
500
- : functionOrResolveObject;
501
- const isConnection = !!scope.isPgFieldConnection;
502
- const isListType = nullableType !== namedType &&
503
- nullableType.constructor === build.graphql.GraphQLList;
504
- const table = scope.pgFieldIntrospection &&
505
- scope.pgFieldIntrospection.kind === "class"
506
- ? scope.pgFieldIntrospection
507
- : null;
508
- const isScalar = isScalarType(getNamedType(type));
509
- const generateImplicitResolverIfPossible = () => {
510
- if (directives.pgQuery &&
511
- ((table && directives.pgQuery.source) ||
512
- (isScalar && directives.pgQuery.fragment))) {
513
- return (data, _args, _resolveContext, resolveInfo) => {
514
- const safeAlias = build.getSafeAliasFromResolveInfo(resolveInfo);
515
- const liveRecord = resolveInfo.rootValue && resolveInfo.rootValue.liveRecord;
516
- if (isConnection) {
517
- return build.pgAddStartEndCursor(data[safeAlias]);
518
- }
519
- else if (isListType) {
520
- const records = data[safeAlias];
521
- if (table && liveRecord) {
522
- records.forEach((r) => r && liveRecord("pg", table, r.__identifiers));
523
- }
524
- return records;
525
- }
526
- else {
527
- const record = data[safeAlias];
528
- if (record && liveRecord && table) {
529
- liveRecord("pg", table, record.__identifiers);
530
- }
531
- return record;
532
- }
533
- };
534
- }
535
- return null;
536
- };
537
- /*
538
- * We accept a resolver function directly, or an object which can
539
- * define 'resolve', 'subscribe' and other relevant methods.
540
- */
541
- const possibleResolver = resolvers[Self.name]
542
- ? resolvers[Self.name][fieldName]
543
- : null;
544
- const resolver = possibleResolver &&
545
- (typeof possibleResolver === "object" ||
546
- typeof possibleResolver === "function")
547
- ? possibleResolver
548
- : generateImplicitResolverIfPossible();
549
- const rawResolversSpec = resolver
550
- ? functionToResolveObject(resolver)
551
- : null;
552
- if (directives.recurseDataGenerators) {
553
- if (!recurseDataGeneratorsWorkaroundFieldByType.get(Self)) {
554
- recurseDataGeneratorsWorkaroundFieldByType.set(Self, fieldName);
555
- }
556
- // eslint-disable-next-line no-console
557
- console.warn("DEPRECATION: `recurseDataGenerators` is misleading, please use `pgField` instead");
558
- if (!directives.pgField) {
559
- directives.pgField = directives.recurseDataGenerators;
560
- }
561
- }
562
- const fieldSpecGenerator = (fieldContext) => {
563
- const { pgIntrospection } = fieldContext.scope;
564
- // @requires directive: pulls down necessary columns from table.
565
- //
566
- // e.g. `@requires(columns: ["id", "name"])`
567
- //
568
- if (directives.requires && pgIntrospection.kind === "class") {
569
- const table = pgIntrospection;
570
- if (Array.isArray(directives.requires.columns)) {
571
- const attrs = table.attributes.filter(attr => directives.requires.columns.indexOf(attr.name) >= 0);
572
- const fieldNames = attrs.map(attr => build.inflection.column(attr));
573
- const ReturnTypes = attrs.map(attr => build.pgGetGqlTypeByTypeIdAndModifier(attr.typeId, attr.typeModifier) || build.graphql.GraphQLString);
574
- fieldContext.addDataGenerator((parsedResolveInfoFragment) => ({
575
- pgQuery: (queryBuilder) => {
576
- attrs.forEach((attr, i) => {
577
- const columnFieldName = fieldNames[i];
578
- const ReturnType = ReturnTypes[i];
579
- queryBuilder.select(build.pgGetSelectValueForFieldAndTypeAndModifier(ReturnType, fieldContext, parsedResolveInfoFragment, sql.fragment `(${queryBuilder.getTableAlias()}.${sql.identifier(attr.name)})`, // The brackets are necessary to stop the parser getting confused, ref: https://www.postgresql.org/docs/9.6/static/rowtypes.html#ROWTYPES-ACCESSING
580
- attr.type, attr.typeModifier), columnFieldName);
581
- });
582
- },
583
- }));
584
- }
585
- else {
586
- throw new Error(`@requires(columns: ["...", ...]) directive called with invalid arguments`);
587
- }
588
- }
589
- if (directives.pgQuery) {
590
- if (table && directives.pgQuery.source) {
591
- fieldContext.addDataGenerator((parsedResolveInfoFragment) => {
592
- return {
593
- pgQuery: (queryBuilder) => {
594
- const source = typeof directives.pgQuery.source === "function"
595
- ? directives.pgQuery.source(queryBuilder, parsedResolveInfoFragment.args)
596
- : directives.pgQuery.source;
597
- queryBuilder.select(() => {
598
- const resolveData = fieldContext.getDataFromParsedResolveInfoFragment(parsedResolveInfoFragment, namedType);
599
- const tableAlias = sql.identifier(Symbol());
600
- const query = build.pgQueryFromResolveData(source, tableAlias, resolveData, {
601
- withPagination: isConnection,
602
- withPaginationAsFields: false,
603
- asJsonAggregate: isListType && !isConnection,
604
- asJson: !isConnection,
605
- addNullCase: !isConnection,
606
- }, (innerQueryBuilder) => {
607
- innerQueryBuilder.parentQueryBuilder =
608
- queryBuilder;
609
- if (build.options.subscriptions &&
610
- table.primaryKeyConstraint) {
611
- innerQueryBuilder.selectIdentifiers(table);
612
- }
613
- if (typeof directives.pgQuery.withQueryBuilder ===
614
- "function") {
615
- directives.pgQuery.withQueryBuilder(innerQueryBuilder, parsedResolveInfoFragment.args);
616
- }
617
- }, queryBuilder.context, queryBuilder.rootValue);
618
- return sql.fragment `(${query})`;
619
- }, build.getSafeAliasFromAlias(parsedResolveInfoFragment.alias));
620
- },
621
- };
622
- });
623
- }
624
- else if (isScalar && directives.pgQuery.fragment) {
625
- fieldContext.addDataGenerator((parsedResolveInfoFragment) => {
626
- return {
627
- pgQuery: (queryBuilder) => {
628
- queryBuilder.select(typeof directives.pgQuery.fragment === "function"
629
- ? directives.pgQuery.fragment(queryBuilder, parsedResolveInfoFragment.args)
630
- : directives.pgQuery.fragment, build.getSafeAliasFromAlias(parsedResolveInfoFragment.alias));
631
- },
632
- };
633
- });
634
- }
635
- else {
636
- throw new Error(`@pgQuery(...) directive called with invalid arguments - for a table value, call it with 'source' for a scalar with 'fragment'!`);
637
- }
638
- }
639
- const resolversSpec = rawResolversSpec
640
- ? Object.keys(rawResolversSpec).reduce((newResolversSpec, key) => {
641
- if (typeof rawResolversSpec[key] === "function") {
642
- newResolversSpec[key] = augmentResolver(rawResolversSpec[key], fieldContext, type);
643
- }
644
- return newResolversSpec;
645
- }, {})
646
- : {};
647
- return Object.assign(Object.assign(Object.assign({ type,
648
- args }, (deprecationReason
649
- ? {
650
- deprecationReason,
651
- }
652
- : null)), (description
653
- ? {
654
- description,
655
- }
656
- : null)), resolversSpec);
657
- };
658
- if (directives.pgField) {
659
- return build.extend(memo, {
660
- [fieldName]: build.pgField(build, fieldWithHooks, fieldName, fieldSpecGenerator, scope, false),
661
- });
662
- }
663
- else {
664
- return build.extend(memo, {
665
- [fieldName]: fieldWithHooks(fieldName, fieldSpecGenerator, scope),
666
- });
667
- }
668
- }
669
- else {
670
- throw new Error(`AST issue: expected 'FieldDefinition', instead received '${field.kind}'`);
671
- }
672
- }, {});
673
- }
674
- return {};
675
- }
676
- function getInputFields(_Self, fields, build) {
677
- if (fields && fields.length) {
678
- return fields.reduce((memo, field) => {
679
- if (field.kind === "InputValueDefinition") {
680
- const description = getDescription(field.description);
681
- const fieldName = getName(field.name);
682
- const type = getType(field.type, build);
683
- const defaultValue = field.defaultValue
684
- ? getValue(field.defaultValue, type)
685
- : undefined;
686
- memo[fieldName] = Object.assign({ type,
687
- defaultValue }, (description
688
- ? {
689
- description,
690
- }
691
- : null));
692
- }
693
- else {
694
- throw new Error(`AST issue: expected 'FieldDefinition', instead received '${field.kind}'`);
695
- }
696
- return memo;
697
- }, {});
698
- }
699
- return {};
700
- }
701
- }
702
- exports.default = makeExtendSchemaPlugin;
703
- //# sourceMappingURL=makeExtendSchemaPlugin.js.map