graphile-utils 5.0.0-beta.4 → 5.0.0-beta.41
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.
- package/CHANGELOG.md +936 -0
- package/README.md +55 -34
- package/dist/exportable.d.ts +1 -1
- package/dist/exportable.d.ts.map +1 -1
- package/dist/exportable.js +3 -3
- package/dist/exportable.js.map +1 -1
- package/dist/gql.js +1 -2
- package/dist/gql.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/makeAddInflectorsPlugin.js +2 -3
- package/dist/makeAddInflectorsPlugin.js.map +1 -1
- package/dist/makeAddPgTableConditionPlugin.d.ts +4 -4
- package/dist/makeAddPgTableConditionPlugin.d.ts.map +1 -1
- package/dist/makeAddPgTableConditionPlugin.js +11 -10
- package/dist/makeAddPgTableConditionPlugin.js.map +1 -1
- package/dist/makeAddPgTableOrderByPlugin.d.ts +10 -9
- package/dist/makeAddPgTableOrderByPlugin.d.ts.map +1 -1
- package/dist/makeAddPgTableOrderByPlugin.js +38 -33
- package/dist/makeAddPgTableOrderByPlugin.js.map +1 -1
- package/dist/makeChangeNullabilityPlugin.d.ts.map +1 -1
- package/dist/makeChangeNullabilityPlugin.js +1 -2
- package/dist/makeChangeNullabilityPlugin.js.map +1 -1
- package/dist/makeExtendSchemaPlugin.d.ts +38 -25
- package/dist/makeExtendSchemaPlugin.d.ts.map +1 -1
- package/dist/makeExtendSchemaPlugin.js +317 -54
- package/dist/makeExtendSchemaPlugin.js.map +1 -1
- package/dist/makePgSmartTagsPlugin.d.ts +8 -1
- package/dist/makePgSmartTagsPlugin.d.ts.map +1 -1
- package/dist/makePgSmartTagsPlugin.js +6 -6
- package/dist/makePgSmartTagsPlugin.js.map +1 -1
- package/dist/makeProcessSchemaPlugin.js +1 -2
- package/dist/makeProcessSchemaPlugin.js.map +1 -1
- package/dist/makeWrapPlansPlugin.d.ts +1 -1
- package/dist/makeWrapPlansPlugin.d.ts.map +1 -1
- package/dist/makeWrapPlansPlugin.js +23 -10
- package/dist/makeWrapPlansPlugin.js.map +1 -1
- package/dist/parseIdentifierParts.js +1 -2
- package/dist/parseIdentifierParts.js.map +1 -1
- package/package.json +25 -22
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeExtendSchemaPlugin =
|
|
3
|
+
exports.makeExtendSchemaPlugin = makeExtendSchemaPlugin;
|
|
4
4
|
const exportable_js_1 = require("./exportable.js");
|
|
5
5
|
function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugin_${String(Math.random()).slice(2)}`) {
|
|
6
6
|
let graphql;
|
|
7
|
+
if (typeof generator === "function" && generator.length >= 2) {
|
|
8
|
+
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`.");
|
|
9
|
+
}
|
|
7
10
|
return {
|
|
8
11
|
name: uniquePluginName,
|
|
9
12
|
version: "0.0.0",
|
|
@@ -12,19 +15,38 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
12
15
|
build(build) {
|
|
13
16
|
// Extract GraphQL into the scope so that our other functions can use it.
|
|
14
17
|
graphql = build.graphql;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
if (!build.makeExtendSchemaPlugin) {
|
|
19
|
+
build.makeExtendSchemaPlugin = Object.create(null);
|
|
20
|
+
}
|
|
21
|
+
return build;
|
|
22
|
+
},
|
|
23
|
+
init(_, build, _context) {
|
|
24
|
+
const { GraphQLDirective, GraphQLEnumType, GraphQLError, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLScalarType, GraphQLUnionType, Kind, parse, } = graphql;
|
|
25
|
+
const { typeDefs, resolvers = Object.create(null), plans: rawPlans, enums, scalars, inputObjects, interfaces, unions, objects, } = typeof generator === "function"
|
|
26
|
+
? generator.length === 1
|
|
27
|
+
? generator(build)
|
|
28
|
+
: /* TODO: DELETE THIS! */
|
|
29
|
+
generator(build, build.options)
|
|
19
30
|
: generator;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
if (typeDefs == null) {
|
|
32
|
+
throw new Error("The first argument to makeExtendSchemaPlugin must be an object containing a `typeDefs` field.");
|
|
33
|
+
}
|
|
34
|
+
const document = typeof typeDefs === "string"
|
|
35
|
+
? parse(typeDefs)
|
|
36
|
+
: Array.isArray(typeDefs)
|
|
37
|
+
? {
|
|
38
|
+
kind: graphql.Kind.DOCUMENT,
|
|
39
|
+
definitions: typeDefs.flatMap((t) => {
|
|
40
|
+
if (!t || t.kind !== "Document") {
|
|
41
|
+
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.');
|
|
42
|
+
}
|
|
43
|
+
return t.definitions;
|
|
44
|
+
}),
|
|
45
|
+
}
|
|
46
|
+
: typeDefs;
|
|
47
|
+
if (!document || document.kind !== "Document") {
|
|
48
|
+
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.');
|
|
49
|
+
}
|
|
28
50
|
const typeExtensions = {
|
|
29
51
|
GraphQLSchema: {
|
|
30
52
|
directives: [],
|
|
@@ -35,7 +57,7 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
35
57
|
GraphQLInterfaceType: {},
|
|
36
58
|
};
|
|
37
59
|
const newTypes = [];
|
|
38
|
-
|
|
60
|
+
document.definitions.forEach((definition) => {
|
|
39
61
|
if (definition.kind === "EnumTypeDefinition") {
|
|
40
62
|
newTypes.push({
|
|
41
63
|
type: GraphQLEnumType,
|
|
@@ -106,8 +128,160 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
106
128
|
throw new Error(`Unexpected '${definition.kind}' definition; we were expecting 'GraphQLEnumType', 'ObjectTypeExtension', 'InputObjectTypeExtension', 'ObjectTypeDefinition' or 'InputObjectTypeDefinition', i.e. something like 'extend type Foo { ... }'`);
|
|
107
129
|
}
|
|
108
130
|
});
|
|
109
|
-
|
|
110
|
-
|
|
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 ["an interface type", "interfaces"];
|
|
179
|
+
}
|
|
180
|
+
else if (node.kind === Kind.UNION_TYPE_EXTENSION ||
|
|
181
|
+
node.kind === Kind.UNION_TYPE_DEFINITION) {
|
|
182
|
+
return ["a union type", "unions"];
|
|
183
|
+
}
|
|
184
|
+
else if (node.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION ||
|
|
185
|
+
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
|
|
186
|
+
return [
|
|
187
|
+
"an input object type",
|
|
188
|
+
"inputObjects",
|
|
189
|
+
nodes
|
|
190
|
+
.flatMap((n) => n.fields?.map((f) => f.name.value))
|
|
191
|
+
.filter(isNotNullish),
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
else if (node.kind === Kind.SCALAR_TYPE_EXTENSION ||
|
|
195
|
+
node.kind === Kind.SCALAR_TYPE_DEFINITION) {
|
|
196
|
+
return ["a scalar type", "scalars"];
|
|
197
|
+
}
|
|
198
|
+
else if (node.kind === Kind.ENUM_TYPE_EXTENSION ||
|
|
199
|
+
node.kind === Kind.ENUM_TYPE_DEFINITION) {
|
|
200
|
+
return [
|
|
201
|
+
"an enum type",
|
|
202
|
+
"enums",
|
|
203
|
+
nodes
|
|
204
|
+
.flatMap((n) => n.values?.map((f) => f.name.value))
|
|
205
|
+
.filter(isNotNullish),
|
|
206
|
+
];
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
throw new Error(`Type ${typeName} not understood`);
|
|
210
|
+
}
|
|
211
|
+
})();
|
|
212
|
+
if (expectedLocation !== attr) {
|
|
213
|
+
throw new Error(`You defined '${typeName}' under '${expectedLocation}', but it is ${description} so it should be defined under '${attr}'.`);
|
|
214
|
+
}
|
|
215
|
+
return names;
|
|
216
|
+
};
|
|
217
|
+
for (const [typeName, spec] of Object.entries(objects ?? {})) {
|
|
218
|
+
const fields = assertLocation(typeName, "objects");
|
|
219
|
+
const o = {};
|
|
220
|
+
plans[typeName] = o;
|
|
221
|
+
const { plans: planResolvers = {}, ...rest } = spec;
|
|
222
|
+
for (const [key, val] of Object.entries(rest)) {
|
|
223
|
+
o[`__${key}`] = val;
|
|
224
|
+
}
|
|
225
|
+
for (const [key, val] of Object.entries(planResolvers)) {
|
|
226
|
+
if (!fields.includes(key)) {
|
|
227
|
+
throw new Error(`Object type '${typeName}' field '${key}' was not defined in this plugin.`);
|
|
228
|
+
}
|
|
229
|
+
o[key] = val;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const [typeName, spec] of Object.entries(inputObjects ?? {})) {
|
|
233
|
+
const fields = assertLocation(typeName, "inputObjects");
|
|
234
|
+
const o = {};
|
|
235
|
+
plans[typeName] = o;
|
|
236
|
+
const { plans: planResolvers = {}, ...rest } = spec;
|
|
237
|
+
for (const [key, val] of Object.entries(rest)) {
|
|
238
|
+
o[`__${key}`] = val;
|
|
239
|
+
}
|
|
240
|
+
for (const [key, val] of Object.entries(planResolvers)) {
|
|
241
|
+
if (!fields.includes(key)) {
|
|
242
|
+
throw new Error(`Input object type '${typeName}' field '${key}' was not defined in this plugin.`);
|
|
243
|
+
}
|
|
244
|
+
o[key] = val;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
for (const [typeName, spec] of Object.entries(unions ?? {})) {
|
|
248
|
+
assertLocation(typeName, "unions");
|
|
249
|
+
const o = {};
|
|
250
|
+
plans[typeName] = o;
|
|
251
|
+
for (const [key, val] of Object.entries(spec)) {
|
|
252
|
+
o[`__${key}`] = val;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
for (const [typeName, spec] of Object.entries(interfaces ?? {})) {
|
|
256
|
+
assertLocation(typeName, "interfaces");
|
|
257
|
+
const o = {};
|
|
258
|
+
plans[typeName] = o;
|
|
259
|
+
for (const [key, val] of Object.entries(spec)) {
|
|
260
|
+
o[`__${key}`] = val;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
for (const [typeName, spec] of Object.entries(scalars ?? {})) {
|
|
264
|
+
assertLocation(typeName, "scalars");
|
|
265
|
+
plans[typeName] = spec;
|
|
266
|
+
}
|
|
267
|
+
for (const [typeName, spec] of Object.entries(enums ?? {})) {
|
|
268
|
+
const enumValues = assertLocation(typeName, "enums");
|
|
269
|
+
const o = {};
|
|
270
|
+
plans[typeName] = o;
|
|
271
|
+
const { values = {}, ...rest } = spec;
|
|
272
|
+
if ("plans" in rest) {
|
|
273
|
+
throw new Error(`Enum type '${typeName}' cannot have field plans, please use 'values'.`);
|
|
274
|
+
}
|
|
275
|
+
for (const [key, val] of Object.entries(rest)) {
|
|
276
|
+
o[`__${key}`] = val;
|
|
277
|
+
}
|
|
278
|
+
for (const [key, val] of Object.entries(values)) {
|
|
279
|
+
if (!enumValues.includes(key)) {
|
|
280
|
+
throw new Error(`Enum type '${typeName}' value '${key}' was not defined in this plugin.`);
|
|
281
|
+
}
|
|
282
|
+
o[key] = val;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
111
285
|
}
|
|
112
286
|
build.makeExtendSchemaPlugin[uniquePluginName] = {
|
|
113
287
|
typeExtensions,
|
|
@@ -115,10 +289,6 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
115
289
|
resolvers,
|
|
116
290
|
plans,
|
|
117
291
|
};
|
|
118
|
-
return build;
|
|
119
|
-
},
|
|
120
|
-
init(_, build, _context) {
|
|
121
|
-
const { makeExtendSchemaPlugin: { [uniquePluginName]: { typeExtensions, newTypes, resolvers, plans, }, }, graphql: { GraphQLEnumType, GraphQLError, GraphQLObjectType, GraphQLInputObjectType, GraphQLUnionType, GraphQLInterfaceType, GraphQLScalarType, GraphQLDirective, Kind, }, } = build;
|
|
122
292
|
newTypes.forEach((def) => {
|
|
123
293
|
if (def.type === GraphQLEnumType) {
|
|
124
294
|
const definition = def.definition;
|
|
@@ -126,7 +296,9 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
126
296
|
const name = getName(definition.name);
|
|
127
297
|
const description = getDescription(definition.description);
|
|
128
298
|
const directives = getDirectives(definition.directives);
|
|
129
|
-
const relevantResolver = plans[name]
|
|
299
|
+
const relevantResolver = (plans[name] ??
|
|
300
|
+
resolvers[name] ??
|
|
301
|
+
{});
|
|
130
302
|
const values = (definition.values ?? []).reduce((memo, value) => {
|
|
131
303
|
const valueName = getName(value.name);
|
|
132
304
|
const valueDescription = getDescription(value.description);
|
|
@@ -154,7 +326,12 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
154
326
|
},
|
|
155
327
|
};
|
|
156
328
|
}, Object.create(null));
|
|
157
|
-
const scope =
|
|
329
|
+
const scope = {
|
|
330
|
+
__origin: `makeExtendSchemaPlugin`,
|
|
331
|
+
directives,
|
|
332
|
+
...scopeFromDirectives(directives),
|
|
333
|
+
...plans[name]?.__scope,
|
|
334
|
+
};
|
|
158
335
|
build.registerEnumType(name, scope, () => ({ values, description }), uniquePluginName);
|
|
159
336
|
}
|
|
160
337
|
else if (def.type === GraphQLObjectType) {
|
|
@@ -162,21 +339,37 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
162
339
|
// https://graphql.org/graphql-js/type/#graphqlobjecttype
|
|
163
340
|
const name = getName(definition.name);
|
|
164
341
|
const description = getDescription(definition.description);
|
|
165
|
-
const interfaces = getInterfaces(definition.interfaces, build);
|
|
166
342
|
const directives = getDirectives(definition.directives);
|
|
343
|
+
const p = (plans[name] ?? {});
|
|
167
344
|
const scope = {
|
|
168
345
|
__origin: `makeExtendSchemaPlugin`,
|
|
169
346
|
directives,
|
|
170
347
|
...scopeFromDirectives(directives),
|
|
348
|
+
...p.__scope,
|
|
171
349
|
};
|
|
172
350
|
build.registerObjectType(name, scope, () => ({
|
|
173
|
-
interfaces,
|
|
351
|
+
interfaces: () => getInterfaces(definition.interfaces, build),
|
|
174
352
|
fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, resolvers, plans, fieldsContext, build),
|
|
175
353
|
...(description
|
|
176
354
|
? {
|
|
177
355
|
description,
|
|
178
356
|
}
|
|
179
357
|
: null),
|
|
358
|
+
...(p.__isTypeOf
|
|
359
|
+
? {
|
|
360
|
+
isTypeOf: p.__isTypeOf,
|
|
361
|
+
}
|
|
362
|
+
: null),
|
|
363
|
+
...(p.__assertStep || p.__planType
|
|
364
|
+
? {
|
|
365
|
+
extensions: {
|
|
366
|
+
grafast: {
|
|
367
|
+
assertStep: p.__assertStep,
|
|
368
|
+
planType: p.__planType,
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
}
|
|
372
|
+
: null),
|
|
180
373
|
}), uniquePluginName);
|
|
181
374
|
}
|
|
182
375
|
else if (def.type === GraphQLInputObjectType) {
|
|
@@ -189,6 +382,7 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
189
382
|
__origin: `makeExtendSchemaPlugin`,
|
|
190
383
|
directives,
|
|
191
384
|
...scopeFromDirectives(directives),
|
|
385
|
+
...plans[name]?.__scope,
|
|
192
386
|
};
|
|
193
387
|
build.registerInputObjectType(name, scope, () => ({
|
|
194
388
|
fields: ({ Self }) => getInputFields(Self, definition.fields, build),
|
|
@@ -205,12 +399,21 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
205
399
|
const name = getName(definition.name);
|
|
206
400
|
const description = getDescription(definition.description);
|
|
207
401
|
const directives = getDirectives(definition.directives);
|
|
402
|
+
const tp = plans[name];
|
|
403
|
+
const tr = resolvers[name];
|
|
404
|
+
if (tp && tr) {
|
|
405
|
+
throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
|
|
406
|
+
}
|
|
407
|
+
const t = tp ?? tr;
|
|
208
408
|
const scope = {
|
|
209
409
|
__origin: `makeExtendSchemaPlugin`,
|
|
210
410
|
directives,
|
|
211
411
|
...scopeFromDirectives(directives),
|
|
412
|
+
...t?.__scope,
|
|
212
413
|
};
|
|
213
|
-
const resolveType =
|
|
414
|
+
const resolveType = t?.__resolveType;
|
|
415
|
+
const toSpecifier = t?.__toSpecifier;
|
|
416
|
+
const planType = t?.__planType;
|
|
214
417
|
build.registerUnionType(name, scope, () => ({
|
|
215
418
|
types: () => {
|
|
216
419
|
if (Array.isArray(definition.types)) {
|
|
@@ -226,6 +429,8 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
226
429
|
}
|
|
227
430
|
},
|
|
228
431
|
...(resolveType ? { resolveType } : null),
|
|
432
|
+
...(toSpecifier ? { toSpecifier } : null),
|
|
433
|
+
...(planType ? { planType } : null),
|
|
229
434
|
...(description ? { description } : null),
|
|
230
435
|
}), uniquePluginName);
|
|
231
436
|
}
|
|
@@ -235,14 +440,25 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
235
440
|
const name = getName(definition.name);
|
|
236
441
|
const description = getDescription(definition.description);
|
|
237
442
|
const directives = getDirectives(definition.directives);
|
|
443
|
+
const tp = plans[name];
|
|
444
|
+
const tr = resolvers[name];
|
|
445
|
+
if (tp && tr) {
|
|
446
|
+
throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
|
|
447
|
+
}
|
|
448
|
+
const t = tp ?? tr;
|
|
238
449
|
const scope = {
|
|
239
450
|
__origin: `makeExtendSchemaPlugin`,
|
|
240
451
|
directives,
|
|
241
452
|
...scopeFromDirectives(directives),
|
|
453
|
+
...t?.__scope,
|
|
242
454
|
};
|
|
243
|
-
const resolveType =
|
|
455
|
+
const resolveType = t?.__resolveType;
|
|
456
|
+
const toSpecifier = t?.__toSpecifier;
|
|
457
|
+
const planType = t?.__planType;
|
|
244
458
|
build.registerInterfaceType(name, scope, () => ({
|
|
245
459
|
...(resolveType ? { resolveType } : null),
|
|
460
|
+
...(toSpecifier ? { toSpecifier } : null),
|
|
461
|
+
...(planType ? { planType } : null),
|
|
246
462
|
...(description ? { description } : null),
|
|
247
463
|
fields: (fieldsContext) => getFields(fieldsContext.Self, definition.fields, Object.create(null), // Interface doesn't need resolvers
|
|
248
464
|
Object.create(null), // Interface doesn't need resolvers
|
|
@@ -263,17 +479,38 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
263
479
|
__origin: `makeExtendSchemaPlugin`,
|
|
264
480
|
directives,
|
|
265
481
|
...scopeFromDirectives(directives),
|
|
482
|
+
...plans[name]?.__scope,
|
|
266
483
|
};
|
|
484
|
+
const possiblePlan = plans[name];
|
|
485
|
+
const possibleResolver = resolvers[name];
|
|
486
|
+
if (possiblePlan && possibleResolver) {
|
|
487
|
+
throw new Error(`You must set only plans.${name} or resolvers.${name} - not both!`);
|
|
488
|
+
}
|
|
489
|
+
const rawConfig = possiblePlan ?? possibleResolver;
|
|
490
|
+
const config = rawConfig
|
|
491
|
+
? rawConfig instanceof GraphQLScalarType
|
|
492
|
+
? (0, exportable_js_1.EXPORTABLE)((rawConfig) => rawConfig.toConfig(), [rawConfig])
|
|
493
|
+
: rawConfig
|
|
494
|
+
: null;
|
|
267
495
|
build.registerScalarType(name, scope, () => ({
|
|
268
496
|
description,
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
497
|
+
astNode: definition,
|
|
498
|
+
extensions: config?.extensions,
|
|
499
|
+
specifiedByURL: config?.specifiedByURL,
|
|
500
|
+
serialize: config?.serialize
|
|
501
|
+
? (0, exportable_js_1.EXPORTABLE)((config) => config.serialize, [config])
|
|
502
|
+
: (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
|
|
503
|
+
parseValue: config?.parseValue
|
|
504
|
+
? (0, exportable_js_1.EXPORTABLE)((config) => config.parseValue, [config])
|
|
505
|
+
: (0, exportable_js_1.EXPORTABLE)(() => (value) => String(value), []),
|
|
506
|
+
parseLiteral: config?.parseLiteral
|
|
507
|
+
? (0, exportable_js_1.EXPORTABLE)((config) => config.parseLiteral, [config])
|
|
508
|
+
: (0, exportable_js_1.EXPORTABLE)((GraphQLError, Kind, name) => (ast) => {
|
|
509
|
+
if (ast.kind !== Kind.STRING) {
|
|
510
|
+
throw new GraphQLError(`${name} can only parse string values`);
|
|
511
|
+
}
|
|
512
|
+
return ast.value;
|
|
513
|
+
}, [GraphQLError, Kind, name]),
|
|
277
514
|
}), uniquePluginName);
|
|
278
515
|
}
|
|
279
516
|
else if (def.type === GraphQLDirective) {
|
|
@@ -503,8 +740,12 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
503
740
|
if (arg.defaultValue) {
|
|
504
741
|
defaultValue = getValue(arg.defaultValue, type);
|
|
505
742
|
}
|
|
743
|
+
const argDirectives = getDirectives(arg.directives);
|
|
744
|
+
const deprecatedDirective = argDirectives.find((d) => d.directiveName === "deprecated");
|
|
745
|
+
const deprecationReason = deprecatedDirective?.args.reason;
|
|
506
746
|
memo[name] = {
|
|
507
747
|
type,
|
|
748
|
+
deprecationReason,
|
|
508
749
|
...(defaultValue != null ? { defaultValue } : null),
|
|
509
750
|
...(description ? { description } : null),
|
|
510
751
|
};
|
|
@@ -518,6 +759,7 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
518
759
|
return {};
|
|
519
760
|
}
|
|
520
761
|
function getFields(SelfGeneric, fields, resolvers, plans, context, build) {
|
|
762
|
+
const { graphql: { getNullableType, getNamedType }, } = build;
|
|
521
763
|
const { fieldWithHooks } = context;
|
|
522
764
|
const isRootSubscription = "isRootSubscription" in context.scope && context.scope.isRootSubscription;
|
|
523
765
|
if (!build.graphql.isNamedType(SelfGeneric)) {
|
|
@@ -532,25 +774,6 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
532
774
|
const args = getArguments(field.arguments, build);
|
|
533
775
|
const type = getType(field.type, build);
|
|
534
776
|
const directives = getDirectives(field.directives);
|
|
535
|
-
const scope = {
|
|
536
|
-
fieldName,
|
|
537
|
-
/*
|
|
538
|
-
...(typeScope.pgIntrospection &&
|
|
539
|
-
typeScope.pgIntrospection.kind === "class"
|
|
540
|
-
? {
|
|
541
|
-
pgFieldIntrospection: typeScope.pgIntrospection,
|
|
542
|
-
}
|
|
543
|
-
: null),
|
|
544
|
-
...(typeScope.isPgRowConnectionType && typeScope.pgIntrospection
|
|
545
|
-
? {
|
|
546
|
-
isPgFieldConnection: true,
|
|
547
|
-
pgFieldIntrospection: typeScope.pgIntrospection,
|
|
548
|
-
}
|
|
549
|
-
: null),
|
|
550
|
-
*/
|
|
551
|
-
fieldDirectives: directives,
|
|
552
|
-
...scopeFromDirectives(directives),
|
|
553
|
-
};
|
|
554
777
|
const deprecatedDirective = directives.find((d) => d.directiveName === "deprecated");
|
|
555
778
|
const deprecationReason = deprecatedDirective?.args.reason;
|
|
556
779
|
/*
|
|
@@ -559,7 +782,9 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
559
782
|
* other relevant methods.
|
|
560
783
|
*/
|
|
561
784
|
const possiblePlan = plans[Self.name]?.[fieldName];
|
|
785
|
+
build.exportNameHint(possiblePlan, `${Self.name}_${fieldName}_plan`);
|
|
562
786
|
const possibleResolver = resolvers[Self.name]?.[fieldName];
|
|
787
|
+
build.exportNameHint(possibleResolver, `${Self.name}_${fieldName}_resolver`);
|
|
563
788
|
if (possiblePlan && possibleResolver) {
|
|
564
789
|
throw new Error(`You must set only plans.${Self.name}.${fieldName} or resolvers.${Self.name}.${fieldName} - not both!`);
|
|
565
790
|
}
|
|
@@ -591,6 +816,42 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
591
816
|
args,
|
|
592
817
|
};
|
|
593
818
|
};
|
|
819
|
+
const nullableType = getNullableType(type);
|
|
820
|
+
const namedType = getNamedType(type);
|
|
821
|
+
const typeScope = build.scopeByType.get(namedType);
|
|
822
|
+
const scope = {
|
|
823
|
+
fieldDirectives: directives,
|
|
824
|
+
// Guess a codec and resource
|
|
825
|
+
...(typeScope && "pgCodec" in typeScope && typeScope.pgCodec
|
|
826
|
+
? {
|
|
827
|
+
pgFieldCodec: typeScope.pgCodec,
|
|
828
|
+
// First guess at a resource; may be overwritten
|
|
829
|
+
pgFieldResource: Object.values(build.input.pgRegistry.pgResources).find((r) => r.codec === typeScope.pgCodec &&
|
|
830
|
+
!r.isUnique &&
|
|
831
|
+
!r.parameters &&
|
|
832
|
+
!r.isVirtual &&
|
|
833
|
+
!r.isList &&
|
|
834
|
+
!r.codec.polymorphism),
|
|
835
|
+
}
|
|
836
|
+
: null),
|
|
837
|
+
// Guess (more accurately) a resource
|
|
838
|
+
...(typeScope &&
|
|
839
|
+
"pgTypeResource" in typeScope &&
|
|
840
|
+
typeScope.pgTypeResource
|
|
841
|
+
? { pgFieldResource: typeScope.pgTypeResource }
|
|
842
|
+
: null),
|
|
843
|
+
// Guess if it's a connection
|
|
844
|
+
...(nullableType === namedType &&
|
|
845
|
+
typeScope &&
|
|
846
|
+
"isConnectionType" in typeScope
|
|
847
|
+
? { isPgFieldConnection: typeScope.isConnectionType }
|
|
848
|
+
: null),
|
|
849
|
+
// Allow user to overwrite
|
|
850
|
+
...scopeFromDirectives(directives),
|
|
851
|
+
...(typeof spec === "object" && spec !== null ? spec.scope : null),
|
|
852
|
+
// fieldName always wins
|
|
853
|
+
fieldName,
|
|
854
|
+
};
|
|
594
855
|
return build.extend(memo, {
|
|
595
856
|
[fieldName]: fieldWithHooks(scope, fieldSpecGenerator),
|
|
596
857
|
}, `Adding '${fieldName}' to '${Self.name}' from '${uniquePluginName}'`);
|
|
@@ -631,7 +892,6 @@ function makeExtendSchemaPlugin(generator, uniquePluginName = `ExtendSchemaPlugi
|
|
|
631
892
|
return {};
|
|
632
893
|
}
|
|
633
894
|
}
|
|
634
|
-
exports.makeExtendSchemaPlugin = makeExtendSchemaPlugin;
|
|
635
895
|
function scopeFromDirectives(directives) {
|
|
636
896
|
return {
|
|
637
897
|
...directives
|
|
@@ -640,4 +900,7 @@ function scopeFromDirectives(directives) {
|
|
|
640
900
|
.reduce((memo, a) => Object.assign(memo, a), Object.create(null)),
|
|
641
901
|
};
|
|
642
902
|
}
|
|
903
|
+
function isNotNullish(v) {
|
|
904
|
+
return v != null;
|
|
905
|
+
}
|
|
643
906
|
//# sourceMappingURL=makeExtendSchemaPlugin.js.map
|