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