@khanacademy/graphql-flow 1.2.0 → 2.0.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/.babelrc +1 -1
- package/.eslintrc.js +0 -1
- package/.github/workflows/changeset-release.yml +1 -1
- package/CHANGELOG.md +10 -0
- package/dist/cli/config.js +2 -4
- package/dist/cli/run.js +1 -2
- package/dist/enums.js +8 -9
- package/dist/generateResponseType.js +33 -41
- package/dist/generateTypeFiles.js +9 -23
- package/dist/generateVariablesType.js +15 -31
- package/dist/index.js +8 -15
- package/dist/parser/parse.js +6 -7
- package/dist/parser/resolve.js +1 -2
- package/dist/parser/utils.js +1 -2
- package/dist/schemaFromIntrospectionData.js +1 -2
- package/dist/types.js +1 -2
- package/dist/utils.js +43 -3
- package/package.json +8 -7
- package/{dist/__test__/generateTypeFileContents.test.js → src/__test__/generateTypeFileContents.test.ts} +38 -41
- package/{dist/__test__/graphql-flow.test.js → src/__test__/graphql-flow.test.ts} +232 -235
- package/src/__test__/{processPragmas.test.js → processPragmas.test.ts} +0 -1
- package/{dist/cli/__test__/config.test.js → src/cli/__test__/config.test.ts} +5 -6
- package/{dist/cli/config.js.flow → src/cli/config.ts} +6 -11
- package/src/cli/{run.js → run.ts} +5 -4
- package/src/{enums.js → enums.ts} +20 -22
- package/src/{generateResponseType.js → generateResponseType.ts} +167 -182
- package/src/{generateTypeFiles.js → generateTypeFiles.ts} +20 -30
- package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
- package/{dist/index.js.flow → src/index.ts} +32 -24
- package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
- package/src/parser/{parse.js → parse.ts} +65 -47
- package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +15 -11
- package/{dist/parser/utils.js.flow → src/parser/utils.ts} +0 -1
- package/{dist/schemaFromIntrospectionData.js.flow → src/schemaFromIntrospectionData.ts} +1 -4
- package/src/types.ts +97 -0
- package/src/utils.ts +73 -0
- package/tools/{find-files-with-gql.js → find-files-with-gql.ts} +2 -3
- package/tsconfig.json +110 -0
- package/types/flow-to-ts.d.ts +1 -0
- package/dist/__test__/example-schema.graphql +0 -67
- package/dist/__test__/processPragmas.test.js +0 -76
- package/dist/cli/config.js.map +0 -1
- package/dist/cli/run.js.flow +0 -236
- package/dist/cli/run.js.map +0 -1
- package/dist/enums.js.flow +0 -98
- package/dist/enums.js.map +0 -1
- package/dist/generateResponseType.js.flow +0 -583
- package/dist/generateResponseType.js.map +0 -1
- package/dist/generateTypeFiles.js.flow +0 -191
- package/dist/generateTypeFiles.js.map +0 -1
- package/dist/generateVariablesType.js.flow +0 -156
- package/dist/generateVariablesType.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/parser/parse.js.flow +0 -417
- package/dist/parser/parse.js.map +0 -1
- package/dist/parser/resolve.js.map +0 -1
- package/dist/parser/utils.js.map +0 -1
- package/dist/schemaFromIntrospectionData.js.map +0 -1
- package/dist/types.js.flow +0 -88
- package/dist/types.js.map +0 -1
- package/dist/utils.js.flow +0 -50
- package/dist/utils.js.map +0 -1
- package/flow-typed/npm/@babel/types_vx.x.x.js +0 -5331
- package/flow-typed/npm/jest_v23.x.x.js +0 -1155
- package/flow-typed/overrides.js +0 -435
- package/src/__test__/generateTypeFileContents.test.js +0 -157
- package/src/__test__/graphql-flow.test.js +0 -639
- package/src/cli/__test__/config.test.js +0 -120
- package/src/cli/config.js +0 -84
- package/src/cli/schema.json +0 -97
- package/src/index.js +0 -160
- package/src/parser/__test__/parse.test.js +0 -249
- package/src/parser/resolve.js +0 -119
- package/src/parser/utils.js +0 -25
- package/src/schemaFromIntrospectionData.js +0 -68
- package/src/types.js +0 -88
- package/src/utils.js +0 -50
- /package/{dist/cli/schema.json → schema.json} +0 -0
|
@@ -1,583 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/* eslint-disable no-console */
|
|
3
|
-
import generate from '@babel/generator'; // eslint-disable-line flowtype-errors/uncovered
|
|
4
|
-
import * as babelTypes from '@babel/types';
|
|
5
|
-
import {type BabelNodeFlowType} from '@babel/types';
|
|
6
|
-
import type {
|
|
7
|
-
FieldNode,
|
|
8
|
-
IntrospectionOutputTypeRef,
|
|
9
|
-
OperationDefinitionNode,
|
|
10
|
-
FragmentDefinitionNode,
|
|
11
|
-
} from 'graphql';
|
|
12
|
-
import type {Context, Schema, Selections} from './types';
|
|
13
|
-
import {
|
|
14
|
-
liftLeadingPropertyComments,
|
|
15
|
-
maybeAddDescriptionComment,
|
|
16
|
-
transferLeadingComments,
|
|
17
|
-
} from './utils';
|
|
18
|
-
import {enumTypeToFlow, scalarTypeToFlow} from './enums';
|
|
19
|
-
import type {
|
|
20
|
-
IntrospectionField,
|
|
21
|
-
IntrospectionInterfaceType,
|
|
22
|
-
IntrospectionObjectType,
|
|
23
|
-
IntrospectionUnionType,
|
|
24
|
-
} from 'graphql/utilities/introspectionQuery';
|
|
25
|
-
import {
|
|
26
|
-
BabelNodeObjectTypeProperty,
|
|
27
|
-
BabelNodeObjectTypeSpreadProperty,
|
|
28
|
-
} from '@babel/types';
|
|
29
|
-
|
|
30
|
-
export const generateResponseType = (
|
|
31
|
-
schema: Schema,
|
|
32
|
-
query: OperationDefinitionNode,
|
|
33
|
-
ctx: Context,
|
|
34
|
-
): string => {
|
|
35
|
-
const ast = querySelectionToObjectType(
|
|
36
|
-
ctx,
|
|
37
|
-
query.selectionSet.selections,
|
|
38
|
-
query.operation === 'mutation'
|
|
39
|
-
? schema.typesByName.Mutation
|
|
40
|
-
: schema.typesByName.Query,
|
|
41
|
-
query.operation === 'mutation' ? 'mutation' : 'query',
|
|
42
|
-
);
|
|
43
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
44
|
-
return generate(ast).code;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const sortedObjectTypeAnnotation = (
|
|
48
|
-
ctx: Context,
|
|
49
|
-
properties: Array<
|
|
50
|
-
BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,
|
|
51
|
-
>,
|
|
52
|
-
) => {
|
|
53
|
-
const obj = babelTypes.objectTypeAnnotation(
|
|
54
|
-
properties.sort((a, b) => {
|
|
55
|
-
if (
|
|
56
|
-
a.type === 'ObjectTypeProperty' &&
|
|
57
|
-
b.type === 'ObjectTypeProperty'
|
|
58
|
-
) {
|
|
59
|
-
const aName = a.key.type === 'Identifier' ? a.key.name : '';
|
|
60
|
-
const bName = b.key.type === 'Identifier' ? b.key.name : '';
|
|
61
|
-
return aName < bName ? -1 : 1;
|
|
62
|
-
}
|
|
63
|
-
return 0;
|
|
64
|
-
}),
|
|
65
|
-
undefined /* indexers */,
|
|
66
|
-
undefined /* callProperties */,
|
|
67
|
-
undefined /* internalSlots */,
|
|
68
|
-
true /* exact */,
|
|
69
|
-
);
|
|
70
|
-
const name = ctx.path.join('_');
|
|
71
|
-
const isTopLevelType = ctx.path.length <= 1;
|
|
72
|
-
if (ctx.allObjectTypes != null && !isTopLevelType) {
|
|
73
|
-
ctx.allObjectTypes[name] = obj;
|
|
74
|
-
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
75
|
-
} else {
|
|
76
|
-
return obj;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export const generateFragmentType = (
|
|
81
|
-
schema: Schema,
|
|
82
|
-
fragment: FragmentDefinitionNode,
|
|
83
|
-
ctx: Context,
|
|
84
|
-
): string => {
|
|
85
|
-
const onType = fragment.typeCondition.name.value;
|
|
86
|
-
let ast;
|
|
87
|
-
|
|
88
|
-
if (schema.typesByName[onType]) {
|
|
89
|
-
ast = sortedObjectTypeAnnotation(
|
|
90
|
-
ctx,
|
|
91
|
-
objectPropertiesToFlow(
|
|
92
|
-
ctx,
|
|
93
|
-
schema.typesByName[onType],
|
|
94
|
-
onType,
|
|
95
|
-
fragment.selectionSet.selections,
|
|
96
|
-
),
|
|
97
|
-
);
|
|
98
|
-
} else if (schema.interfacesByName[onType]) {
|
|
99
|
-
ast = unionOrInterfaceToFlow(
|
|
100
|
-
ctx,
|
|
101
|
-
ctx.schema.interfacesByName[onType],
|
|
102
|
-
fragment.selectionSet.selections,
|
|
103
|
-
);
|
|
104
|
-
} else if (schema.unionsByName[onType]) {
|
|
105
|
-
ast = unionOrInterfaceToFlow(
|
|
106
|
-
ctx,
|
|
107
|
-
ctx.schema.unionsByName[onType],
|
|
108
|
-
fragment.selectionSet.selections,
|
|
109
|
-
);
|
|
110
|
-
} else {
|
|
111
|
-
throw new Error(`Unknown ${onType}`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
115
|
-
return generate(ast).code;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const _typeToFlow = (
|
|
119
|
-
ctx: Context,
|
|
120
|
-
type,
|
|
121
|
-
selection,
|
|
122
|
-
): babelTypes.BabelNodeFlowType => {
|
|
123
|
-
if (type.kind === 'SCALAR') {
|
|
124
|
-
return scalarTypeToFlow(ctx, type.name);
|
|
125
|
-
}
|
|
126
|
-
if (type.kind === 'LIST') {
|
|
127
|
-
return babelTypes.genericTypeAnnotation(
|
|
128
|
-
ctx.readOnlyArray
|
|
129
|
-
? babelTypes.identifier('$ReadOnlyArray')
|
|
130
|
-
: babelTypes.identifier('Array'),
|
|
131
|
-
babelTypes.typeParameterInstantiation([
|
|
132
|
-
typeToFlow(ctx, type.ofType, selection),
|
|
133
|
-
]),
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
if (type.kind === 'UNION') {
|
|
137
|
-
const union = ctx.schema.unionsByName[type.name];
|
|
138
|
-
if (!selection.selectionSet) {
|
|
139
|
-
console.log('no selection set', selection);
|
|
140
|
-
return babelTypes.anyTypeAnnotation();
|
|
141
|
-
}
|
|
142
|
-
return unionOrInterfaceToFlow(
|
|
143
|
-
ctx,
|
|
144
|
-
union,
|
|
145
|
-
selection.selectionSet.selections,
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (type.kind === 'INTERFACE') {
|
|
150
|
-
if (!selection.selectionSet) {
|
|
151
|
-
console.log('no selection set', selection);
|
|
152
|
-
return babelTypes.anyTypeAnnotation();
|
|
153
|
-
}
|
|
154
|
-
return unionOrInterfaceToFlow(
|
|
155
|
-
ctx,
|
|
156
|
-
ctx.schema.interfacesByName[type.name],
|
|
157
|
-
selection.selectionSet.selections,
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
if (type.kind === 'ENUM') {
|
|
161
|
-
return enumTypeToFlow(ctx, type.name);
|
|
162
|
-
}
|
|
163
|
-
if (type.kind !== 'OBJECT') {
|
|
164
|
-
console.log('not object', type);
|
|
165
|
-
return babelTypes.anyTypeAnnotation();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const tname = type.name;
|
|
169
|
-
if (!ctx.schema.typesByName[tname]) {
|
|
170
|
-
console.log('unknown referenced type', tname);
|
|
171
|
-
return babelTypes.anyTypeAnnotation();
|
|
172
|
-
}
|
|
173
|
-
const childType = ctx.schema.typesByName[tname];
|
|
174
|
-
if (!selection.selectionSet) {
|
|
175
|
-
console.log('no selection set', selection);
|
|
176
|
-
return babelTypes.anyTypeAnnotation();
|
|
177
|
-
}
|
|
178
|
-
return maybeAddDescriptionComment(
|
|
179
|
-
childType.description,
|
|
180
|
-
querySelectionToObjectType(
|
|
181
|
-
ctx,
|
|
182
|
-
selection.selectionSet.selections,
|
|
183
|
-
childType,
|
|
184
|
-
tname,
|
|
185
|
-
),
|
|
186
|
-
);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export const typeToFlow = (
|
|
190
|
-
ctx: Context,
|
|
191
|
-
type: IntrospectionOutputTypeRef,
|
|
192
|
-
selection: FieldNode,
|
|
193
|
-
): babelTypes.BabelNodeFlowType => {
|
|
194
|
-
// throw new Error('npoe');
|
|
195
|
-
if (type.kind === 'NON_NULL') {
|
|
196
|
-
return _typeToFlow(ctx, type.ofType, selection);
|
|
197
|
-
}
|
|
198
|
-
// If we don'babelTypes care about strict nullability checking, then pretend everything is non-null
|
|
199
|
-
if (!ctx.strictNullability) {
|
|
200
|
-
return _typeToFlow(ctx, type, selection);
|
|
201
|
-
}
|
|
202
|
-
const inner = _typeToFlow(ctx, type, selection);
|
|
203
|
-
const result = babelTypes.nullableTypeAnnotation(inner);
|
|
204
|
-
return transferLeadingComments(inner, result);
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
const ensureOnlyOneTypenameProperty = (properties) => {
|
|
208
|
-
let seenTypeName: false | string = false;
|
|
209
|
-
return properties.filter((type) => {
|
|
210
|
-
// The apollo-utilities "addTypeName" utility will add it
|
|
211
|
-
// even if it's already specified :( so we have to filter out
|
|
212
|
-
// the extra one here.
|
|
213
|
-
if (
|
|
214
|
-
type.type === 'ObjectTypeProperty' &&
|
|
215
|
-
type.key.name === '__typename'
|
|
216
|
-
) {
|
|
217
|
-
const name =
|
|
218
|
-
type.value.type === 'StringLiteralTypeAnnotation'
|
|
219
|
-
? type.value.value
|
|
220
|
-
: 'INVALID';
|
|
221
|
-
if (seenTypeName) {
|
|
222
|
-
if (name !== seenTypeName) {
|
|
223
|
-
throw new Error(
|
|
224
|
-
`Got two different type names ${name}, ${seenTypeName}`,
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
seenTypeName = name;
|
|
230
|
-
}
|
|
231
|
-
return true;
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
const querySelectionToObjectType = (
|
|
236
|
-
ctx: Context,
|
|
237
|
-
selections,
|
|
238
|
-
type,
|
|
239
|
-
typeName: string,
|
|
240
|
-
): BabelNodeFlowType => {
|
|
241
|
-
return sortedObjectTypeAnnotation(
|
|
242
|
-
ctx,
|
|
243
|
-
ensureOnlyOneTypenameProperty(
|
|
244
|
-
objectPropertiesToFlow(ctx, type, typeName, selections),
|
|
245
|
-
),
|
|
246
|
-
);
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
export const objectPropertiesToFlow = (
|
|
250
|
-
ctx: Context,
|
|
251
|
-
type: IntrospectionObjectType & {
|
|
252
|
-
fieldsByName: {[name: string]: IntrospectionField},
|
|
253
|
-
},
|
|
254
|
-
typeName: string,
|
|
255
|
-
selections: Selections,
|
|
256
|
-
): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {
|
|
257
|
-
return [].concat(
|
|
258
|
-
...selections.map((selection) => {
|
|
259
|
-
switch (selection.kind) {
|
|
260
|
-
case 'InlineFragment': {
|
|
261
|
-
const newTypeName =
|
|
262
|
-
selection.typeCondition?.name.value ?? typeName;
|
|
263
|
-
if (newTypeName !== typeName) {
|
|
264
|
-
return [];
|
|
265
|
-
}
|
|
266
|
-
return objectPropertiesToFlow(
|
|
267
|
-
ctx,
|
|
268
|
-
ctx.schema.typesByName[newTypeName],
|
|
269
|
-
newTypeName,
|
|
270
|
-
selection.selectionSet.selections,
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
case 'FragmentSpread':
|
|
274
|
-
if (!ctx.fragments[selection.name.value]) {
|
|
275
|
-
ctx.errors.push(
|
|
276
|
-
`No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`,
|
|
277
|
-
);
|
|
278
|
-
return [
|
|
279
|
-
babelTypes.objectTypeProperty(
|
|
280
|
-
babelTypes.identifier(selection.name.value),
|
|
281
|
-
babelTypes.genericTypeAnnotation(
|
|
282
|
-
babelTypes.identifier(`UNKNOWN_FRAGMENT`),
|
|
283
|
-
),
|
|
284
|
-
),
|
|
285
|
-
];
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return objectPropertiesToFlow(
|
|
289
|
-
ctx,
|
|
290
|
-
type,
|
|
291
|
-
typeName,
|
|
292
|
-
ctx.fragments[selection.name.value].selectionSet
|
|
293
|
-
.selections,
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
case 'Field':
|
|
297
|
-
const name = selection.name.value;
|
|
298
|
-
const alias: string = selection.alias
|
|
299
|
-
? selection.alias.value
|
|
300
|
-
: name;
|
|
301
|
-
if (name === '__typename') {
|
|
302
|
-
return [
|
|
303
|
-
babelTypes.objectTypeProperty(
|
|
304
|
-
babelTypes.identifier(alias),
|
|
305
|
-
babelTypes.stringLiteralTypeAnnotation(
|
|
306
|
-
typeName,
|
|
307
|
-
),
|
|
308
|
-
),
|
|
309
|
-
];
|
|
310
|
-
}
|
|
311
|
-
if (!type.fieldsByName[name]) {
|
|
312
|
-
ctx.errors.push(
|
|
313
|
-
`Unknown field '${name}' for type '${typeName}'`,
|
|
314
|
-
);
|
|
315
|
-
return babelTypes.objectTypeProperty(
|
|
316
|
-
babelTypes.identifier(alias),
|
|
317
|
-
babelTypes.genericTypeAnnotation(
|
|
318
|
-
babelTypes.identifier(
|
|
319
|
-
`UNKNOWN_FIELD["${name}"]`,
|
|
320
|
-
),
|
|
321
|
-
),
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
const typeField = type.fieldsByName[name];
|
|
325
|
-
|
|
326
|
-
return [
|
|
327
|
-
maybeAddDescriptionComment(
|
|
328
|
-
typeField.description,
|
|
329
|
-
liftLeadingPropertyComments(
|
|
330
|
-
babelTypes.objectTypeProperty(
|
|
331
|
-
babelTypes.identifier(alias),
|
|
332
|
-
typeToFlow(
|
|
333
|
-
{
|
|
334
|
-
...ctx,
|
|
335
|
-
path: ctx.path.concat([alias]),
|
|
336
|
-
},
|
|
337
|
-
typeField.type,
|
|
338
|
-
selection,
|
|
339
|
-
),
|
|
340
|
-
),
|
|
341
|
-
),
|
|
342
|
-
),
|
|
343
|
-
];
|
|
344
|
-
|
|
345
|
-
default:
|
|
346
|
-
ctx.errors.push(
|
|
347
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
348
|
-
`Unsupported selection kind '${selection.kind}'`,
|
|
349
|
-
);
|
|
350
|
-
return [];
|
|
351
|
-
}
|
|
352
|
-
}),
|
|
353
|
-
);
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
export const unionOrInterfaceToFlow = (
|
|
357
|
-
ctx: Context,
|
|
358
|
-
type:
|
|
359
|
-
| IntrospectionUnionType
|
|
360
|
-
| (IntrospectionInterfaceType & {
|
|
361
|
-
fieldsByName: {[key: string]: IntrospectionField},
|
|
362
|
-
}),
|
|
363
|
-
selections: Selections,
|
|
364
|
-
): BabelNodeFlowType => {
|
|
365
|
-
const allFields = selections.every(
|
|
366
|
-
(selection) => selection.kind === 'Field',
|
|
367
|
-
);
|
|
368
|
-
const selectedAttributes: Array<{
|
|
369
|
-
attributes: Array<
|
|
370
|
-
BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,
|
|
371
|
-
>,
|
|
372
|
-
typeName: string,
|
|
373
|
-
}> = type.possibleTypes
|
|
374
|
-
.slice()
|
|
375
|
-
.sort((a, b) => {
|
|
376
|
-
return a.name < b.name ? -1 : 1;
|
|
377
|
-
})
|
|
378
|
-
.map((possible) => {
|
|
379
|
-
const configWithUpdatedPath = {
|
|
380
|
-
...ctx,
|
|
381
|
-
path: allFields ? ctx.path : ctx.path.concat([possible.name]),
|
|
382
|
-
};
|
|
383
|
-
return {
|
|
384
|
-
typeName: possible.name,
|
|
385
|
-
attributes: ensureOnlyOneTypenameProperty(
|
|
386
|
-
selections
|
|
387
|
-
.map((selection) =>
|
|
388
|
-
unionOrInterfaceSelection(
|
|
389
|
-
configWithUpdatedPath,
|
|
390
|
-
type,
|
|
391
|
-
possible,
|
|
392
|
-
selection,
|
|
393
|
-
),
|
|
394
|
-
)
|
|
395
|
-
.flat(),
|
|
396
|
-
),
|
|
397
|
-
};
|
|
398
|
-
});
|
|
399
|
-
// If they're all fields, the only selection that could be different is __typename
|
|
400
|
-
if (allFields) {
|
|
401
|
-
const sharedAttributes = selectedAttributes[0].attributes.slice();
|
|
402
|
-
const typeNameIndex = selectedAttributes[0].attributes.findIndex(
|
|
403
|
-
(x) =>
|
|
404
|
-
x.type === 'ObjectTypeProperty' &&
|
|
405
|
-
x.key.type === 'Identifier' &&
|
|
406
|
-
x.key.name === '__typename',
|
|
407
|
-
);
|
|
408
|
-
if (typeNameIndex !== -1) {
|
|
409
|
-
sharedAttributes[typeNameIndex] = babelTypes.objectTypeProperty(
|
|
410
|
-
babelTypes.identifier('__typename'),
|
|
411
|
-
babelTypes.unionTypeAnnotation(
|
|
412
|
-
selectedAttributes.map(
|
|
413
|
-
(attrs) =>
|
|
414
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
415
|
-
((attrs.attributes[
|
|
416
|
-
typeNameIndex
|
|
417
|
-
]: any): BabelNodeObjectTypeProperty).value,
|
|
418
|
-
),
|
|
419
|
-
),
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
return sortedObjectTypeAnnotation(ctx, sharedAttributes);
|
|
423
|
-
}
|
|
424
|
-
if (selectedAttributes.length === 1) {
|
|
425
|
-
return sortedObjectTypeAnnotation(
|
|
426
|
-
ctx,
|
|
427
|
-
selectedAttributes[0].attributes,
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* When generating the objects for the sub-options of a union, the path needs
|
|
432
|
-
* to include the name of the object type.
|
|
433
|
-
* ```
|
|
434
|
-
* query getFriend {
|
|
435
|
-
* friend {
|
|
436
|
-
* ... on Human { id }
|
|
437
|
-
* ... on Droid { arms }
|
|
438
|
-
* }
|
|
439
|
-
* }
|
|
440
|
-
* ```
|
|
441
|
-
* produces
|
|
442
|
-
* ```
|
|
443
|
-
* type getFriend = {friend: getFriend_friend_Human | getFriend_friend_Droid }
|
|
444
|
-
* type getFriend_friend_Human = {id: string}
|
|
445
|
-
* type getFriend_friend_Droid = {arms: number}
|
|
446
|
-
* ```
|
|
447
|
-
* Note that this is different from when an attribute has a plain object type.
|
|
448
|
-
* ```
|
|
449
|
-
* query getHuman {
|
|
450
|
-
* me: human(id: "me") { id }
|
|
451
|
-
* }
|
|
452
|
-
* ```
|
|
453
|
-
* produces
|
|
454
|
-
* ```
|
|
455
|
-
* type getHuman = {me: getHuman_me}
|
|
456
|
-
* type getHuman_me = {id: string}
|
|
457
|
-
* ```
|
|
458
|
-
* instead of e.g. `getHuman_me_Human`.
|
|
459
|
-
*/
|
|
460
|
-
const result = babelTypes.unionTypeAnnotation(
|
|
461
|
-
selectedAttributes.map(({typeName, attributes}) =>
|
|
462
|
-
sortedObjectTypeAnnotation(
|
|
463
|
-
{...ctx, path: ctx.path.concat([typeName])},
|
|
464
|
-
attributes,
|
|
465
|
-
),
|
|
466
|
-
),
|
|
467
|
-
);
|
|
468
|
-
const name = ctx.path.join('_');
|
|
469
|
-
if (ctx.allObjectTypes && ctx.path.length > 1) {
|
|
470
|
-
ctx.allObjectTypes[name] = result;
|
|
471
|
-
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
472
|
-
}
|
|
473
|
-
return result;
|
|
474
|
-
};
|
|
475
|
-
const unionOrInterfaceSelection = (
|
|
476
|
-
config,
|
|
477
|
-
type,
|
|
478
|
-
possible,
|
|
479
|
-
selection,
|
|
480
|
-
): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {
|
|
481
|
-
if (selection.kind === 'Field' && selection.name.value === '__typename') {
|
|
482
|
-
const alias = selection.alias
|
|
483
|
-
? selection.alias.value
|
|
484
|
-
: selection.name.value;
|
|
485
|
-
return [
|
|
486
|
-
babelTypes.objectTypeProperty(
|
|
487
|
-
babelTypes.identifier(alias),
|
|
488
|
-
babelTypes.stringLiteralTypeAnnotation(possible.name),
|
|
489
|
-
),
|
|
490
|
-
];
|
|
491
|
-
}
|
|
492
|
-
if (selection.kind === 'Field' && type.kind !== 'UNION') {
|
|
493
|
-
// this is an interface
|
|
494
|
-
const name = selection.name.value;
|
|
495
|
-
const alias = selection.alias ? selection.alias.value : name;
|
|
496
|
-
if (!type.fieldsByName[name]) {
|
|
497
|
-
config.errors.push(
|
|
498
|
-
'Unknown field: ' +
|
|
499
|
-
name +
|
|
500
|
-
' on type ' +
|
|
501
|
-
type.name +
|
|
502
|
-
' for possible ' +
|
|
503
|
-
possible.name,
|
|
504
|
-
);
|
|
505
|
-
return [
|
|
506
|
-
babelTypes.objectTypeProperty(
|
|
507
|
-
babelTypes.identifier(alias),
|
|
508
|
-
babelTypes.genericTypeAnnotation(
|
|
509
|
-
babelTypes.identifier(`UNKNOWN_FIELD`),
|
|
510
|
-
),
|
|
511
|
-
),
|
|
512
|
-
];
|
|
513
|
-
}
|
|
514
|
-
const typeField = type.fieldsByName[name];
|
|
515
|
-
return [
|
|
516
|
-
liftLeadingPropertyComments(
|
|
517
|
-
babelTypes.objectTypeProperty(
|
|
518
|
-
babelTypes.identifier(alias),
|
|
519
|
-
typeToFlow(
|
|
520
|
-
{...config, path: config.path.concat([name])},
|
|
521
|
-
typeField.type,
|
|
522
|
-
selection,
|
|
523
|
-
),
|
|
524
|
-
),
|
|
525
|
-
),
|
|
526
|
-
];
|
|
527
|
-
}
|
|
528
|
-
if (selection.kind === 'FragmentSpread') {
|
|
529
|
-
const fragment = config.fragments[selection.name.value];
|
|
530
|
-
if (!fragment) {
|
|
531
|
-
throw new Error(`Unknown fragment ${selection.name.value}`);
|
|
532
|
-
}
|
|
533
|
-
const typeName = fragment.typeCondition.name.value;
|
|
534
|
-
if (
|
|
535
|
-
(config.schema.interfacesByName[typeName] &&
|
|
536
|
-
config.schema.interfacesByName[typeName].possibleTypesByName[
|
|
537
|
-
possible.name
|
|
538
|
-
]) ||
|
|
539
|
-
typeName === possible.name
|
|
540
|
-
) {
|
|
541
|
-
return [].concat(
|
|
542
|
-
...fragment.selectionSet.selections.map((selection) =>
|
|
543
|
-
unionOrInterfaceSelection(
|
|
544
|
-
config,
|
|
545
|
-
config.schema.typesByName[possible.name],
|
|
546
|
-
possible,
|
|
547
|
-
selection,
|
|
548
|
-
),
|
|
549
|
-
),
|
|
550
|
-
);
|
|
551
|
-
} else {
|
|
552
|
-
return [];
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
if (selection.kind !== 'InlineFragment') {
|
|
556
|
-
config.errors.push(
|
|
557
|
-
`union selectors must be inline fragment: found ${selection.kind}`,
|
|
558
|
-
);
|
|
559
|
-
if (type.kind === 'UNION') {
|
|
560
|
-
config.errors
|
|
561
|
-
.push(`You're trying to select a field from the union ${type.name},
|
|
562
|
-
but the only field you're allowed to select is "__typename".
|
|
563
|
-
Try using an inline fragment "... on SomeType {}".`);
|
|
564
|
-
}
|
|
565
|
-
return [];
|
|
566
|
-
}
|
|
567
|
-
if (selection.typeCondition) {
|
|
568
|
-
const typeName = selection.typeCondition.name.value;
|
|
569
|
-
const indirectMatch =
|
|
570
|
-
config.schema.interfacesByName[typeName]?.possibleTypesByName[
|
|
571
|
-
possible.name
|
|
572
|
-
];
|
|
573
|
-
if (typeName !== possible.name && !indirectMatch) {
|
|
574
|
-
return [];
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
return objectPropertiesToFlow(
|
|
578
|
-
config,
|
|
579
|
-
config.schema.typesByName[possible.name],
|
|
580
|
-
possible.name,
|
|
581
|
-
selection.selectionSet.selections,
|
|
582
|
-
);
|
|
583
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generateResponseType.js"],"names":["generateResponseType","schema","query","ctx","ast","querySelectionToObjectType","selectionSet","selections","operation","typesByName","Mutation","Query","code","sortedObjectTypeAnnotation","properties","obj","babelTypes","objectTypeAnnotation","sort","a","b","type","aName","key","name","bName","undefined","path","join","isTopLevelType","length","allObjectTypes","genericTypeAnnotation","identifier","generateFragmentType","fragment","onType","typeCondition","value","objectPropertiesToFlow","interfacesByName","unionOrInterfaceToFlow","unionsByName","Error","_typeToFlow","selection","kind","readOnlyArray","typeParameterInstantiation","typeToFlow","ofType","union","console","log","anyTypeAnnotation","tname","childType","description","strictNullability","inner","result","nullableTypeAnnotation","ensureOnlyOneTypenameProperty","seenTypeName","filter","typeName","concat","map","newTypeName","fragments","errors","push","objectTypeProperty","alias","stringLiteralTypeAnnotation","fieldsByName","typeField","allFields","every","selectedAttributes","possibleTypes","slice","possible","configWithUpdatedPath","attributes","unionOrInterfaceSelection","flat","sharedAttributes","typeNameIndex","findIndex","x","unionTypeAnnotation","attrs","config","possibleTypesByName","indirectMatch"],"mappings":";;;;;;;AAEA;;AACA;;AASA;;AAKA;;;;;;;;AAhBA;AACyC;AA2BlC,MAAMA,oBAAoB,GAAG,CAChCC,MADgC,EAEhCC,KAFgC,EAGhCC,GAHgC,KAIvB;AACT,QAAMC,GAAG,GAAGC,0BAA0B,CAClCF,GADkC,EAElCD,KAAK,CAACI,YAAN,CAAmBC,UAFe,EAGlCL,KAAK,CAACM,SAAN,KAAoB,UAApB,GACMP,MAAM,CAACQ,WAAP,CAAmBC,QADzB,GAEMT,MAAM,CAACQ,WAAP,CAAmBE,KALS,EAMlCT,KAAK,CAACM,SAAN,KAAoB,UAApB,GAAiC,UAAjC,GAA8C,OANZ,CAAtC,CADS,CAST;;AACA,SAAO,wBAASJ,GAAT,EAAcQ,IAArB;AACH,CAfM;;;;AAiBP,MAAMC,0BAA0B,GAAG,CAC/BV,GAD+B,EAE/BW,UAF+B,KAK9B;AACD,QAAMC,GAAG,GAAGC,UAAU,CAACC,oBAAX,CACRH,UAAU,CAACI,IAAX,CAAgB,CAACC,CAAD,EAAIC,CAAJ,KAAU;AACtB,QACID,CAAC,CAACE,IAAF,KAAW,oBAAX,IACAD,CAAC,CAACC,IAAF,KAAW,oBAFf,EAGE;AACE,YAAMC,KAAK,GAAGH,CAAC,CAACI,GAAF,CAAMF,IAAN,KAAe,YAAf,GAA8BF,CAAC,CAACI,GAAF,CAAMC,IAApC,GAA2C,EAAzD;AACA,YAAMC,KAAK,GAAGL,CAAC,CAACG,GAAF,CAAMF,IAAN,KAAe,YAAf,GAA8BD,CAAC,CAACG,GAAF,CAAMC,IAApC,GAA2C,EAAzD;AACA,aAAOF,KAAK,GAAGG,KAAR,GAAgB,CAAC,CAAjB,GAAqB,C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AAS,CAACR,aAAV,CAAwBb,IAAxB,CAA6Bc,KAA9C;AACA,UAAMyD,aAAa,4BACfF,MAAM,CAAC5F,MAAP,CAAcuC,gBAAd,CAA+ByB,QAA/B,CADe,0DACf,sBAA0C6B,mBAA1C,CACIZ,QAAQ,CAAC1D,IADb,CADJ;;AAIA,QAAIyC,QAAQ,KAAKiB,QAAQ,CAAC1D,IAAtB,IAA8B,CAACuE,aAAnC,EAAkD;AAC9C,aAAO,EAAP;AACH;AACJ;;AACD,SAAOxD,sBAAsB,CACzBsD,MADyB,EAEzBA,MAAM,CAAC5F,MAAP,CAAcQ,WAAd,CAA0ByE,QAAQ,CAAC1D,IAAnC,CAFyB,EAGzB0D,QAAQ,CAAC1D,IAHgB,EAIzBqB,SAAS,CAACvC,YAAV,CAAuBC,UAJE,CAA7B;AAMH,CA5GD","sourcesContent":["// @flow\n/* eslint-disable no-console */\nimport generate from '@babel/generator'; // eslint-disable-line flowtype-errors/uncovered\nimport * as babelTypes from '@babel/types';\nimport {type BabelNodeFlowType} from '@babel/types';\nimport type {\n FieldNode,\n IntrospectionOutputTypeRef,\n OperationDefinitionNode,\n FragmentDefinitionNode,\n} from 'graphql';\nimport type {Context, Schema, Selections} from './types';\nimport {\n liftLeadingPropertyComments,\n maybeAddDescriptionComment,\n transferLeadingComments,\n} from './utils';\nimport {enumTypeToFlow, scalarTypeToFlow} from './enums';\nimport type {\n IntrospectionField,\n IntrospectionInterfaceType,\n IntrospectionObjectType,\n IntrospectionUnionType,\n} from 'graphql/utilities/introspectionQuery';\nimport {\n BabelNodeObjectTypeProperty,\n BabelNodeObjectTypeSpreadProperty,\n} from '@babel/types';\n\nexport const generateResponseType = (\n schema: Schema,\n query: OperationDefinitionNode,\n ctx: Context,\n): string => {\n const ast = querySelectionToObjectType(\n ctx,\n query.selectionSet.selections,\n query.operation === 'mutation'\n ? schema.typesByName.Mutation\n : schema.typesByName.Query,\n query.operation === 'mutation' ? 'mutation' : 'query',\n );\n // eslint-disable-next-line flowtype-errors/uncovered\n return generate(ast).code;\n};\n\nconst sortedObjectTypeAnnotation = (\n ctx: Context,\n properties: Array<\n BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,\n >,\n) => {\n const obj = babelTypes.objectTypeAnnotation(\n properties.sort((a, b) => {\n if (\n a.type === 'ObjectTypeProperty' &&\n b.type === 'ObjectTypeProperty'\n ) {\n const aName = a.key.type === 'Identifier' ? a.key.name : '';\n const bName = b.key.type === 'Identifier' ? b.key.name : '';\n return aName < bName ? -1 : 1;\n }\n return 0;\n }),\n undefined /* indexers */,\n undefined /* callProperties */,\n undefined /* internalSlots */,\n true /* exact */,\n );\n const name = ctx.path.join('_');\n const isTopLevelType = ctx.path.length <= 1;\n if (ctx.allObjectTypes != null && !isTopLevelType) {\n ctx.allObjectTypes[name] = obj;\n return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));\n } else {\n return obj;\n }\n};\n\nexport const generateFragmentType = (\n schema: Schema,\n fragment: FragmentDefinitionNode,\n ctx: Context,\n): string => {\n const onType = fragment.typeCondition.name.value;\n let ast;\n\n if (schema.typesByName[onType]) {\n ast = sortedObjectTypeAnnotation(\n ctx,\n objectPropertiesToFlow(\n ctx,\n schema.typesByName[onType],\n onType,\n fragment.selectionSet.selections,\n ),\n );\n } else if (schema.interfacesByName[onType]) {\n ast = unionOrInterfaceToFlow(\n ctx,\n ctx.schema.interfacesByName[onType],\n fragment.selectionSet.selections,\n );\n } else if (schema.unionsByName[onType]) {\n ast = unionOrInterfaceToFlow(\n ctx,\n ctx.schema.unionsByName[onType],\n fragment.selectionSet.selections,\n );\n } else {\n throw new Error(`Unknown ${onType}`);\n }\n\n // eslint-disable-next-line flowtype-errors/uncovered\n return generate(ast).code;\n};\n\nconst _typeToFlow = (\n ctx: Context,\n type,\n selection,\n): babelTypes.BabelNodeFlowType => {\n if (type.kind === 'SCALAR') {\n return scalarTypeToFlow(ctx, type.name);\n }\n if (type.kind === 'LIST') {\n return babelTypes.genericTypeAnnotation(\n ctx.readOnlyArray\n ? babelTypes.identifier('$ReadOnlyArray')\n : babelTypes.identifier('Array'),\n babelTypes.typeParameterInstantiation([\n typeToFlow(ctx, type.ofType, selection),\n ]),\n );\n }\n if (type.kind === 'UNION') {\n const union = ctx.schema.unionsByName[type.name];\n if (!selection.selectionSet) {\n console.log('no selection set', selection);\n return babelTypes.anyTypeAnnotation();\n }\n return unionOrInterfaceToFlow(\n ctx,\n union,\n selection.selectionSet.selections,\n );\n }\n\n if (type.kind === 'INTERFACE') {\n if (!selection.selectionSet) {\n console.log('no selection set', selection);\n return babelTypes.anyTypeAnnotation();\n }\n return unionOrInterfaceToFlow(\n ctx,\n ctx.schema.interfacesByName[type.name],\n selection.selectionSet.selections,\n );\n }\n if (type.kind === 'ENUM') {\n return enumTypeToFlow(ctx, type.name);\n }\n if (type.kind !== 'OBJECT') {\n console.log('not object', type);\n return babelTypes.anyTypeAnnotation();\n }\n\n const tname = type.name;\n if (!ctx.schema.typesByName[tname]) {\n console.log('unknown referenced type', tname);\n return babelTypes.anyTypeAnnotation();\n }\n const childType = ctx.schema.typesByName[tname];\n if (!selection.selectionSet) {\n console.log('no selection set', selection);\n return babelTypes.anyTypeAnnotation();\n }\n return maybeAddDescriptionComment(\n childType.description,\n querySelectionToObjectType(\n ctx,\n selection.selectionSet.selections,\n childType,\n tname,\n ),\n );\n};\n\nexport const typeToFlow = (\n ctx: Context,\n type: IntrospectionOutputTypeRef,\n selection: FieldNode,\n): babelTypes.BabelNodeFlowType => {\n // throw new Error('npoe');\n if (type.kind === 'NON_NULL') {\n return _typeToFlow(ctx, type.ofType, selection);\n }\n // If we don'babelTypes care about strict nullability checking, then pretend everything is non-null\n if (!ctx.strictNullability) {\n return _typeToFlow(ctx, type, selection);\n }\n const inner = _typeToFlow(ctx, type, selection);\n const result = babelTypes.nullableTypeAnnotation(inner);\n return transferLeadingComments(inner, result);\n};\n\nconst ensureOnlyOneTypenameProperty = (properties) => {\n let seenTypeName: false | string = false;\n return properties.filter((type) => {\n // The apollo-utilities \"addTypeName\" utility will add it\n // even if it's already specified :( so we have to filter out\n // the extra one here.\n if (\n type.type === 'ObjectTypeProperty' &&\n type.key.name === '__typename'\n ) {\n const name =\n type.value.type === 'StringLiteralTypeAnnotation'\n ? type.value.value\n : 'INVALID';\n if (seenTypeName) {\n if (name !== seenTypeName) {\n throw new Error(\n `Got two different type names ${name}, ${seenTypeName}`,\n );\n }\n return false;\n }\n seenTypeName = name;\n }\n return true;\n });\n};\n\nconst querySelectionToObjectType = (\n ctx: Context,\n selections,\n type,\n typeName: string,\n): BabelNodeFlowType => {\n return sortedObjectTypeAnnotation(\n ctx,\n ensureOnlyOneTypenameProperty(\n objectPropertiesToFlow(ctx, type, typeName, selections),\n ),\n );\n};\n\nexport const objectPropertiesToFlow = (\n ctx: Context,\n type: IntrospectionObjectType & {\n fieldsByName: {[name: string]: IntrospectionField},\n },\n typeName: string,\n selections: Selections,\n): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {\n return [].concat(\n ...selections.map((selection) => {\n switch (selection.kind) {\n case 'InlineFragment': {\n const newTypeName =\n selection.typeCondition?.name.value ?? typeName;\n if (newTypeName !== typeName) {\n return [];\n }\n return objectPropertiesToFlow(\n ctx,\n ctx.schema.typesByName[newTypeName],\n newTypeName,\n selection.selectionSet.selections,\n );\n }\n case 'FragmentSpread':\n if (!ctx.fragments[selection.name.value]) {\n ctx.errors.push(\n `No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`,\n );\n return [\n babelTypes.objectTypeProperty(\n babelTypes.identifier(selection.name.value),\n babelTypes.genericTypeAnnotation(\n babelTypes.identifier(`UNKNOWN_FRAGMENT`),\n ),\n ),\n ];\n }\n\n return objectPropertiesToFlow(\n ctx,\n type,\n typeName,\n ctx.fragments[selection.name.value].selectionSet\n .selections,\n );\n\n case 'Field':\n const name = selection.name.value;\n const alias: string = selection.alias\n ? selection.alias.value\n : name;\n if (name === '__typename') {\n return [\n babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n babelTypes.stringLiteralTypeAnnotation(\n typeName,\n ),\n ),\n ];\n }\n if (!type.fieldsByName[name]) {\n ctx.errors.push(\n `Unknown field '${name}' for type '${typeName}'`,\n );\n return babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n babelTypes.genericTypeAnnotation(\n babelTypes.identifier(\n `UNKNOWN_FIELD[\"${name}\"]`,\n ),\n ),\n );\n }\n const typeField = type.fieldsByName[name];\n\n return [\n maybeAddDescriptionComment(\n typeField.description,\n liftLeadingPropertyComments(\n babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n typeToFlow(\n {\n ...ctx,\n path: ctx.path.concat([alias]),\n },\n typeField.type,\n selection,\n ),\n ),\n ),\n ),\n ];\n\n default:\n ctx.errors.push(\n // eslint-disable-next-line flowtype-errors/uncovered\n `Unsupported selection kind '${selection.kind}'`,\n );\n return [];\n }\n }),\n );\n};\n\nexport const unionOrInterfaceToFlow = (\n ctx: Context,\n type:\n | IntrospectionUnionType\n | (IntrospectionInterfaceType & {\n fieldsByName: {[key: string]: IntrospectionField},\n }),\n selections: Selections,\n): BabelNodeFlowType => {\n const allFields = selections.every(\n (selection) => selection.kind === 'Field',\n );\n const selectedAttributes: Array<{\n attributes: Array<\n BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,\n >,\n typeName: string,\n }> = type.possibleTypes\n .slice()\n .sort((a, b) => {\n return a.name < b.name ? -1 : 1;\n })\n .map((possible) => {\n const configWithUpdatedPath = {\n ...ctx,\n path: allFields ? ctx.path : ctx.path.concat([possible.name]),\n };\n return {\n typeName: possible.name,\n attributes: ensureOnlyOneTypenameProperty(\n selections\n .map((selection) =>\n unionOrInterfaceSelection(\n configWithUpdatedPath,\n type,\n possible,\n selection,\n ),\n )\n .flat(),\n ),\n };\n });\n // If they're all fields, the only selection that could be different is __typename\n if (allFields) {\n const sharedAttributes = selectedAttributes[0].attributes.slice();\n const typeNameIndex = selectedAttributes[0].attributes.findIndex(\n (x) =>\n x.type === 'ObjectTypeProperty' &&\n x.key.type === 'Identifier' &&\n x.key.name === '__typename',\n );\n if (typeNameIndex !== -1) {\n sharedAttributes[typeNameIndex] = babelTypes.objectTypeProperty(\n babelTypes.identifier('__typename'),\n babelTypes.unionTypeAnnotation(\n selectedAttributes.map(\n (attrs) =>\n // eslint-disable-next-line flowtype-errors/uncovered\n ((attrs.attributes[\n typeNameIndex\n ]: any): BabelNodeObjectTypeProperty).value,\n ),\n ),\n );\n }\n return sortedObjectTypeAnnotation(ctx, sharedAttributes);\n }\n if (selectedAttributes.length === 1) {\n return sortedObjectTypeAnnotation(\n ctx,\n selectedAttributes[0].attributes,\n );\n }\n /**\n * When generating the objects for the sub-options of a union, the path needs\n * to include the name of the object type.\n * ```\n * query getFriend {\n * friend {\n * ... on Human { id }\n * ... on Droid { arms }\n * }\n * }\n * ```\n * produces\n * ```\n * type getFriend = {friend: getFriend_friend_Human | getFriend_friend_Droid }\n * type getFriend_friend_Human = {id: string}\n * type getFriend_friend_Droid = {arms: number}\n * ```\n * Note that this is different from when an attribute has a plain object type.\n * ```\n * query getHuman {\n * me: human(id: \"me\") { id }\n * }\n * ```\n * produces\n * ```\n * type getHuman = {me: getHuman_me}\n * type getHuman_me = {id: string}\n * ```\n * instead of e.g. `getHuman_me_Human`.\n */\n const result = babelTypes.unionTypeAnnotation(\n selectedAttributes.map(({typeName, attributes}) =>\n sortedObjectTypeAnnotation(\n {...ctx, path: ctx.path.concat([typeName])},\n attributes,\n ),\n ),\n );\n const name = ctx.path.join('_');\n if (ctx.allObjectTypes && ctx.path.length > 1) {\n ctx.allObjectTypes[name] = result;\n return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));\n }\n return result;\n};\nconst unionOrInterfaceSelection = (\n config,\n type,\n possible,\n selection,\n): Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty> => {\n if (selection.kind === 'Field' && selection.name.value === '__typename') {\n const alias = selection.alias\n ? selection.alias.value\n : selection.name.value;\n return [\n babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n babelTypes.stringLiteralTypeAnnotation(possible.name),\n ),\n ];\n }\n if (selection.kind === 'Field' && type.kind !== 'UNION') {\n // this is an interface\n const name = selection.name.value;\n const alias = selection.alias ? selection.alias.value : name;\n if (!type.fieldsByName[name]) {\n config.errors.push(\n 'Unknown field: ' +\n name +\n ' on type ' +\n type.name +\n ' for possible ' +\n possible.name,\n );\n return [\n babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n babelTypes.genericTypeAnnotation(\n babelTypes.identifier(`UNKNOWN_FIELD`),\n ),\n ),\n ];\n }\n const typeField = type.fieldsByName[name];\n return [\n liftLeadingPropertyComments(\n babelTypes.objectTypeProperty(\n babelTypes.identifier(alias),\n typeToFlow(\n {...config, path: config.path.concat([name])},\n typeField.type,\n selection,\n ),\n ),\n ),\n ];\n }\n if (selection.kind === 'FragmentSpread') {\n const fragment = config.fragments[selection.name.value];\n if (!fragment) {\n throw new Error(`Unknown fragment ${selection.name.value}`);\n }\n const typeName = fragment.typeCondition.name.value;\n if (\n (config.schema.interfacesByName[typeName] &&\n config.schema.interfacesByName[typeName].possibleTypesByName[\n possible.name\n ]) ||\n typeName === possible.name\n ) {\n return [].concat(\n ...fragment.selectionSet.selections.map((selection) =>\n unionOrInterfaceSelection(\n config,\n config.schema.typesByName[possible.name],\n possible,\n selection,\n ),\n ),\n );\n } else {\n return [];\n }\n }\n if (selection.kind !== 'InlineFragment') {\n config.errors.push(\n `union selectors must be inline fragment: found ${selection.kind}`,\n );\n if (type.kind === 'UNION') {\n config.errors\n .push(`You're trying to select a field from the union ${type.name},\nbut the only field you're allowed to select is \"__typename\".\nTry using an inline fragment \"... on SomeType {}\".`);\n }\n return [];\n }\n if (selection.typeCondition) {\n const typeName = selection.typeCondition.name.value;\n const indirectMatch =\n config.schema.interfacesByName[typeName]?.possibleTypesByName[\n possible.name\n ];\n if (typeName !== possible.name && !indirectMatch) {\n return [];\n }\n }\n return objectPropertiesToFlow(\n config,\n config.schema.typesByName[possible.name],\n possible.name,\n selection.selectionSet.selections,\n );\n};\n"],"file":"generateResponseType.js"}
|