@khanacademy/graphql-flow 0.3.0 → 1.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/.github/workflows/pr-checks.yml +4 -6
- package/CHANGELOG.md +22 -0
- package/Readme.md +41 -167
- package/dist/__test__/example-schema.graphql +67 -0
- package/dist/__test__/generateTypeFileContents.test.js +157 -0
- package/dist/__test__/graphql-flow.test.js +639 -0
- package/dist/__test__/processPragmas.test.js +76 -0
- package/dist/cli/__test__/config.test.js +120 -0
- package/dist/cli/config.js +45 -106
- package/dist/cli/config.js.flow +37 -159
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/run.js +40 -36
- package/dist/cli/run.js.flow +56 -42
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/schema.json +91 -0
- package/dist/enums.js +9 -9
- package/dist/enums.js.flow +11 -11
- package/dist/enums.js.map +1 -1
- package/dist/generateResponseType.js +47 -47
- package/dist/generateResponseType.js.flow +55 -57
- package/dist/generateResponseType.js.map +1 -1
- package/dist/generateTypeFiles.js +13 -17
- package/dist/generateTypeFiles.js.flow +21 -43
- package/dist/generateTypeFiles.js.map +1 -1
- package/dist/generateVariablesType.js +24 -24
- package/dist/generateVariablesType.js.flow +25 -28
- package/dist/generateVariablesType.js.map +1 -1
- package/dist/index.js +0 -8
- package/dist/index.js.flow +4 -5
- package/dist/index.js.map +1 -1
- package/dist/parser/__test__/parse.test.js +247 -0
- package/dist/types.js.flow +26 -6
- package/package.json +3 -2
- package/src/__test__/generateTypeFileContents.test.js +3 -1
- package/src/__test__/graphql-flow.test.js +7 -7
- package/src/__test__/processPragmas.test.js +28 -15
- package/src/cli/__test__/config.test.js +110 -84
- package/src/cli/config.js +37 -159
- package/src/cli/run.js +56 -42
- package/src/cli/schema.json +91 -0
- package/src/enums.js +11 -11
- package/src/generateResponseType.js +55 -57
- package/src/generateTypeFiles.js +21 -43
- package/src/generateVariablesType.js +25 -28
- package/src/index.js +4 -5
- package/src/types.js +26 -6
- package/dist/cli/utils.js +0 -21
- package/dist/cli/utils.js.flow +0 -14
- package/dist/cli/utils.js.map +0 -1
- package/dist/jest-mock-graphql-tag.js +0 -88
- package/dist/jest-mock-graphql-tag.js.flow +0 -96
- package/dist/jest-mock-graphql-tag.js.map +0 -1
- package/src/cli/__test__/utils.test.js +0 -19
- package/src/cli/utils.js +0 -14
- package/src/jest-mock-graphql-tag.js +0 -96
|
@@ -21,15 +21,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
21
21
|
|
|
22
22
|
/* eslint-disable no-console */
|
|
23
23
|
// eslint-disable-line flowtype-errors/uncovered
|
|
24
|
-
const generateResponseType = (schema, query,
|
|
25
|
-
const ast = querySelectionToObjectType(
|
|
24
|
+
const generateResponseType = (schema, query, ctx) => {
|
|
25
|
+
const ast = querySelectionToObjectType(ctx, query.selectionSet.selections, query.operation === 'mutation' ? schema.typesByName.Mutation : schema.typesByName.Query, query.operation === 'mutation' ? 'mutation' : 'query'); // eslint-disable-next-line flowtype-errors/uncovered
|
|
26
26
|
|
|
27
27
|
return (0, _generator.default)(ast).code;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
exports.generateResponseType = generateResponseType;
|
|
31
31
|
|
|
32
|
-
const sortedObjectTypeAnnotation = (
|
|
32
|
+
const sortedObjectTypeAnnotation = (ctx, properties) => {
|
|
33
33
|
const obj = babelTypes.objectTypeAnnotation(properties.sort((a, b) => {
|
|
34
34
|
if (a.type === 'ObjectTypeProperty' && b.type === 'ObjectTypeProperty') {
|
|
35
35
|
const aName = a.key.type === 'Identifier' ? a.key.name : '';
|
|
@@ -47,27 +47,27 @@ const sortedObjectTypeAnnotation = (config, properties) => {
|
|
|
47
47
|
, true
|
|
48
48
|
/* exact */
|
|
49
49
|
);
|
|
50
|
-
const name =
|
|
51
|
-
const isTopLevelType =
|
|
50
|
+
const name = ctx.path.join('_');
|
|
51
|
+
const isTopLevelType = ctx.path.length <= 1;
|
|
52
52
|
|
|
53
|
-
if (
|
|
54
|
-
|
|
53
|
+
if (ctx.allObjectTypes != null && !isTopLevelType) {
|
|
54
|
+
ctx.allObjectTypes[name] = obj;
|
|
55
55
|
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
56
56
|
} else {
|
|
57
57
|
return obj;
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
-
const generateFragmentType = (schema, fragment,
|
|
61
|
+
const generateFragmentType = (schema, fragment, ctx) => {
|
|
62
62
|
const onType = fragment.typeCondition.name.value;
|
|
63
63
|
let ast;
|
|
64
64
|
|
|
65
65
|
if (schema.typesByName[onType]) {
|
|
66
|
-
ast = sortedObjectTypeAnnotation(
|
|
66
|
+
ast = sortedObjectTypeAnnotation(ctx, objectPropertiesToFlow(ctx, schema.typesByName[onType], onType, fragment.selectionSet.selections));
|
|
67
67
|
} else if (schema.interfacesByName[onType]) {
|
|
68
|
-
ast = unionOrInterfaceToFlow(
|
|
68
|
+
ast = unionOrInterfaceToFlow(ctx, ctx.schema.interfacesByName[onType], fragment.selectionSet.selections);
|
|
69
69
|
} else if (schema.unionsByName[onType]) {
|
|
70
|
-
ast = unionOrInterfaceToFlow(
|
|
70
|
+
ast = unionOrInterfaceToFlow(ctx, ctx.schema.unionsByName[onType], fragment.selectionSet.selections);
|
|
71
71
|
} else {
|
|
72
72
|
throw new Error(`Unknown ${onType}`);
|
|
73
73
|
} // eslint-disable-next-line flowtype-errors/uncovered
|
|
@@ -78,24 +78,24 @@ const generateFragmentType = (schema, fragment, config) => {
|
|
|
78
78
|
|
|
79
79
|
exports.generateFragmentType = generateFragmentType;
|
|
80
80
|
|
|
81
|
-
const _typeToFlow = (
|
|
81
|
+
const _typeToFlow = (ctx, type, selection) => {
|
|
82
82
|
if (type.kind === 'SCALAR') {
|
|
83
|
-
return (0, _enums.scalarTypeToFlow)(
|
|
83
|
+
return (0, _enums.scalarTypeToFlow)(ctx, type.name);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
if (type.kind === 'LIST') {
|
|
87
|
-
return babelTypes.genericTypeAnnotation(
|
|
87
|
+
return babelTypes.genericTypeAnnotation(ctx.readOnlyArray ? babelTypes.identifier('$ReadOnlyArray') : babelTypes.identifier('Array'), babelTypes.typeParameterInstantiation([typeToFlow(ctx, type.ofType, selection)]));
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
if (type.kind === 'UNION') {
|
|
91
|
-
const union =
|
|
91
|
+
const union = ctx.schema.unionsByName[type.name];
|
|
92
92
|
|
|
93
93
|
if (!selection.selectionSet) {
|
|
94
94
|
console.log('no selection set', selection);
|
|
95
95
|
return babelTypes.anyTypeAnnotation();
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
return unionOrInterfaceToFlow(
|
|
98
|
+
return unionOrInterfaceToFlow(ctx, union, selection.selectionSet.selections);
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
if (type.kind === 'INTERFACE') {
|
|
@@ -104,11 +104,11 @@ const _typeToFlow = (config, type, selection) => {
|
|
|
104
104
|
return babelTypes.anyTypeAnnotation();
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
return unionOrInterfaceToFlow(
|
|
107
|
+
return unionOrInterfaceToFlow(ctx, ctx.schema.interfacesByName[type.name], selection.selectionSet.selections);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
if (type.kind === 'ENUM') {
|
|
111
|
-
return (0, _enums.enumTypeToFlow)(
|
|
111
|
+
return (0, _enums.enumTypeToFlow)(ctx, type.name);
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
if (type.kind !== 'OBJECT') {
|
|
@@ -118,33 +118,33 @@ const _typeToFlow = (config, type, selection) => {
|
|
|
118
118
|
|
|
119
119
|
const tname = type.name;
|
|
120
120
|
|
|
121
|
-
if (!
|
|
121
|
+
if (!ctx.schema.typesByName[tname]) {
|
|
122
122
|
console.log('unknown referenced type', tname);
|
|
123
123
|
return babelTypes.anyTypeAnnotation();
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
const childType =
|
|
126
|
+
const childType = ctx.schema.typesByName[tname];
|
|
127
127
|
|
|
128
128
|
if (!selection.selectionSet) {
|
|
129
129
|
console.log('no selection set', selection);
|
|
130
130
|
return babelTypes.anyTypeAnnotation();
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
return (0, _utils.maybeAddDescriptionComment)(childType.description, querySelectionToObjectType(
|
|
133
|
+
return (0, _utils.maybeAddDescriptionComment)(childType.description, querySelectionToObjectType(ctx, selection.selectionSet.selections, childType, tname));
|
|
134
134
|
};
|
|
135
135
|
|
|
136
|
-
const typeToFlow = (
|
|
136
|
+
const typeToFlow = (ctx, type, selection) => {
|
|
137
137
|
// throw new Error('npoe');
|
|
138
138
|
if (type.kind === 'NON_NULL') {
|
|
139
|
-
return _typeToFlow(
|
|
139
|
+
return _typeToFlow(ctx, type.ofType, selection);
|
|
140
140
|
} // If we don'babelTypes care about strict nullability checking, then pretend everything is non-null
|
|
141
141
|
|
|
142
142
|
|
|
143
|
-
if (!
|
|
144
|
-
return _typeToFlow(
|
|
143
|
+
if (!ctx.strictNullability) {
|
|
144
|
+
return _typeToFlow(ctx, type, selection);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
const inner = _typeToFlow(
|
|
147
|
+
const inner = _typeToFlow(ctx, type, selection);
|
|
148
148
|
|
|
149
149
|
const result = babelTypes.nullableTypeAnnotation(inner);
|
|
150
150
|
return (0, _utils.transferLeadingComments)(inner, result);
|
|
@@ -176,11 +176,11 @@ const ensureOnlyOneTypenameProperty = properties => {
|
|
|
176
176
|
});
|
|
177
177
|
};
|
|
178
178
|
|
|
179
|
-
const querySelectionToObjectType = (
|
|
180
|
-
return sortedObjectTypeAnnotation(
|
|
179
|
+
const querySelectionToObjectType = (ctx, selections, type, typeName) => {
|
|
180
|
+
return sortedObjectTypeAnnotation(ctx, ensureOnlyOneTypenameProperty(objectPropertiesToFlow(ctx, type, typeName, selections)));
|
|
181
181
|
};
|
|
182
182
|
|
|
183
|
-
const objectPropertiesToFlow = (
|
|
183
|
+
const objectPropertiesToFlow = (ctx, type, typeName, selections) => {
|
|
184
184
|
return [].concat(...selections.map(selection => {
|
|
185
185
|
switch (selection.kind) {
|
|
186
186
|
case 'InlineFragment':
|
|
@@ -193,16 +193,16 @@ const objectPropertiesToFlow = (config, type, typeName, selections) => {
|
|
|
193
193
|
return [];
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
return objectPropertiesToFlow(
|
|
196
|
+
return objectPropertiesToFlow(ctx, ctx.schema.typesByName[newTypeName], newTypeName, selection.selectionSet.selections);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
case 'FragmentSpread':
|
|
200
|
-
if (!
|
|
201
|
-
|
|
200
|
+
if (!ctx.fragments[selection.name.value]) {
|
|
201
|
+
ctx.errors.push(`No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`);
|
|
202
202
|
return [babelTypes.objectTypeProperty(babelTypes.identifier(selection.name.value), babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_FRAGMENT`)))];
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
return objectPropertiesToFlow(
|
|
205
|
+
return objectPropertiesToFlow(ctx, type, typeName, ctx.fragments[selection.name.value].selectionSet.selections);
|
|
206
206
|
|
|
207
207
|
case 'Field':
|
|
208
208
|
const name = selection.name.value;
|
|
@@ -213,17 +213,17 @@ const objectPropertiesToFlow = (config, type, typeName, selections) => {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
if (!type.fieldsByName[name]) {
|
|
216
|
-
|
|
216
|
+
ctx.errors.push(`Unknown field '${name}' for type '${typeName}'`);
|
|
217
217
|
return babelTypes.objectTypeProperty(babelTypes.identifier(alias), babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_FIELD["${name}"]`)));
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
const typeField = type.fieldsByName[name];
|
|
221
|
-
return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.objectTypeProperty(babelTypes.identifier(alias), typeToFlow({ ...
|
|
222
|
-
path:
|
|
221
|
+
return [(0, _utils.maybeAddDescriptionComment)(typeField.description, (0, _utils.liftLeadingPropertyComments)(babelTypes.objectTypeProperty(babelTypes.identifier(alias), typeToFlow({ ...ctx,
|
|
222
|
+
path: ctx.path.concat([alias])
|
|
223
223
|
}, typeField.type, selection))))];
|
|
224
224
|
|
|
225
225
|
default:
|
|
226
|
-
|
|
226
|
+
ctx.errors.push( // eslint-disable-next-line flowtype-errors/uncovered
|
|
227
227
|
`Unsupported selection kind '${selection.kind}'`);
|
|
228
228
|
return [];
|
|
229
229
|
}
|
|
@@ -232,13 +232,13 @@ const objectPropertiesToFlow = (config, type, typeName, selections) => {
|
|
|
232
232
|
|
|
233
233
|
exports.objectPropertiesToFlow = objectPropertiesToFlow;
|
|
234
234
|
|
|
235
|
-
const unionOrInterfaceToFlow = (
|
|
235
|
+
const unionOrInterfaceToFlow = (ctx, type, selections) => {
|
|
236
236
|
const allFields = selections.every(selection => selection.kind === 'Field');
|
|
237
237
|
const selectedAttributes = type.possibleTypes.slice().sort((a, b) => {
|
|
238
238
|
return a.name < b.name ? -1 : 1;
|
|
239
239
|
}).map(possible => {
|
|
240
|
-
const configWithUpdatedPath = { ...
|
|
241
|
-
path: allFields ?
|
|
240
|
+
const configWithUpdatedPath = { ...ctx,
|
|
241
|
+
path: allFields ? ctx.path : ctx.path.concat([possible.name])
|
|
242
242
|
};
|
|
243
243
|
return {
|
|
244
244
|
typeName: possible.name,
|
|
@@ -255,11 +255,11 @@ const unionOrInterfaceToFlow = (config, type, selections) => {
|
|
|
255
255
|
attrs.attributes[typeNameIndex].value)));
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
return sortedObjectTypeAnnotation(
|
|
258
|
+
return sortedObjectTypeAnnotation(ctx, sharedAttributes);
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
if (selectedAttributes.length === 1) {
|
|
262
|
-
return sortedObjectTypeAnnotation(
|
|
262
|
+
return sortedObjectTypeAnnotation(ctx, selectedAttributes[0].attributes);
|
|
263
263
|
}
|
|
264
264
|
/**
|
|
265
265
|
* When generating the objects for the sub-options of a union, the path needs
|
|
@@ -296,13 +296,13 @@ const unionOrInterfaceToFlow = (config, type, selections) => {
|
|
|
296
296
|
const result = babelTypes.unionTypeAnnotation(selectedAttributes.map(({
|
|
297
297
|
typeName,
|
|
298
298
|
attributes
|
|
299
|
-
}) => sortedObjectTypeAnnotation({ ...
|
|
300
|
-
path:
|
|
299
|
+
}) => sortedObjectTypeAnnotation({ ...ctx,
|
|
300
|
+
path: ctx.path.concat([typeName])
|
|
301
301
|
}, attributes)));
|
|
302
|
-
const name =
|
|
302
|
+
const name = ctx.path.join('_');
|
|
303
303
|
|
|
304
|
-
if (
|
|
305
|
-
|
|
304
|
+
if (ctx.allObjectTypes && ctx.path.length > 1) {
|
|
305
|
+
ctx.allObjectTypes[name] = result;
|
|
306
306
|
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
307
307
|
}
|
|
308
308
|
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
OperationDefinitionNode,
|
|
10
10
|
FragmentDefinitionNode,
|
|
11
11
|
} from 'graphql';
|
|
12
|
-
import type {
|
|
12
|
+
import type {Context, Schema, Selections} from './types';
|
|
13
13
|
import {
|
|
14
14
|
liftLeadingPropertyComments,
|
|
15
15
|
maybeAddDescriptionComment,
|
|
@@ -30,10 +30,10 @@ import {
|
|
|
30
30
|
export const generateResponseType = (
|
|
31
31
|
schema: Schema,
|
|
32
32
|
query: OperationDefinitionNode,
|
|
33
|
-
|
|
33
|
+
ctx: Context,
|
|
34
34
|
): string => {
|
|
35
35
|
const ast = querySelectionToObjectType(
|
|
36
|
-
|
|
36
|
+
ctx,
|
|
37
37
|
query.selectionSet.selections,
|
|
38
38
|
query.operation === 'mutation'
|
|
39
39
|
? schema.typesByName.Mutation
|
|
@@ -45,7 +45,7 @@ export const generateResponseType = (
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
const sortedObjectTypeAnnotation = (
|
|
48
|
-
|
|
48
|
+
ctx: Context,
|
|
49
49
|
properties: Array<
|
|
50
50
|
BabelNodeObjectTypeProperty | BabelNodeObjectTypeSpreadProperty,
|
|
51
51
|
>,
|
|
@@ -67,10 +67,10 @@ const sortedObjectTypeAnnotation = (
|
|
|
67
67
|
undefined /* internalSlots */,
|
|
68
68
|
true /* exact */,
|
|
69
69
|
);
|
|
70
|
-
const name =
|
|
71
|
-
const isTopLevelType =
|
|
72
|
-
if (
|
|
73
|
-
|
|
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
74
|
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
75
75
|
} else {
|
|
76
76
|
return obj;
|
|
@@ -80,16 +80,16 @@ const sortedObjectTypeAnnotation = (
|
|
|
80
80
|
export const generateFragmentType = (
|
|
81
81
|
schema: Schema,
|
|
82
82
|
fragment: FragmentDefinitionNode,
|
|
83
|
-
|
|
83
|
+
ctx: Context,
|
|
84
84
|
): string => {
|
|
85
85
|
const onType = fragment.typeCondition.name.value;
|
|
86
86
|
let ast;
|
|
87
87
|
|
|
88
88
|
if (schema.typesByName[onType]) {
|
|
89
89
|
ast = sortedObjectTypeAnnotation(
|
|
90
|
-
|
|
90
|
+
ctx,
|
|
91
91
|
objectPropertiesToFlow(
|
|
92
|
-
|
|
92
|
+
ctx,
|
|
93
93
|
schema.typesByName[onType],
|
|
94
94
|
onType,
|
|
95
95
|
fragment.selectionSet.selections,
|
|
@@ -97,14 +97,14 @@ export const generateFragmentType = (
|
|
|
97
97
|
);
|
|
98
98
|
} else if (schema.interfacesByName[onType]) {
|
|
99
99
|
ast = unionOrInterfaceToFlow(
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
ctx,
|
|
101
|
+
ctx.schema.interfacesByName[onType],
|
|
102
102
|
fragment.selectionSet.selections,
|
|
103
103
|
);
|
|
104
104
|
} else if (schema.unionsByName[onType]) {
|
|
105
105
|
ast = unionOrInterfaceToFlow(
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
ctx,
|
|
107
|
+
ctx.schema.unionsByName[onType],
|
|
108
108
|
fragment.selectionSet.selections,
|
|
109
109
|
);
|
|
110
110
|
} else {
|
|
@@ -116,31 +116,31 @@ export const generateFragmentType = (
|
|
|
116
116
|
};
|
|
117
117
|
|
|
118
118
|
const _typeToFlow = (
|
|
119
|
-
|
|
119
|
+
ctx: Context,
|
|
120
120
|
type,
|
|
121
121
|
selection,
|
|
122
122
|
): babelTypes.BabelNodeFlowType => {
|
|
123
123
|
if (type.kind === 'SCALAR') {
|
|
124
|
-
return scalarTypeToFlow(
|
|
124
|
+
return scalarTypeToFlow(ctx, type.name);
|
|
125
125
|
}
|
|
126
126
|
if (type.kind === 'LIST') {
|
|
127
127
|
return babelTypes.genericTypeAnnotation(
|
|
128
|
-
|
|
128
|
+
ctx.readOnlyArray
|
|
129
129
|
? babelTypes.identifier('$ReadOnlyArray')
|
|
130
130
|
: babelTypes.identifier('Array'),
|
|
131
131
|
babelTypes.typeParameterInstantiation([
|
|
132
|
-
typeToFlow(
|
|
132
|
+
typeToFlow(ctx, type.ofType, selection),
|
|
133
133
|
]),
|
|
134
134
|
);
|
|
135
135
|
}
|
|
136
136
|
if (type.kind === 'UNION') {
|
|
137
|
-
const union =
|
|
137
|
+
const union = ctx.schema.unionsByName[type.name];
|
|
138
138
|
if (!selection.selectionSet) {
|
|
139
139
|
console.log('no selection set', selection);
|
|
140
140
|
return babelTypes.anyTypeAnnotation();
|
|
141
141
|
}
|
|
142
142
|
return unionOrInterfaceToFlow(
|
|
143
|
-
|
|
143
|
+
ctx,
|
|
144
144
|
union,
|
|
145
145
|
selection.selectionSet.selections,
|
|
146
146
|
);
|
|
@@ -152,13 +152,13 @@ const _typeToFlow = (
|
|
|
152
152
|
return babelTypes.anyTypeAnnotation();
|
|
153
153
|
}
|
|
154
154
|
return unionOrInterfaceToFlow(
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
ctx,
|
|
156
|
+
ctx.schema.interfacesByName[type.name],
|
|
157
157
|
selection.selectionSet.selections,
|
|
158
158
|
);
|
|
159
159
|
}
|
|
160
160
|
if (type.kind === 'ENUM') {
|
|
161
|
-
return enumTypeToFlow(
|
|
161
|
+
return enumTypeToFlow(ctx, type.name);
|
|
162
162
|
}
|
|
163
163
|
if (type.kind !== 'OBJECT') {
|
|
164
164
|
console.log('not object', type);
|
|
@@ -166,11 +166,11 @@ const _typeToFlow = (
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
const tname = type.name;
|
|
169
|
-
if (!
|
|
169
|
+
if (!ctx.schema.typesByName[tname]) {
|
|
170
170
|
console.log('unknown referenced type', tname);
|
|
171
171
|
return babelTypes.anyTypeAnnotation();
|
|
172
172
|
}
|
|
173
|
-
const childType =
|
|
173
|
+
const childType = ctx.schema.typesByName[tname];
|
|
174
174
|
if (!selection.selectionSet) {
|
|
175
175
|
console.log('no selection set', selection);
|
|
176
176
|
return babelTypes.anyTypeAnnotation();
|
|
@@ -178,7 +178,7 @@ const _typeToFlow = (
|
|
|
178
178
|
return maybeAddDescriptionComment(
|
|
179
179
|
childType.description,
|
|
180
180
|
querySelectionToObjectType(
|
|
181
|
-
|
|
181
|
+
ctx,
|
|
182
182
|
selection.selectionSet.selections,
|
|
183
183
|
childType,
|
|
184
184
|
tname,
|
|
@@ -187,19 +187,19 @@ const _typeToFlow = (
|
|
|
187
187
|
};
|
|
188
188
|
|
|
189
189
|
export const typeToFlow = (
|
|
190
|
-
|
|
190
|
+
ctx: Context,
|
|
191
191
|
type: IntrospectionOutputTypeRef,
|
|
192
192
|
selection: FieldNode,
|
|
193
193
|
): babelTypes.BabelNodeFlowType => {
|
|
194
194
|
// throw new Error('npoe');
|
|
195
195
|
if (type.kind === 'NON_NULL') {
|
|
196
|
-
return _typeToFlow(
|
|
196
|
+
return _typeToFlow(ctx, type.ofType, selection);
|
|
197
197
|
}
|
|
198
198
|
// If we don'babelTypes care about strict nullability checking, then pretend everything is non-null
|
|
199
|
-
if (!
|
|
200
|
-
return _typeToFlow(
|
|
199
|
+
if (!ctx.strictNullability) {
|
|
200
|
+
return _typeToFlow(ctx, type, selection);
|
|
201
201
|
}
|
|
202
|
-
const inner = _typeToFlow(
|
|
202
|
+
const inner = _typeToFlow(ctx, type, selection);
|
|
203
203
|
const result = babelTypes.nullableTypeAnnotation(inner);
|
|
204
204
|
return transferLeadingComments(inner, result);
|
|
205
205
|
};
|
|
@@ -233,21 +233,21 @@ const ensureOnlyOneTypenameProperty = (properties) => {
|
|
|
233
233
|
};
|
|
234
234
|
|
|
235
235
|
const querySelectionToObjectType = (
|
|
236
|
-
|
|
236
|
+
ctx: Context,
|
|
237
237
|
selections,
|
|
238
238
|
type,
|
|
239
239
|
typeName: string,
|
|
240
240
|
): BabelNodeFlowType => {
|
|
241
241
|
return sortedObjectTypeAnnotation(
|
|
242
|
-
|
|
242
|
+
ctx,
|
|
243
243
|
ensureOnlyOneTypenameProperty(
|
|
244
|
-
objectPropertiesToFlow(
|
|
244
|
+
objectPropertiesToFlow(ctx, type, typeName, selections),
|
|
245
245
|
),
|
|
246
246
|
);
|
|
247
247
|
};
|
|
248
248
|
|
|
249
249
|
export const objectPropertiesToFlow = (
|
|
250
|
-
|
|
250
|
+
ctx: Context,
|
|
251
251
|
type: IntrospectionObjectType & {
|
|
252
252
|
fieldsByName: {[name: string]: IntrospectionField},
|
|
253
253
|
},
|
|
@@ -264,15 +264,15 @@ export const objectPropertiesToFlow = (
|
|
|
264
264
|
return [];
|
|
265
265
|
}
|
|
266
266
|
return objectPropertiesToFlow(
|
|
267
|
-
|
|
268
|
-
|
|
267
|
+
ctx,
|
|
268
|
+
ctx.schema.typesByName[newTypeName],
|
|
269
269
|
newTypeName,
|
|
270
270
|
selection.selectionSet.selections,
|
|
271
271
|
);
|
|
272
272
|
}
|
|
273
273
|
case 'FragmentSpread':
|
|
274
|
-
if (!
|
|
275
|
-
|
|
274
|
+
if (!ctx.fragments[selection.name.value]) {
|
|
275
|
+
ctx.errors.push(
|
|
276
276
|
`No fragment named '${selection.name.value}'. Did you forget to include it in the template literal?`,
|
|
277
277
|
);
|
|
278
278
|
return [
|
|
@@ -286,10 +286,10 @@ export const objectPropertiesToFlow = (
|
|
|
286
286
|
}
|
|
287
287
|
|
|
288
288
|
return objectPropertiesToFlow(
|
|
289
|
-
|
|
289
|
+
ctx,
|
|
290
290
|
type,
|
|
291
291
|
typeName,
|
|
292
|
-
|
|
292
|
+
ctx.fragments[selection.name.value].selectionSet
|
|
293
293
|
.selections,
|
|
294
294
|
);
|
|
295
295
|
|
|
@@ -309,7 +309,7 @@ export const objectPropertiesToFlow = (
|
|
|
309
309
|
];
|
|
310
310
|
}
|
|
311
311
|
if (!type.fieldsByName[name]) {
|
|
312
|
-
|
|
312
|
+
ctx.errors.push(
|
|
313
313
|
`Unknown field '${name}' for type '${typeName}'`,
|
|
314
314
|
);
|
|
315
315
|
return babelTypes.objectTypeProperty(
|
|
@@ -331,8 +331,8 @@ export const objectPropertiesToFlow = (
|
|
|
331
331
|
babelTypes.identifier(alias),
|
|
332
332
|
typeToFlow(
|
|
333
333
|
{
|
|
334
|
-
...
|
|
335
|
-
path:
|
|
334
|
+
...ctx,
|
|
335
|
+
path: ctx.path.concat([alias]),
|
|
336
336
|
},
|
|
337
337
|
typeField.type,
|
|
338
338
|
selection,
|
|
@@ -343,7 +343,7 @@ export const objectPropertiesToFlow = (
|
|
|
343
343
|
];
|
|
344
344
|
|
|
345
345
|
default:
|
|
346
|
-
|
|
346
|
+
ctx.errors.push(
|
|
347
347
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
348
348
|
`Unsupported selection kind '${selection.kind}'`,
|
|
349
349
|
);
|
|
@@ -354,7 +354,7 @@ export const objectPropertiesToFlow = (
|
|
|
354
354
|
};
|
|
355
355
|
|
|
356
356
|
export const unionOrInterfaceToFlow = (
|
|
357
|
-
|
|
357
|
+
ctx: Context,
|
|
358
358
|
type:
|
|
359
359
|
| IntrospectionUnionType
|
|
360
360
|
| (IntrospectionInterfaceType & {
|
|
@@ -377,10 +377,8 @@ export const unionOrInterfaceToFlow = (
|
|
|
377
377
|
})
|
|
378
378
|
.map((possible) => {
|
|
379
379
|
const configWithUpdatedPath = {
|
|
380
|
-
...
|
|
381
|
-
path: allFields
|
|
382
|
-
? config.path
|
|
383
|
-
: config.path.concat([possible.name]),
|
|
380
|
+
...ctx,
|
|
381
|
+
path: allFields ? ctx.path : ctx.path.concat([possible.name]),
|
|
384
382
|
};
|
|
385
383
|
return {
|
|
386
384
|
typeName: possible.name,
|
|
@@ -421,11 +419,11 @@ export const unionOrInterfaceToFlow = (
|
|
|
421
419
|
),
|
|
422
420
|
);
|
|
423
421
|
}
|
|
424
|
-
return sortedObjectTypeAnnotation(
|
|
422
|
+
return sortedObjectTypeAnnotation(ctx, sharedAttributes);
|
|
425
423
|
}
|
|
426
424
|
if (selectedAttributes.length === 1) {
|
|
427
425
|
return sortedObjectTypeAnnotation(
|
|
428
|
-
|
|
426
|
+
ctx,
|
|
429
427
|
selectedAttributes[0].attributes,
|
|
430
428
|
);
|
|
431
429
|
}
|
|
@@ -462,14 +460,14 @@ export const unionOrInterfaceToFlow = (
|
|
|
462
460
|
const result = babelTypes.unionTypeAnnotation(
|
|
463
461
|
selectedAttributes.map(({typeName, attributes}) =>
|
|
464
462
|
sortedObjectTypeAnnotation(
|
|
465
|
-
{...
|
|
463
|
+
{...ctx, path: ctx.path.concat([typeName])},
|
|
466
464
|
attributes,
|
|
467
465
|
),
|
|
468
466
|
),
|
|
469
467
|
);
|
|
470
|
-
const name =
|
|
471
|
-
if (
|
|
472
|
-
|
|
468
|
+
const name = ctx.path.join('_');
|
|
469
|
+
if (ctx.allObjectTypes && ctx.path.length > 1) {
|
|
470
|
+
ctx.allObjectTypes[name] = result;
|
|
473
471
|
return babelTypes.genericTypeAnnotation(babelTypes.identifier(name));
|
|
474
472
|
}
|
|
475
473
|
return result;
|