@khanacademy/graphql-flow 0.1.0 → 0.2.2

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.
@@ -7,6 +7,7 @@ import type {
7
7
  FieldNode,
8
8
  IntrospectionOutputTypeRef,
9
9
  OperationDefinitionNode,
10
+ FragmentDefinitionNode,
10
11
  } from 'graphql';
11
12
  import type {Config, Schema, Selections} from './types';
12
13
  import {
@@ -21,7 +22,7 @@ import type {
21
22
  IntrospectionObjectType,
22
23
  IntrospectionUnionType,
23
24
  } from 'graphql/utilities/introspectionQuery';
24
- import type {
25
+ import {
25
26
  BabelNodeObjectTypeProperty,
26
27
  BabelNodeObjectTypeSpreadProperty,
27
28
  } from '@babel/types';
@@ -43,6 +44,77 @@ export const generateResponseType = (
43
44
  return generate(ast).code;
44
45
  };
45
46
 
47
+ const sortedObjectTypeAnnotation = (
48
+ config: Config,
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 = config.path.join('_');
71
+ const isTopLevelType = config.path.length <= 1;
72
+ if (config.allObjectTypes != null && !isTopLevelType) {
73
+ config.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
+ config: Config,
84
+ ): string => {
85
+ const onType = fragment.typeCondition.name.value;
86
+ let ast;
87
+
88
+ if (schema.typesByName[onType]) {
89
+ ast = sortedObjectTypeAnnotation(
90
+ config,
91
+ objectPropertiesToFlow(
92
+ config,
93
+ schema.typesByName[onType],
94
+ onType,
95
+ fragment.selectionSet.selections,
96
+ ),
97
+ );
98
+ } else if (schema.interfacesByName[onType]) {
99
+ ast = unionOrInterfaceToFlow(
100
+ config,
101
+ config.schema.interfacesByName[onType],
102
+ fragment.selectionSet.selections,
103
+ );
104
+ } else if (schema.unionsByName[onType]) {
105
+ ast = unionOrInterfaceToFlow(
106
+ config,
107
+ config.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
+
46
118
  const _typeToFlow = (
47
119
  config: Config,
48
120
  type,
@@ -132,35 +204,45 @@ export const typeToFlow = (
132
204
  return transferLeadingComments(inner, result);
133
205
  };
134
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
+
135
235
  const querySelectionToObjectType = (
136
236
  config: Config,
137
237
  selections,
138
238
  type,
139
239
  typeName: string,
140
240
  ): BabelNodeFlowType => {
141
- let seenTypeName = false;
142
- return babelTypes.objectTypeAnnotation(
143
- objectPropertiesToFlow(config, type, typeName, selections).filter(
144
- (type) => {
145
- // The apollo-utilities "addTypeName" utility will add it
146
- // even if it's already specified :( so we have to filter out
147
- // the extra one here.
148
- if (
149
- type.type === 'ObjectTypeProperty' &&
150
- type.key.name === '__typename'
151
- ) {
152
- if (seenTypeName) {
153
- return false;
154
- }
155
- seenTypeName = true;
156
- }
157
- return true;
158
- },
241
+ return sortedObjectTypeAnnotation(
242
+ config,
243
+ ensureOnlyOneTypenameProperty(
244
+ objectPropertiesToFlow(config, type, typeName, selections),
159
245
  ),
160
- undefined /* indexers */,
161
- undefined /* callProperties */,
162
- undefined /* internalSlots */,
163
- true /* exact */,
164
246
  );
165
247
  };
166
248
 
@@ -178,6 +260,9 @@ export const objectPropertiesToFlow = (
178
260
  case 'InlineFragment': {
179
261
  const newTypeName =
180
262
  selection.typeCondition?.name.value ?? typeName;
263
+ if (newTypeName !== typeName) {
264
+ return [];
265
+ }
181
266
  return objectPropertiesToFlow(
182
267
  config,
183
268
  config.schema.typesByName[newTypeName],
@@ -245,7 +330,10 @@ export const objectPropertiesToFlow = (
245
330
  babelTypes.objectTypeProperty(
246
331
  babelTypes.identifier(alias),
247
332
  typeToFlow(
248
- config,
333
+ {
334
+ ...config,
335
+ path: config.path.concat([alias]),
336
+ },
249
337
  typeField.type,
250
338
  selection,
251
339
  ),
@@ -274,54 +362,117 @@ export const unionOrInterfaceToFlow = (
274
362
  }),
275
363
  selections: Selections,
276
364
  ): BabelNodeFlowType => {
277
- const selectedAttributes: Array<
278
- Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty>,
279
- > = type.possibleTypes.map((possible) => {
280
- let seenTypeName = false;
281
- return selections
282
- .map((selection) =>
283
- unionOrInterfaceSelection(config, type, possible, selection),
284
- )
285
- .flat()
286
- .filter((type) => {
287
- // The apollo-utilities "addTypeName" utility will add it
288
- // even if it's already specified :( so we have to filter out
289
- // the extra one here.
290
- if (
291
- type.type === 'ObjectTypeProperty' &&
292
- type.key.name === '__typename'
293
- ) {
294
- if (seenTypeName) {
295
- return false;
296
- }
297
- seenTypeName = true;
298
- }
299
- return true;
300
- });
301
- });
302
365
  const allFields = selections.every(
303
366
  (selection) => selection.kind === 'Field',
304
367
  );
305
- if (selectedAttributes.length === 1 || allFields) {
306
- return babelTypes.objectTypeAnnotation(
307
- selectedAttributes[0],
308
- undefined /* indexers */,
309
- undefined /* callProperties */,
310
- undefined /* internalSlots */,
311
- true /* exact */,
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
+ ...config,
381
+ path: allFields
382
+ ? config.path
383
+ : config.path.concat([possible.name]),
384
+ };
385
+ return {
386
+ typeName: possible.name,
387
+ attributes: ensureOnlyOneTypenameProperty(
388
+ selections
389
+ .map((selection) =>
390
+ unionOrInterfaceSelection(
391
+ configWithUpdatedPath,
392
+ type,
393
+ possible,
394
+ selection,
395
+ ),
396
+ )
397
+ .flat(),
398
+ ),
399
+ };
400
+ });
401
+ // If they're all fields, the only selection that could be different is __typename
402
+ if (allFields) {
403
+ const sharedAttributes = selectedAttributes[0].attributes.slice();
404
+ const typeNameIndex = selectedAttributes[0].attributes.findIndex(
405
+ (x) =>
406
+ x.type === 'ObjectTypeProperty' &&
407
+ x.key.type === 'Identifier' &&
408
+ x.key.name === '__typename',
312
409
  );
410
+ if (typeNameIndex !== -1) {
411
+ sharedAttributes[typeNameIndex] = babelTypes.objectTypeProperty(
412
+ babelTypes.identifier('__typename'),
413
+ babelTypes.unionTypeAnnotation(
414
+ selectedAttributes.map(
415
+ (attrs) =>
416
+ // eslint-disable-next-line flowtype-errors/uncovered
417
+ ((attrs.attributes[
418
+ typeNameIndex
419
+ ]: any): BabelNodeObjectTypeProperty).value,
420
+ ),
421
+ ),
422
+ );
423
+ }
424
+ return sortedObjectTypeAnnotation(config, sharedAttributes);
313
425
  }
314
- return babelTypes.unionTypeAnnotation(
315
- selectedAttributes.map((properties) =>
316
- babelTypes.objectTypeAnnotation(
317
- properties,
318
- undefined /* indexers */,
319
- undefined /* callProperties */,
320
- undefined /* internalSlots */,
321
- true /* exact */,
426
+ if (selectedAttributes.length === 1) {
427
+ return sortedObjectTypeAnnotation(
428
+ config,
429
+ selectedAttributes[0].attributes,
430
+ );
431
+ }
432
+ /**
433
+ * When generating the objects for the sub-options of a union, the path needs
434
+ * to include the name of the object type.
435
+ * ```
436
+ * query getFriend {
437
+ * friend {
438
+ * ... on Human { id }
439
+ * ... on Droid { arms }
440
+ * }
441
+ * }
442
+ * ```
443
+ * produces
444
+ * ```
445
+ * type getFriend = {friend: getFriend_friend_Human | getFriend_friend_Droid }
446
+ * type getFriend_friend_Human = {id: string}
447
+ * type getFriend_friend_Droid = {arms: number}
448
+ * ```
449
+ * Note that this is different from when an attribute has a plain object type.
450
+ * ```
451
+ * query getHuman {
452
+ * me: human(id: "me") { id }
453
+ * }
454
+ * ```
455
+ * produces
456
+ * ```
457
+ * type getHuman = {me: getHuman_me}
458
+ * type getHuman_me = {id: string}
459
+ * ```
460
+ * instead of e.g. `getHuman_me_Human`.
461
+ */
462
+ const result = babelTypes.unionTypeAnnotation(
463
+ selectedAttributes.map(({typeName, attributes}) =>
464
+ sortedObjectTypeAnnotation(
465
+ {...config, path: config.path.concat([typeName])},
466
+ attributes,
322
467
  ),
323
468
  ),
324
469
  );
470
+ const name = config.path.join('_');
471
+ if (config.allObjectTypes && config.path.length > 1) {
472
+ config.allObjectTypes[name] = result;
473
+ return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
474
+ }
475
+ return result;
325
476
  };
326
477
  const unionOrInterfaceSelection = (
327
478
  config,
@@ -367,13 +518,20 @@ const unionOrInterfaceSelection = (
367
518
  liftLeadingPropertyComments(
368
519
  babelTypes.objectTypeProperty(
369
520
  babelTypes.identifier(alias),
370
- typeToFlow(config, typeField.type, selection),
521
+ typeToFlow(
522
+ {...config, path: config.path.concat([name])},
523
+ typeField.type,
524
+ selection,
525
+ ),
371
526
  ),
372
527
  ),
373
528
  ];
374
529
  }
375
530
  if (selection.kind === 'FragmentSpread') {
376
531
  const fragment = config.fragments[selection.name.value];
532
+ if (!fragment) {
533
+ throw new Error(`Unknown fragment ${selection.name.value}`);
534
+ }
377
535
  const typeName = fragment.typeCondition.name.value;
378
536
  if (
379
537
  (config.schema.interfacesByName[typeName] &&
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/generateResponseType.js"],"names":["generateResponseType","schema","query","config","ast","querySelectionToObjectType","selectionSet","selections","operation","typesByName","Mutation","Query","code","_typeToFlow","type","selection","kind","name","babelTypes","genericTypeAnnotation","readOnlyArray","identifier","typeParameterInstantiation","typeToFlow","ofType","union","unionsByName","console","log","anyTypeAnnotation","unionOrInterfaceToFlow","interfacesByName","tname","childType","description","strictNullability","inner","result","nullableTypeAnnotation","typeName","seenTypeName","objectTypeAnnotation","objectPropertiesToFlow","filter","key","undefined","concat","map","newTypeName","typeCondition","value","fragments","errors","push","objectTypeProperty","alias","stringLiteralTypeAnnotation","fieldsByName","typeField","selectedAttributes","possibleTypes","possible","unionOrInterfaceSelection","flat","allFields","every","length","unionTypeAnnotation","properties","fragment","possibleTypesByName","indirectMatch"],"mappings":";;;;;;;AAEA;;AACA;;AAQA;;AAKA;;;;;;;;AAfA;AACyC;AA0BlC,MAAMA,oBAAoB,GAAG,CAChCC,MADgC,EAEhCC,KAFgC,EAGhCC,MAHgC,KAIvB;AACT,QAAMC,GAAG,GAAGC,0BAA0B,CAClCF,MADkC,EAElCD,KAAK,CAACI,YAAN,CAAmBC,UAFe,EAGlCL,KAAK,CAACM,SAAN,KAAoB,UAApB,GACMP,MAAM,CAACQ,WAAP,CAAm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e,0DACf,sBAA0C+B,mBAA1C,CACIT,QAAQ,CAAC5C,IADb,CADJ;;AAIA,QAAIsB,QAAQ,KAAKsB,QAAQ,CAAC5C,IAAtB,IAA8B,CAACsD,aAAnC,EAAkD;AAC9C,aAAO,EAAP;AACH;AACJ;;AACD,SAAO7B,sBAAsB,CACzBvC,MADyB,EAEzBA,MAAM,CAACF,MAAP,CAAcQ,WAAd,CAA0BoD,QAAQ,CAAC5C,IAAnC,CAFyB,EAGzB4C,QAAQ,CAAC5C,IAHgB,EAIzBF,SAAS,CAACT,YAAV,CAAuBC,UAJE,CAA7B;AAMH,CArGD","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} from 'graphql';\nimport type {Config, 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 type {\n BabelNodeObjectTypeProperty,\n BabelNodeObjectTypeSpreadProperty,\n} from '@babel/types';\n\nexport const generateResponseType = (\n schema: Schema,\n query: OperationDefinitionNode,\n config: Config,\n): string => {\n const ast = querySelectionToObjectType(\n config,\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 _typeToFlow = (\n config: Config,\n type,\n selection,\n): babelTypes.BabelNodeFlowType => {\n if (type.kind === 'SCALAR') {\n return scalarTypeToFlow(config, type.name);\n }\n if (type.kind === 'LIST') {\n return babelTypes.genericTypeAnnotation(\n config.readOnlyArray\n ? babelTypes.identifier('$ReadOnlyArray')\n : babelTypes.identifier('Array'),\n babelTypes.typeParameterInstantiation([\n typeToFlow(config, type.ofType, selection),\n ]),\n );\n }\n if (type.kind === 'UNION') {\n const union = config.schema.unionsByName[type.name];\n if (!selection.selectionSet) {\n console.log('no selection set', selection);\n return babelTypes.anyTypeAnnotation();\n }\n return unionOrInterfaceToFlow(\n config,\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 config,\n config.schema.interfacesByName[type.name],\n selection.selectionSet.selections,\n );\n }\n if (type.kind === 'ENUM') {\n return enumTypeToFlow(config, 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 (!config.schema.typesByName[tname]) {\n console.log('unknown referenced type', tname);\n return babelTypes.anyTypeAnnotation();\n }\n const childType = config.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 config,\n selection.selectionSet.selections,\n childType,\n tname,\n ),\n );\n};\n\nexport const typeToFlow = (\n config: Config,\n type: IntrospectionOutputTypeRef,\n selection: FieldNode,\n): babelTypes.BabelNodeFlowType => {\n // throw new Error('npoe');\n if (type.kind === 'NON_NULL') {\n return _typeToFlow(config, type.ofType, selection);\n }\n // If we don'babelTypes care about strict nullability checking, then pretend everything is non-null\n if (!config.strictNullability) {\n return _typeToFlow(config, type, selection);\n }\n const inner = _typeToFlow(config, type, selection);\n const result = babelTypes.nullableTypeAnnotation(inner);\n return transferLeadingComments(inner, result);\n};\n\nconst querySelectionToObjectType = (\n config: Config,\n selections,\n type,\n typeName: string,\n): BabelNodeFlowType => {\n let seenTypeName = false;\n return babelTypes.objectTypeAnnotation(\n objectPropertiesToFlow(config, type, typeName, selections).filter(\n (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 if (seenTypeName) {\n return false;\n }\n seenTypeName = true;\n }\n return true;\n },\n ),\n undefined /* indexers */,\n undefined /* callProperties */,\n undefined /* internalSlots */,\n true /* exact */,\n );\n};\n\nexport const objectPropertiesToFlow = (\n config: Config,\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 return objectPropertiesToFlow(\n config,\n config.schema.typesByName[newTypeName],\n newTypeName,\n selection.selectionSet.selections,\n );\n }\n case 'FragmentSpread':\n if (!config.fragments[selection.name.value]) {\n config.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 config,\n type,\n typeName,\n config.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 config.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 config,\n typeField.type,\n selection,\n ),\n ),\n ),\n ),\n ];\n\n default:\n config.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 config: Config,\n type:\n | IntrospectionUnionType\n | (IntrospectionInterfaceType & {\n fieldsByName: {[key: string]: IntrospectionField},\n }),\n selections: Selections,\n): BabelNodeFlowType => {\n const selectedAttributes: Array<\n Array<BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty>,\n > = type.possibleTypes.map((possible) => {\n let seenTypeName = false;\n return selections\n .map((selection) =>\n unionOrInterfaceSelection(config, type, possible, selection),\n )\n .flat()\n .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 if (seenTypeName) {\n return false;\n }\n seenTypeName = true;\n }\n return true;\n });\n });\n const allFields = selections.every(\n (selection) => selection.kind === 'Field',\n );\n if (selectedAttributes.length === 1 || allFields) {\n return babelTypes.objectTypeAnnotation(\n selectedAttributes[0],\n undefined /* indexers */,\n undefined /* callProperties */,\n undefined /* internalSlots */,\n true /* exact */,\n );\n }\n return babelTypes.unionTypeAnnotation(\n selectedAttributes.map((properties) =>\n babelTypes.objectTypeAnnotation(\n properties,\n undefined /* indexers */,\n undefined /* callProperties */,\n undefined /* internalSlots */,\n true /* exact */,\n ),\n ),\n );\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(config, typeField.type, selection),\n ),\n ),\n ];\n }\n if (selection.kind === 'FragmentSpread') {\n const fragment = config.fragments[selection.name.value];\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"}
1
+ {"version":3,"sources":["../src/generateResponseType.js"],"names":["generateResponseType","schema","query","config","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","possibleTypesByName","indirectMatch"],"mappings":";;;;;;;AAEA;;AACA;;AASA;;AAKA;;;;;;;;AAhBA;AACyC;AA2BlC,MAAMA,oBAAoB,GAAG,CAChCC,MADgC,EAEhCC,KAFgC,EAGhCC,MAHgC,KAIvB;AACT,QAAMC,GAAG,GAAGC,0BAA0B,CAClCF,MADkC,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;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,CAA0ByE,QAAQ,CAAC1D,IAAnC,CAFqB,EAGrB0D,QAHqB,EAIrBrC,SAJqB,CAD1B,CADA,CAAP;AAUH,KAjBD,MAiBO;AACH,aAAO,EAAP;AACH;AACJ;;AACD,MAAIA,SAAS,CAACC,IAAV,KAAmB,gBAAvB,EAAyC;AACrC3C,IAAAA,MAAM,CAACmE,MAAP,CAAcC,IAAd,CACK,kDAAiD1B,SAAS,CAACC,IAAK,EADrE;;AAGA,QAAIzB,IAAI,CAACyB,IAAL,KAAc,OAAlB,EAA2B;AACvB3C,MAAAA,MAAM,CAACmE,MAAP,CACKC,IADL,CACW,kDAAiDlD,IAAI,CAACG,IAAK;AAClF;AACA,mDAHY;AAIH;;AACD,WAAO,EAAP;AACH;;AACD,MAAIqB,SAAS,CAACR,aAAd,EAA6B;AAAA;;AACzB,UAAM4B,QAAQ,GAAGpB,SAAS,CAACR,aAAV,CAAwBb,IAAxB,CAA6Bc,KAA9C;AACA,UAAMwD,aAAa,4BACf3F,MAAM,CAACF,MAAP,CAAcuC,gBAAd,CAA+ByB,QAA/B,CADe,0DACf,sBAA0C4B,mBAA1C,CACIX,QAAQ,CAAC1D,IADb,CADJ;;AAIA,QAAIyC,QAAQ,KAAKiB,QAAQ,CAAC1D,IAAtB,IAA8B,CAACsE,aAAnC,EAAkD;AAC9C,aAAO,EAAP;AACH;AACJ;;AACD,SAAOvD,sBAAsB,CACzBpC,MADyB,EAEzBA,MAAM,CAACF,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 {Config, 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 config: Config,\n): string => {\n const ast = querySelectionToObjectType(\n config,\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 config: Config,\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 = config.path.join('_');\n const isTopLevelType = config.path.length <= 1;\n if (config.allObjectTypes != null && !isTopLevelType) {\n config.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 config: Config,\n): string => {\n const onType = fragment.typeCondition.name.value;\n let ast;\n\n if (schema.typesByName[onType]) {\n ast = sortedObjectTypeAnnotation(\n config,\n objectPropertiesToFlow(\n config,\n schema.typesByName[onType],\n onType,\n fragment.selectionSet.selections,\n ),\n );\n } else if (schema.interfacesByName[onType]) {\n ast = unionOrInterfaceToFlow(\n config,\n config.schema.interfacesByName[onType],\n fragment.selectionSet.selections,\n );\n } else if (schema.unionsByName[onType]) {\n ast = unionOrInterfaceToFlow(\n config,\n config.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 config: Config,\n type,\n selection,\n): babelTypes.BabelNodeFlowType => {\n if (type.kind === 'SCALAR') {\n return scalarTypeToFlow(config, type.name);\n }\n if (type.kind === 'LIST') {\n return babelTypes.genericTypeAnnotation(\n config.readOnlyArray\n ? babelTypes.identifier('$ReadOnlyArray')\n : babelTypes.identifier('Array'),\n babelTypes.typeParameterInstantiation([\n typeToFlow(config, type.ofType, selection),\n ]),\n );\n }\n if (type.kind === 'UNION') {\n const union = config.schema.unionsByName[type.name];\n if (!selection.selectionSet) {\n console.log('no selection set', selection);\n return babelTypes.anyTypeAnnotation();\n }\n return unionOrInterfaceToFlow(\n config,\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 config,\n config.schema.interfacesByName[type.name],\n selection.selectionSet.selections,\n );\n }\n if (type.kind === 'ENUM') {\n return enumTypeToFlow(config, 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 (!config.schema.typesByName[tname]) {\n console.log('unknown referenced type', tname);\n return babelTypes.anyTypeAnnotation();\n }\n const childType = config.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 config,\n selection.selectionSet.selections,\n childType,\n tname,\n ),\n );\n};\n\nexport const typeToFlow = (\n config: Config,\n type: IntrospectionOutputTypeRef,\n selection: FieldNode,\n): babelTypes.BabelNodeFlowType => {\n // throw new Error('npoe');\n if (type.kind === 'NON_NULL') {\n return _typeToFlow(config, type.ofType, selection);\n }\n // If we don'babelTypes care about strict nullability checking, then pretend everything is non-null\n if (!config.strictNullability) {\n return _typeToFlow(config, type, selection);\n }\n const inner = _typeToFlow(config, 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 config: Config,\n selections,\n type,\n typeName: string,\n): BabelNodeFlowType => {\n return sortedObjectTypeAnnotation(\n config,\n ensureOnlyOneTypenameProperty(\n objectPropertiesToFlow(config, type, typeName, selections),\n ),\n );\n};\n\nexport const objectPropertiesToFlow = (\n config: Config,\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 config,\n config.schema.typesByName[newTypeName],\n newTypeName,\n selection.selectionSet.selections,\n );\n }\n case 'FragmentSpread':\n if (!config.fragments[selection.name.value]) {\n config.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 config,\n type,\n typeName,\n config.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 config.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 ...config,\n path: config.path.concat([alias]),\n },\n typeField.type,\n selection,\n ),\n ),\n ),\n ),\n ];\n\n default:\n config.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 config: Config,\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 ...config,\n path: allFields\n ? config.path\n : config.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(config, sharedAttributes);\n }\n if (selectedAttributes.length === 1) {\n return sortedObjectTypeAnnotation(\n config,\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 {...config, path: config.path.concat([typeName])},\n attributes,\n ),\n ),\n );\n const name = config.path.join('_');\n if (config.allObjectTypes && config.path.length > 1) {\n config.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"}