@graphql-codegen/plugin-helpers 2.5.0-alpha-252a8d50d.0 → 2.5.0-alpha-2fbcdb6d3.0
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/cjs/errors.js +18 -0
- package/cjs/federation.js +258 -0
- package/cjs/getCachedDocumentNodeFromSchema.js +5 -0
- package/cjs/helpers.js +169 -0
- package/cjs/index.js +14 -0
- package/cjs/oldVisit.js +20 -0
- package/cjs/package.json +1 -0
- package/cjs/profiler.js +50 -0
- package/cjs/resolve-external-module-and-fn.js +33 -0
- package/cjs/types.js +7 -0
- package/cjs/utils.js +36 -0
- package/esm/errors.js +13 -0
- package/esm/federation.js +251 -0
- package/esm/getCachedDocumentNodeFromSchema.js +2 -0
- package/esm/helpers.js +159 -0
- package/esm/index.js +9 -0
- package/esm/oldVisit.js +16 -0
- package/esm/profiler.js +45 -0
- package/esm/resolve-external-module-and-fn.js +28 -0
- package/esm/types.js +3 -0
- package/esm/utils.js +29 -0
- package/package.json +22 -15
- package/{errors.d.ts → typings/errors.d.ts} +0 -0
- package/{federation.d.ts → typings/federation.d.ts} +0 -0
- package/{getCachedDocumentNodeFromSchema.d.ts → typings/getCachedDocumentNodeFromSchema.d.ts} +0 -0
- package/{helpers.d.ts → typings/helpers.d.ts} +1 -1
- package/typings/index.d.ts +9 -0
- package/{oldVisit.d.ts → typings/oldVisit.d.ts} +0 -0
- package/{profiler.d.ts → typings/profiler.d.ts} +0 -0
- package/{resolve-external-module-and-fn.d.ts → typings/resolve-external-module-and-fn.d.ts} +0 -0
- package/{types.d.ts → typings/types.d.ts} +14 -14
- package/{utils.d.ts → typings/utils.d.ts} +1 -1
- package/index.d.ts +0 -9
- package/index.js +0 -588
- package/index.mjs +0 -560
package/cjs/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isDetailedError = exports.DetailedError = void 0;
|
|
4
|
+
class DetailedError extends Error {
|
|
5
|
+
constructor(message, details, source) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.message = message;
|
|
8
|
+
this.details = details;
|
|
9
|
+
this.source = source;
|
|
10
|
+
Object.setPrototypeOf(this, DetailedError.prototype);
|
|
11
|
+
Error.captureStackTrace(this, DetailedError);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.DetailedError = DetailedError;
|
|
15
|
+
function isDetailedError(error) {
|
|
16
|
+
return error.details;
|
|
17
|
+
}
|
|
18
|
+
exports.isDetailedError = isDetailedError;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApolloFederation = exports.removeFederation = exports.addFederationReferencesToSchema = exports.federationSpec = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const merge_js_1 = tslib_1.__importDefault(require("lodash/merge.js"));
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
9
|
+
const index_js_1 = require("./index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Federation Spec
|
|
12
|
+
*/
|
|
13
|
+
exports.federationSpec = (0, graphql_1.parse)(/* GraphQL */ `
|
|
14
|
+
scalar _FieldSet
|
|
15
|
+
|
|
16
|
+
directive @external on FIELD_DEFINITION
|
|
17
|
+
directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
|
|
18
|
+
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
|
|
19
|
+
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
|
|
20
|
+
`);
|
|
21
|
+
/**
|
|
22
|
+
* Adds `__resolveReference` in each ObjectType involved in Federation.
|
|
23
|
+
* @param schema
|
|
24
|
+
*/
|
|
25
|
+
function addFederationReferencesToSchema(schema) {
|
|
26
|
+
return (0, utils_1.mapSchema)(schema, {
|
|
27
|
+
[utils_1.MapperKind.OBJECT_TYPE]: type => {
|
|
28
|
+
if (isFederationObjectType(type, schema)) {
|
|
29
|
+
const typeConfig = type.toConfig();
|
|
30
|
+
typeConfig.fields = {
|
|
31
|
+
[resolveReferenceFieldName]: {
|
|
32
|
+
type,
|
|
33
|
+
},
|
|
34
|
+
...typeConfig.fields,
|
|
35
|
+
};
|
|
36
|
+
return new graphql_1.GraphQLObjectType(typeConfig);
|
|
37
|
+
}
|
|
38
|
+
return type;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
exports.addFederationReferencesToSchema = addFederationReferencesToSchema;
|
|
43
|
+
/**
|
|
44
|
+
* Removes Federation Spec from GraphQL Schema
|
|
45
|
+
* @param schema
|
|
46
|
+
* @param config
|
|
47
|
+
*/
|
|
48
|
+
function removeFederation(schema) {
|
|
49
|
+
return (0, utils_1.mapSchema)(schema, {
|
|
50
|
+
[utils_1.MapperKind.QUERY]: queryType => {
|
|
51
|
+
const queryTypeConfig = queryType.toConfig();
|
|
52
|
+
delete queryTypeConfig.fields._entities;
|
|
53
|
+
delete queryTypeConfig.fields._service;
|
|
54
|
+
return new graphql_1.GraphQLObjectType(queryTypeConfig);
|
|
55
|
+
},
|
|
56
|
+
[utils_1.MapperKind.UNION_TYPE]: unionType => {
|
|
57
|
+
const unionTypeName = unionType.name;
|
|
58
|
+
if (unionTypeName === '_Entity' || unionTypeName === '_Any') {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return unionType;
|
|
62
|
+
},
|
|
63
|
+
[utils_1.MapperKind.OBJECT_TYPE]: objectType => {
|
|
64
|
+
if (objectType.name === '_Service') {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return objectType;
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
exports.removeFederation = removeFederation;
|
|
72
|
+
const resolveReferenceFieldName = '__resolveReference';
|
|
73
|
+
class ApolloFederation {
|
|
74
|
+
constructor({ enabled, schema }) {
|
|
75
|
+
this.enabled = false;
|
|
76
|
+
this.enabled = enabled;
|
|
77
|
+
this.schema = schema;
|
|
78
|
+
this.providesMap = this.createMapOfProvides();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Excludes types definde by Federation
|
|
82
|
+
* @param typeNames List of type names
|
|
83
|
+
*/
|
|
84
|
+
filterTypeNames(typeNames) {
|
|
85
|
+
return this.enabled ? typeNames.filter(t => t !== '_FieldSet') : typeNames;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Excludes `__resolveReference` fields
|
|
89
|
+
* @param fieldNames List of field names
|
|
90
|
+
*/
|
|
91
|
+
filterFieldNames(fieldNames) {
|
|
92
|
+
return this.enabled ? fieldNames.filter(t => t !== resolveReferenceFieldName) : fieldNames;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Decides if directive should not be generated
|
|
96
|
+
* @param name directive's name
|
|
97
|
+
*/
|
|
98
|
+
skipDirective(name) {
|
|
99
|
+
return this.enabled && ['external', 'requires', 'provides', 'key'].includes(name);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Decides if scalar should not be generated
|
|
103
|
+
* @param name directive's name
|
|
104
|
+
*/
|
|
105
|
+
skipScalar(name) {
|
|
106
|
+
return this.enabled && name === '_FieldSet';
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Decides if field should not be generated
|
|
110
|
+
* @param data
|
|
111
|
+
*/
|
|
112
|
+
skipField({ fieldNode, parentType }) {
|
|
113
|
+
if (!this.enabled || !(0, graphql_1.isObjectType)(parentType) || !isFederationObjectType(parentType, this.schema)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return this.isExternalAndNotProvided(fieldNode, parentType);
|
|
117
|
+
}
|
|
118
|
+
isResolveReferenceField(fieldNode) {
|
|
119
|
+
const name = typeof fieldNode.name === 'string' ? fieldNode.name : fieldNode.name.value;
|
|
120
|
+
return this.enabled && name === resolveReferenceFieldName;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Transforms ParentType signature in ObjectTypes involved in Federation
|
|
124
|
+
* @param data
|
|
125
|
+
*/
|
|
126
|
+
transformParentType({ fieldNode, parentType, parentTypeSignature, }) {
|
|
127
|
+
if (this.enabled &&
|
|
128
|
+
(0, graphql_1.isObjectType)(parentType) &&
|
|
129
|
+
isFederationObjectType(parentType, this.schema) &&
|
|
130
|
+
(isTypeExtension(parentType, this.schema) || fieldNode.name.value === resolveReferenceFieldName)) {
|
|
131
|
+
const keys = getDirectivesByName('key', parentType);
|
|
132
|
+
if (keys.length) {
|
|
133
|
+
const outputs = [`{ __typename: '${parentType.name}' } &`];
|
|
134
|
+
// Look for @requires and see what the service needs and gets
|
|
135
|
+
const requires = getDirectivesByName('requires', fieldNode).map(this.extractKeyOrRequiresFieldSet);
|
|
136
|
+
const requiredFields = this.translateFieldSet((0, merge_js_1.default)({}, ...requires), parentTypeSignature);
|
|
137
|
+
// @key() @key() - "primary keys" in Federation
|
|
138
|
+
const primaryKeys = keys.map(def => {
|
|
139
|
+
const fields = this.extractKeyOrRequiresFieldSet(def);
|
|
140
|
+
return this.translateFieldSet(fields, parentTypeSignature);
|
|
141
|
+
});
|
|
142
|
+
const [open, close] = primaryKeys.length > 1 ? ['(', ')'] : ['', ''];
|
|
143
|
+
outputs.push([open, primaryKeys.join(' | '), close].join(''));
|
|
144
|
+
// include required fields
|
|
145
|
+
if (requires.length) {
|
|
146
|
+
outputs.push(`& ${requiredFields}`);
|
|
147
|
+
}
|
|
148
|
+
return outputs.join(' ');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return parentTypeSignature;
|
|
152
|
+
}
|
|
153
|
+
isExternalAndNotProvided(fieldNode, objectType) {
|
|
154
|
+
return this.isExternal(fieldNode) && !this.hasProvides(objectType, fieldNode);
|
|
155
|
+
}
|
|
156
|
+
isExternal(node) {
|
|
157
|
+
return getDirectivesByName('external', node).length > 0;
|
|
158
|
+
}
|
|
159
|
+
hasProvides(objectType, node) {
|
|
160
|
+
const fields = this.providesMap[(0, graphql_1.isObjectType)(objectType) ? objectType.name : objectType.name.value];
|
|
161
|
+
if (fields && fields.length) {
|
|
162
|
+
return fields.includes(node.name.value);
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
translateFieldSet(fields, parentTypeRef) {
|
|
167
|
+
return `GraphQLRecursivePick<${parentTypeRef}, ${JSON.stringify(fields)}>`;
|
|
168
|
+
}
|
|
169
|
+
extractKeyOrRequiresFieldSet(directive) {
|
|
170
|
+
const arg = directive.arguments.find(arg => arg.name.value === 'fields');
|
|
171
|
+
const { value } = arg.value;
|
|
172
|
+
return (0, index_js_1.oldVisit)((0, graphql_1.parse)(`{${value}}`), {
|
|
173
|
+
leave: {
|
|
174
|
+
SelectionSet(node) {
|
|
175
|
+
return node.selections.reduce((accum, field) => {
|
|
176
|
+
accum[field.name] = field.selection;
|
|
177
|
+
return accum;
|
|
178
|
+
}, {});
|
|
179
|
+
},
|
|
180
|
+
Field(node) {
|
|
181
|
+
return {
|
|
182
|
+
name: node.name.value,
|
|
183
|
+
selection: node.selectionSet ? node.selectionSet : true,
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
Document(node) {
|
|
187
|
+
return node.definitions.find((def) => def.kind === 'OperationDefinition' && def.operation === 'query').selectionSet;
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
extractProvidesFieldSet(directive) {
|
|
193
|
+
const arg = directive.arguments.find(arg => arg.name.value === 'fields');
|
|
194
|
+
const { value } = arg.value;
|
|
195
|
+
if (/[{}]/gi.test(value)) {
|
|
196
|
+
throw new Error('Nested fields in _FieldSet is not supported in the @provides directive');
|
|
197
|
+
}
|
|
198
|
+
return value.split(/\s+/g);
|
|
199
|
+
}
|
|
200
|
+
createMapOfProvides() {
|
|
201
|
+
const providesMap = {};
|
|
202
|
+
Object.keys(this.schema.getTypeMap()).forEach(typename => {
|
|
203
|
+
const objectType = this.schema.getType(typename);
|
|
204
|
+
if ((0, graphql_1.isObjectType)(objectType)) {
|
|
205
|
+
Object.values(objectType.getFields()).forEach(field => {
|
|
206
|
+
const provides = getDirectivesByName('provides', field.astNode)
|
|
207
|
+
.map(this.extractProvidesFieldSet)
|
|
208
|
+
.reduce((prev, curr) => [...prev, ...curr], []);
|
|
209
|
+
const ofType = (0, utils_js_1.getBaseType)(field.type);
|
|
210
|
+
if (!providesMap[ofType.name]) {
|
|
211
|
+
providesMap[ofType.name] = [];
|
|
212
|
+
}
|
|
213
|
+
providesMap[ofType.name].push(...provides);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
return providesMap;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.ApolloFederation = ApolloFederation;
|
|
221
|
+
/**
|
|
222
|
+
* Checks if Object Type is involved in Federation. Based on `@key` directive
|
|
223
|
+
* @param node Type
|
|
224
|
+
*/
|
|
225
|
+
function isFederationObjectType(node, schema) {
|
|
226
|
+
const { name: { value: name }, directives, } = (0, graphql_1.isObjectType)(node) ? (0, utils_1.astFromObjectType)(node, schema) : node;
|
|
227
|
+
const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
|
|
228
|
+
const isNotRoot = !rootTypeNames.has(name);
|
|
229
|
+
const isNotIntrospection = !name.startsWith('__');
|
|
230
|
+
const hasKeyDirective = directives.some(d => d.name.value === 'key');
|
|
231
|
+
return isNotRoot && isNotIntrospection && hasKeyDirective;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Extracts directives from a node based on directive's name
|
|
235
|
+
* @param name directive name
|
|
236
|
+
* @param node ObjectType or Field
|
|
237
|
+
*/
|
|
238
|
+
function getDirectivesByName(name, node) {
|
|
239
|
+
var _a;
|
|
240
|
+
let astNode;
|
|
241
|
+
if ((0, graphql_1.isObjectType)(node)) {
|
|
242
|
+
astNode = node.astNode;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
astNode = node;
|
|
246
|
+
}
|
|
247
|
+
return ((_a = astNode === null || astNode === void 0 ? void 0 : astNode.directives) === null || _a === void 0 ? void 0 : _a.filter(d => d.name.value === name)) || [];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Checks if the Object Type extends a federated type from a remote schema.
|
|
251
|
+
* Based on if any of its fields contain the `@external` directive
|
|
252
|
+
* @param node Type
|
|
253
|
+
*/
|
|
254
|
+
function isTypeExtension(node, schema) {
|
|
255
|
+
var _a;
|
|
256
|
+
const definition = (0, graphql_1.isObjectType)(node) ? node.astNode || (0, utils_1.astFromObjectType)(node, schema) : node;
|
|
257
|
+
return (_a = definition.fields) === null || _a === void 0 ? void 0 : _a.some(field => getDirectivesByName('external', field).length);
|
|
258
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCachedDocumentNodeFromSchema = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
5
|
+
exports.getCachedDocumentNodeFromSchema = (0, utils_1.memoize1)(utils_1.getDocumentNodeFromSchema);
|
package/cjs/helpers.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isUsingTypes = exports.hasNullableTypeRecursively = exports.normalizeConfig = exports.normalizeInstanceOrArray = exports.normalizeOutputParam = exports.isConfiguredOutput = exports.isOutputConfigArray = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
function isOutputConfigArray(type) {
|
|
7
|
+
return Array.isArray(type);
|
|
8
|
+
}
|
|
9
|
+
exports.isOutputConfigArray = isOutputConfigArray;
|
|
10
|
+
function isConfiguredOutput(type) {
|
|
11
|
+
return (typeof type === 'object' && type.plugins) || type.preset;
|
|
12
|
+
}
|
|
13
|
+
exports.isConfiguredOutput = isConfiguredOutput;
|
|
14
|
+
function normalizeOutputParam(config) {
|
|
15
|
+
// In case of direct array with a list of plugins
|
|
16
|
+
if (isOutputConfigArray(config)) {
|
|
17
|
+
return {
|
|
18
|
+
documents: [],
|
|
19
|
+
schema: [],
|
|
20
|
+
plugins: isConfiguredOutput(config) ? config.plugins : config,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (isConfiguredOutput(config)) {
|
|
24
|
+
return config;
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`Invalid "generates" config!`);
|
|
27
|
+
}
|
|
28
|
+
exports.normalizeOutputParam = normalizeOutputParam;
|
|
29
|
+
function normalizeInstanceOrArray(type) {
|
|
30
|
+
if (Array.isArray(type)) {
|
|
31
|
+
return type;
|
|
32
|
+
}
|
|
33
|
+
if (!type) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
return [type];
|
|
37
|
+
}
|
|
38
|
+
exports.normalizeInstanceOrArray = normalizeInstanceOrArray;
|
|
39
|
+
function normalizeConfig(config) {
|
|
40
|
+
if (typeof config === 'string') {
|
|
41
|
+
return [{ [config]: {} }];
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(config)) {
|
|
44
|
+
return config.map(plugin => (typeof plugin === 'string' ? { [plugin]: {} } : plugin));
|
|
45
|
+
}
|
|
46
|
+
if (typeof config === 'object') {
|
|
47
|
+
return Object.keys(config).reduce((prev, pluginName) => [...prev, { [pluginName]: config[pluginName] }], []);
|
|
48
|
+
}
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
exports.normalizeConfig = normalizeConfig;
|
|
52
|
+
function hasNullableTypeRecursively(type) {
|
|
53
|
+
if (!(0, graphql_1.isNonNullType)(type)) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
if ((0, graphql_1.isListType)(type) || (0, graphql_1.isNonNullType)(type)) {
|
|
57
|
+
return hasNullableTypeRecursively(type.ofType);
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
exports.hasNullableTypeRecursively = hasNullableTypeRecursively;
|
|
62
|
+
function isUsingTypes(document, externalFragments, schema) {
|
|
63
|
+
let foundFields = 0;
|
|
64
|
+
const typesStack = [];
|
|
65
|
+
(0, graphql_1.visit)(document, {
|
|
66
|
+
SelectionSet: {
|
|
67
|
+
enter(node, key, parent, anscestors) {
|
|
68
|
+
const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
|
69
|
+
if (insideIgnoredFragment) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const selections = node.selections || [];
|
|
73
|
+
if (schema && selections.length > 0) {
|
|
74
|
+
const nextTypeName = (() => {
|
|
75
|
+
if (parent.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
|
|
76
|
+
return parent.typeCondition.name.value;
|
|
77
|
+
}
|
|
78
|
+
if (parent.kind === graphql_1.Kind.FIELD) {
|
|
79
|
+
const lastType = typesStack[typesStack.length - 1];
|
|
80
|
+
if (!lastType) {
|
|
81
|
+
throw new Error(`Unable to find parent type! Please make sure you operation passes validation`);
|
|
82
|
+
}
|
|
83
|
+
const field = lastType.getFields()[parent.name.value];
|
|
84
|
+
if (!field) {
|
|
85
|
+
throw new Error(`Unable to find field "${parent.name.value}" on type "${lastType}"!`);
|
|
86
|
+
}
|
|
87
|
+
return (0, utils_js_1.getBaseType)(field.type).name;
|
|
88
|
+
}
|
|
89
|
+
if (parent.kind === graphql_1.Kind.OPERATION_DEFINITION) {
|
|
90
|
+
if (parent.operation === 'query') {
|
|
91
|
+
return schema.getQueryType().name;
|
|
92
|
+
}
|
|
93
|
+
if (parent.operation === 'mutation') {
|
|
94
|
+
return schema.getMutationType().name;
|
|
95
|
+
}
|
|
96
|
+
if (parent.operation === 'subscription') {
|
|
97
|
+
return schema.getSubscriptionType().name;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (parent.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
|
101
|
+
if (parent.typeCondition) {
|
|
102
|
+
return parent.typeCondition.name.value;
|
|
103
|
+
}
|
|
104
|
+
return typesStack[typesStack.length - 1].name;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
})();
|
|
108
|
+
typesStack.push(schema.getType(nextTypeName));
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
leave(node) {
|
|
112
|
+
const selections = node.selections || [];
|
|
113
|
+
if (schema && selections.length > 0) {
|
|
114
|
+
typesStack.pop();
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
Field: {
|
|
119
|
+
enter: (node, key, parent, path, anscestors) => {
|
|
120
|
+
if (node.name.value.startsWith('__')) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
|
124
|
+
if (insideIgnoredFragment) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const selections = node.selectionSet ? node.selectionSet.selections || [] : [];
|
|
128
|
+
const relevantFragmentSpreads = selections.filter(s => s.kind === graphql_1.Kind.FRAGMENT_SPREAD && !externalFragments.includes(s.name.value));
|
|
129
|
+
if (selections.length === 0 || relevantFragmentSpreads.length > 0) {
|
|
130
|
+
foundFields++;
|
|
131
|
+
}
|
|
132
|
+
if (schema) {
|
|
133
|
+
const lastType = typesStack[typesStack.length - 1];
|
|
134
|
+
if (lastType && (0, graphql_1.isObjectType)(lastType)) {
|
|
135
|
+
const field = lastType.getFields()[node.name.value];
|
|
136
|
+
if (!field) {
|
|
137
|
+
throw new Error(`Unable to find field "${node.name.value}" on type "${lastType}"!`);
|
|
138
|
+
}
|
|
139
|
+
const currentType = field.type;
|
|
140
|
+
// To handle `Maybe` usage
|
|
141
|
+
if (hasNullableTypeRecursively(currentType)) {
|
|
142
|
+
foundFields++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
VariableDefinition: {
|
|
149
|
+
enter: (node, key, parent, path, anscestors) => {
|
|
150
|
+
const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
|
151
|
+
if (insideIgnoredFragment) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
foundFields++;
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
InputValueDefinition: {
|
|
158
|
+
enter: (node, key, parent, path, anscestors) => {
|
|
159
|
+
const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
|
160
|
+
if (insideIgnoredFragment) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
foundFields++;
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
return foundFields > 0;
|
|
168
|
+
}
|
|
169
|
+
exports.isUsingTypes = isUsingTypes;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveExternalModuleAndFn = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
var resolve_external_module_and_fn_js_1 = require("./resolve-external-module-and-fn.js");
|
|
6
|
+
Object.defineProperty(exports, "resolveExternalModuleAndFn", { enumerable: true, get: function () { return resolve_external_module_and_fn_js_1.resolveExternalModuleAndFn; } });
|
|
7
|
+
tslib_1.__exportStar(require("./types.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./utils.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./helpers.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./federation.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./errors.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./getCachedDocumentNodeFromSchema.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./oldVisit.js"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./profiler.js"), exports);
|
package/cjs/oldVisit.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oldVisit = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
function oldVisit(root, { enter: enterVisitors, leave: leaveVisitors, ...newVisitor }) {
|
|
6
|
+
if (typeof enterVisitors === 'object') {
|
|
7
|
+
for (const key in enterVisitors) {
|
|
8
|
+
newVisitor[key] = newVisitor[key] || {};
|
|
9
|
+
newVisitor[key].enter = enterVisitors[key];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (typeof leaveVisitors === 'object') {
|
|
13
|
+
for (const key in leaveVisitors) {
|
|
14
|
+
newVisitor[key] = newVisitor[key] || {};
|
|
15
|
+
newVisitor[key].leave = leaveVisitors[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return (0, graphql_1.visit)(root, newVisitor);
|
|
19
|
+
}
|
|
20
|
+
exports.oldVisit = oldVisit;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/profiler.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createProfiler = exports.createNoopProfiler = void 0;
|
|
4
|
+
function createNoopProfiler() {
|
|
5
|
+
return {
|
|
6
|
+
run(fn) {
|
|
7
|
+
return Promise.resolve().then(() => fn());
|
|
8
|
+
},
|
|
9
|
+
collect() {
|
|
10
|
+
return [];
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.createNoopProfiler = createNoopProfiler;
|
|
15
|
+
function createProfiler() {
|
|
16
|
+
const events = [];
|
|
17
|
+
return {
|
|
18
|
+
collect() {
|
|
19
|
+
return events;
|
|
20
|
+
},
|
|
21
|
+
run(fn, name, cat) {
|
|
22
|
+
let startTime;
|
|
23
|
+
return Promise.resolve()
|
|
24
|
+
.then(() => {
|
|
25
|
+
startTime = process.hrtime();
|
|
26
|
+
})
|
|
27
|
+
.then(() => fn())
|
|
28
|
+
.then(value => {
|
|
29
|
+
const duration = process.hrtime(startTime);
|
|
30
|
+
// Trace Event Format documentation:
|
|
31
|
+
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
|
|
32
|
+
const event = {
|
|
33
|
+
name,
|
|
34
|
+
cat,
|
|
35
|
+
ph: 'X',
|
|
36
|
+
ts: hrtimeToMicroseconds(startTime),
|
|
37
|
+
pid: 1,
|
|
38
|
+
tid: 0,
|
|
39
|
+
dur: hrtimeToMicroseconds(duration),
|
|
40
|
+
};
|
|
41
|
+
events.push(event);
|
|
42
|
+
return value;
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
exports.createProfiler = createProfiler;
|
|
48
|
+
function hrtimeToMicroseconds(hrtime) {
|
|
49
|
+
return (hrtime[0] * 1e9 + hrtime[1]) / 1000;
|
|
50
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveExternalModuleAndFn = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const module_1 = require("module");
|
|
6
|
+
const process_1 = require("process");
|
|
7
|
+
const changeCaseAll = tslib_1.__importStar(require("change-case-all"));
|
|
8
|
+
function resolveExternalModuleAndFn(pointer) {
|
|
9
|
+
if (typeof pointer === 'function') {
|
|
10
|
+
return pointer;
|
|
11
|
+
}
|
|
12
|
+
// eslint-disable-next-line prefer-const
|
|
13
|
+
let [moduleName, functionName] = pointer.split('#');
|
|
14
|
+
// Temp workaround until v2
|
|
15
|
+
if (moduleName === 'change-case') {
|
|
16
|
+
moduleName = 'change-case-all';
|
|
17
|
+
}
|
|
18
|
+
let loadedModule;
|
|
19
|
+
if (moduleName === 'change-case-all') {
|
|
20
|
+
loadedModule = changeCaseAll;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
// we have to use a path to a filename here (it does not need to exist.)
|
|
24
|
+
// https://github.com/dotansimha/graphql-code-generator/issues/6553
|
|
25
|
+
const cwdRequire = (0, module_1.createRequire)((0, process_1.cwd)() + '/index.js');
|
|
26
|
+
loadedModule = cwdRequire(moduleName);
|
|
27
|
+
if (!(functionName in loadedModule) && typeof loadedModule !== 'function') {
|
|
28
|
+
throw new Error(`${functionName} couldn't be found in module ${moduleName}!`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return loadedModule[functionName] || loadedModule;
|
|
32
|
+
}
|
|
33
|
+
exports.resolveExternalModuleAndFn = resolveExternalModuleAndFn;
|
package/cjs/types.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isComplexPluginOutput = void 0;
|
|
4
|
+
function isComplexPluginOutput(obj) {
|
|
5
|
+
return typeof obj === 'object' && obj.hasOwnProperty('content');
|
|
6
|
+
}
|
|
7
|
+
exports.isComplexPluginOutput = isComplexPluginOutput;
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeNonNullWrapper = exports.getBaseType = exports.isWrapperType = exports.mergeOutputs = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
function mergeOutputs(content) {
|
|
6
|
+
const result = { content: '', prepend: [], append: [] };
|
|
7
|
+
if (Array.isArray(content)) {
|
|
8
|
+
content.forEach(item => {
|
|
9
|
+
if (typeof item === 'string') {
|
|
10
|
+
result.content += item;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
result.content += item.content;
|
|
14
|
+
result.prepend.push(...(item.prepend || []));
|
|
15
|
+
result.append.push(...(item.append || []));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return [...result.prepend, result.content, ...result.append].join('\n');
|
|
20
|
+
}
|
|
21
|
+
exports.mergeOutputs = mergeOutputs;
|
|
22
|
+
function isWrapperType(t) {
|
|
23
|
+
return (0, graphql_1.isListType)(t) || (0, graphql_1.isNonNullType)(t);
|
|
24
|
+
}
|
|
25
|
+
exports.isWrapperType = isWrapperType;
|
|
26
|
+
function getBaseType(type) {
|
|
27
|
+
if (isWrapperType(type)) {
|
|
28
|
+
return getBaseType(type.ofType);
|
|
29
|
+
}
|
|
30
|
+
return type;
|
|
31
|
+
}
|
|
32
|
+
exports.getBaseType = getBaseType;
|
|
33
|
+
function removeNonNullWrapper(type) {
|
|
34
|
+
return (0, graphql_1.isNonNullType)(type) ? type.ofType : type;
|
|
35
|
+
}
|
|
36
|
+
exports.removeNonNullWrapper = removeNonNullWrapper;
|
package/esm/errors.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class DetailedError extends Error {
|
|
2
|
+
constructor(message, details, source) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.message = message;
|
|
5
|
+
this.details = details;
|
|
6
|
+
this.source = source;
|
|
7
|
+
Object.setPrototypeOf(this, DetailedError.prototype);
|
|
8
|
+
Error.captureStackTrace(this, DetailedError);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function isDetailedError(error) {
|
|
12
|
+
return error.details;
|
|
13
|
+
}
|