@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
package/dist/cli/run.js.flow
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// @flow
|
|
3
|
-
/* eslint-disable no-console */
|
|
4
|
-
import type {Schema} from '../types';
|
|
5
|
-
import type {GraphQLSchema} from 'graphql/type/schema';
|
|
6
|
-
|
|
7
|
-
import {generateTypeFiles, processPragmas} from '../generateTypeFiles';
|
|
8
|
-
import {processFiles} from '../parser/parse';
|
|
9
|
-
import {resolveDocuments} from '../parser/resolve';
|
|
10
|
-
import {findApplicableConfig, getSchemas, loadConfigFile} from './config';
|
|
11
|
-
|
|
12
|
-
import {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line flowtype-errors/uncovered
|
|
13
|
-
|
|
14
|
-
import {execSync} from 'child_process';
|
|
15
|
-
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
|
|
16
|
-
import {type DocumentNode} from 'graphql';
|
|
17
|
-
import {print} from 'graphql/language/printer';
|
|
18
|
-
import {validate} from 'graphql/validation';
|
|
19
|
-
import path from 'path';
|
|
20
|
-
import {dirname} from 'path';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* This CLI tool executes the following steps:
|
|
24
|
-
* 1) parse & validate config file
|
|
25
|
-
* 2) crawl files to find all operations and fragments, with
|
|
26
|
-
* tagged template literals and expressions.
|
|
27
|
-
* 3) resolve the found operations, passing the literals and
|
|
28
|
-
* fragments into the `graphql-tag` function to produce
|
|
29
|
-
* the DocumentNodes.
|
|
30
|
-
* 4) generate types for all resolved Queries & Mutations
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
/** Step (1) */
|
|
34
|
-
|
|
35
|
-
const findGraphqlTagReferences = (root: string): Array<string> => {
|
|
36
|
-
const response = execSync(
|
|
37
|
-
"git grep -I --word-regexp --name-only --fixed-strings 'graphql-tag' -- '*.js' '*.jsx' '*.ts' '*.tsx'",
|
|
38
|
-
{
|
|
39
|
-
encoding: 'utf8',
|
|
40
|
-
cwd: root,
|
|
41
|
-
},
|
|
42
|
-
);
|
|
43
|
-
return response
|
|
44
|
-
.trim()
|
|
45
|
-
.split('\n')
|
|
46
|
-
.map((relative) => path.join(root, relative));
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const [_, __, configFilePath, ...cliFiles] = process.argv;
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
configFilePath === '-h' ||
|
|
53
|
-
configFilePath === '--help' ||
|
|
54
|
-
configFilePath === 'help' ||
|
|
55
|
-
!configFilePath
|
|
56
|
-
) {
|
|
57
|
-
console.log(`graphql-flow
|
|
58
|
-
|
|
59
|
-
Usage: graphql-flow [configFile.json] [filesToCrawl...]`);
|
|
60
|
-
process.exit(1); // eslint-disable-line flowtype-errors/uncovered
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const makeAbsPath = (maybeRelativePath: string, basePath: string) => {
|
|
64
|
-
return path.isAbsolute(maybeRelativePath)
|
|
65
|
-
? maybeRelativePath
|
|
66
|
-
: path.join(basePath, maybeRelativePath);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const absConfigPath = makeAbsPath(configFilePath, process.cwd());
|
|
70
|
-
|
|
71
|
-
const config = loadConfigFile(absConfigPath);
|
|
72
|
-
|
|
73
|
-
const inputFiles = cliFiles.length
|
|
74
|
-
? cliFiles
|
|
75
|
-
: findGraphqlTagReferences(
|
|
76
|
-
makeAbsPath(config.crawl.root, path.dirname(absConfigPath)),
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
/** Step (2) */
|
|
80
|
-
|
|
81
|
-
const files = processFiles(inputFiles, (f) => {
|
|
82
|
-
if (existsSync(f)) {
|
|
83
|
-
return readFileSync(f, 'utf8');
|
|
84
|
-
}
|
|
85
|
-
if (existsSync(f + '.js')) {
|
|
86
|
-
return {text: readFileSync(f + '.js', 'utf8'), resolvedPath: f + '.js'};
|
|
87
|
-
}
|
|
88
|
-
if (existsSync(f + '.ts')) {
|
|
89
|
-
return {text: readFileSync(f + '.ts', 'utf8'), resolvedPath: f + '.ts'};
|
|
90
|
-
}
|
|
91
|
-
if (existsSync(f + '.tsx')) {
|
|
92
|
-
return {
|
|
93
|
-
text: readFileSync(f + '.tsx', 'utf8'),
|
|
94
|
-
resolvedPath: f + '.tsx',
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
throw new Error(`Unable to find ${f}`);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
let filesHadErrors = false;
|
|
101
|
-
Object.keys(files).forEach((key) => {
|
|
102
|
-
const file = files[key];
|
|
103
|
-
if (file.errors.length) {
|
|
104
|
-
filesHadErrors = true;
|
|
105
|
-
console.error(`Errors in ${file.path}`);
|
|
106
|
-
file.errors.forEach((error) => {
|
|
107
|
-
console.error(` - ${error.message}`);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
if (filesHadErrors) {
|
|
113
|
-
console.error('Aborting');
|
|
114
|
-
process.exit(1); // eslint-disable-line flowtype-errors/uncovered
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** Step (3) */
|
|
118
|
-
|
|
119
|
-
const {resolved, errors} = resolveDocuments(files);
|
|
120
|
-
if (errors.length) {
|
|
121
|
-
errors.forEach((error) => {
|
|
122
|
-
console.error(`Resolution error ${error.message} in ${error.loc.path}`);
|
|
123
|
-
});
|
|
124
|
-
console.error('Aborting');
|
|
125
|
-
process.exit(1); // eslint-disable-line flowtype-errors/uncovered
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
console.log(Object.keys(resolved).length, 'resolved queries');
|
|
129
|
-
|
|
130
|
-
/** Step (4) */
|
|
131
|
-
|
|
132
|
-
const schemaCache: {[key: string]: [GraphQLSchema, Schema]} = {};
|
|
133
|
-
const getCachedSchemas = (schemaFilePath: string) => {
|
|
134
|
-
if (!schemaCache[schemaFilePath]) {
|
|
135
|
-
schemaCache[schemaFilePath] = getSchemas(
|
|
136
|
-
makeAbsPath(schemaFilePath, path.dirname(absConfigPath)),
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return schemaCache[schemaFilePath];
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
let validationFailures: number = 0;
|
|
144
|
-
const printedOperations: Array<string> = [];
|
|
145
|
-
|
|
146
|
-
Object.keys(resolved).forEach((filePathAndLine) => {
|
|
147
|
-
const {document, raw} = resolved[filePathAndLine];
|
|
148
|
-
|
|
149
|
-
const hasNonFragments = document.definitions.some(
|
|
150
|
-
({kind}) => kind !== 'FragmentDefinition',
|
|
151
|
-
);
|
|
152
|
-
const rawSource: string = raw.literals[0];
|
|
153
|
-
|
|
154
|
-
const generateConfig = findApplicableConfig(
|
|
155
|
-
// strip off the trailing line number, e.g. `:23`
|
|
156
|
-
filePathAndLine.split(':')[0],
|
|
157
|
-
config.generate,
|
|
158
|
-
);
|
|
159
|
-
if (!generateConfig) {
|
|
160
|
-
return; // no generate config matches, bail
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
164
|
-
const withTypeNames: DocumentNode = addTypenameToDocument(document);
|
|
165
|
-
const printed = print(withTypeNames);
|
|
166
|
-
if (hasNonFragments && !printedOperations.includes(printed)) {
|
|
167
|
-
printedOperations.push(printed);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const pragmaResult = processPragmas(
|
|
171
|
-
generateConfig,
|
|
172
|
-
config.crawl,
|
|
173
|
-
rawSource,
|
|
174
|
-
);
|
|
175
|
-
if (!pragmaResult.generate) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
if (pragmaResult.strict != null) {
|
|
179
|
-
generateConfig.strictNullability = pragmaResult.strict;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const [schemaForValidation, schemaForTypeGeneration] = getCachedSchemas(
|
|
183
|
-
generateConfig.schemaFilePath,
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
if (hasNonFragments) {
|
|
187
|
-
/* eslint-disable flowtype-errors/uncovered */
|
|
188
|
-
const errors = validate(schemaForValidation, withTypeNames);
|
|
189
|
-
/* eslint-disable flowtype-errors/uncovered */
|
|
190
|
-
if (errors.length) {
|
|
191
|
-
errors.forEach((error) => {
|
|
192
|
-
console.error(
|
|
193
|
-
`Schema validation found errors for ${raw.loc.path}!`,
|
|
194
|
-
);
|
|
195
|
-
console.error(printed);
|
|
196
|
-
console.error(error);
|
|
197
|
-
validationFailures++;
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
/* eslint-enable flowtype-errors/uncovered */
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
try {
|
|
204
|
-
generateTypeFiles(
|
|
205
|
-
raw.loc.path,
|
|
206
|
-
schemaForTypeGeneration,
|
|
207
|
-
withTypeNames,
|
|
208
|
-
generateConfig,
|
|
209
|
-
);
|
|
210
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
211
|
-
} catch (err) {
|
|
212
|
-
console.error(`Error while generating operation from ${raw.loc.path}`);
|
|
213
|
-
console.error(printed);
|
|
214
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
215
|
-
console.error(err);
|
|
216
|
-
validationFailures++;
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
if (validationFailures) {
|
|
221
|
-
console.error(
|
|
222
|
-
`Encountered ${validationFailures} validation failures while printing types.`,
|
|
223
|
-
);
|
|
224
|
-
// eslint-disable-next-line flowtype-errors/uncovered
|
|
225
|
-
process.exit(1);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (config.crawl.dumpOperations) {
|
|
229
|
-
const dumpOperations = config.crawl.dumpOperations;
|
|
230
|
-
const parent = dirname(dumpOperations);
|
|
231
|
-
mkdirSync(parent, {recursive: true});
|
|
232
|
-
writeFileSync(
|
|
233
|
-
dumpOperations,
|
|
234
|
-
JSON.stringify(printedOperations.sort(), null, 2),
|
|
235
|
-
);
|
|
236
|
-
}
|
package/dist/cli/run.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/run.js"],"names":["findGraphqlTagReferences","root","response","encoding","cwd","trim","split","map","relative","path","join","_","__","configFilePath","cliFiles","process","argv","console","log","exit","makeAbsPath","maybeRelativePath","basePath","isAbsolute","absConfigPath","config","inputFiles","length","crawl","dirname","files","f","text","resolvedPath","Error","filesHadErrors","Object","keys","forEach","key","file","errors","error","message","resolved","loc","schemaCache","getCachedSchemas","schemaFilePath","validationFailures","printedOperations","filePathAndLine","document","raw","hasNonFragments","definitions","some","kind","rawSource","literals","generateConfig","generate","withTypeNames","printed","includes","push","pragmaResult","strict","strictNullability","schemaForValidation","schemaForTypeGeneration","err","dumpOperations","parent","recursive","JSON","stringify","sort"],"mappings":"AAAA;;AAEA;;;AAIA;;AACA;;AACA;;AACA;;AAEA;;AAEA;;AACA;;AAEA;;AACA;;AACA;;;;;;AAPwD;;AAUxD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AAEA,MAAMA,wBAAwB,GAAIC,IAAD,IAAiC;AAC9D,QAAMC,QAAQ,GAAG,6BACb,sGADa,EAEb;AACIC,IAAAA,QAAQ,EAAE,MADd;AAEIC,IAAAA,GAAG,EAAEH;AAFT,GAFa,CAAjB;AAOA,SAAOC,QAAQ,CACVG,IADE,GAEFC,KAFE,CAEI,IAFJ,EAGFC,GAHE,CAGGC,QAAD,IAAcC,cAAKC,IAAL,CAAUT,IAAV,EAAgBO,QAAhB,CAHhB,CAAP;AAIH,CAZD;;AAcA,MAAM,CAACG,CAAD,EAAIC,EAAJ,EAAQC,cAAR,EAAwB,GAAGC,QAA3B,IAAuCC,OAAO,CAACC,IAArD;;AAEA,IACIH,cAAc,KAAK,IAAnB,IACAA,cAAc,KAAK,QADnB,IAEAA,cAAc,KAAK,MAFnB,IAGA,CAACA,cAJL,EAKE;AACEI,EAAAA,OAAO,CAACC,GAAR,CAAa;AACjB;AACA,wDAFI;AAGAH,EAAAA,OAAO,CAACI,IAAR,CAAa,CAAb,EAJF,CAImB;AACpB;;AAED,MAAMC,WAAW,GAAG,CAACC,iBAAD,EAA4BC,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EAAAA,OAAO,CAACI,IAAR,CAAa,CAAb;AACH;;AAED,IAAIM,MAAM,CAACG,KAAP,CAAa4C,cAAjB,EAAiC;AAC7B,QAAMA,cAAc,GAAG/C,MAAM,CAACG,KAAP,CAAa4C,cAApC;AACA,QAAMC,MAAM,GAAG,mBAAQD,cAAR,CAAf;AACA,qBAAUC,MAAV,EAAkB;AAACC,IAAAA,SAAS,EAAE;AAAZ,GAAlB;AACA,yBACIF,cADJ,EAEIG,IAAI,CAACC,SAAL,CAAe1B,iBAAiB,CAAC2B,IAAlB,EAAf,EAAyC,IAAzC,EAA+C,CAA/C,CAFJ;AAIH","sourcesContent":["#!/usr/bin/env node\n// @flow\n/* eslint-disable no-console */\nimport type {Schema} from '../types';\nimport type {GraphQLSchema} from 'graphql/type/schema';\n\nimport {generateTypeFiles, processPragmas} from '../generateTypeFiles';\nimport {processFiles} from '../parser/parse';\nimport {resolveDocuments} from '../parser/resolve';\nimport {findApplicableConfig, getSchemas, loadConfigFile} from './config';\n\nimport {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line flowtype-errors/uncovered\n\nimport {execSync} from 'child_process';\nimport {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';\nimport {type DocumentNode} from 'graphql';\nimport {print} from 'graphql/language/printer';\nimport {validate} from 'graphql/validation';\nimport path from 'path';\nimport {dirname} from 'path';\n\n/**\n * This CLI tool executes the following steps:\n * 1) parse & validate config file\n * 2) crawl files to find all operations and fragments, with\n * tagged template literals and expressions.\n * 3) resolve the found operations, passing the literals and\n * fragments into the `graphql-tag` function to produce\n * the DocumentNodes.\n * 4) generate types for all resolved Queries & Mutations\n */\n\n/** Step (1) */\n\nconst findGraphqlTagReferences = (root: string): Array<string> => {\n const response = execSync(\n \"git grep -I --word-regexp --name-only --fixed-strings 'graphql-tag' -- '*.js' '*.jsx' '*.ts' '*.tsx'\",\n {\n encoding: 'utf8',\n cwd: root,\n },\n );\n return response\n .trim()\n .split('\\n')\n .map((relative) => path.join(root, relative));\n};\n\nconst [_, __, configFilePath, ...cliFiles] = process.argv;\n\nif (\n configFilePath === '-h' ||\n configFilePath === '--help' ||\n configFilePath === 'help' ||\n !configFilePath\n) {\n console.log(`graphql-flow\n\nUsage: graphql-flow [configFile.json] [filesToCrawl...]`);\n process.exit(1); // eslint-disable-line flowtype-errors/uncovered\n}\n\nconst makeAbsPath = (maybeRelativePath: string, basePath: string) => {\n return path.isAbsolute(maybeRelativePath)\n ? maybeRelativePath\n : path.join(basePath, maybeRelativePath);\n};\n\nconst absConfigPath = makeAbsPath(configFilePath, process.cwd());\n\nconst config = loadConfigFile(absConfigPath);\n\nconst inputFiles = cliFiles.length\n ? cliFiles\n : findGraphqlTagReferences(\n makeAbsPath(config.crawl.root, path.dirname(absConfigPath)),\n );\n\n/** Step (2) */\n\nconst files = processFiles(inputFiles, (f) => {\n if (existsSync(f)) {\n return readFileSync(f, 'utf8');\n }\n if (existsSync(f + '.js')) {\n return {text: readFileSync(f + '.js', 'utf8'), resolvedPath: f + '.js'};\n }\n if (existsSync(f + '.ts')) {\n return {text: readFileSync(f + '.ts', 'utf8'), resolvedPath: f + '.ts'};\n }\n if (existsSync(f + '.tsx')) {\n return {\n text: readFileSync(f + '.tsx', 'utf8'),\n resolvedPath: f + '.tsx',\n };\n }\n throw new Error(`Unable to find ${f}`);\n});\n\nlet filesHadErrors = false;\nObject.keys(files).forEach((key) => {\n const file = files[key];\n if (file.errors.length) {\n filesHadErrors = true;\n console.error(`Errors in ${file.path}`);\n file.errors.forEach((error) => {\n console.error(` - ${error.message}`);\n });\n }\n});\n\nif (filesHadErrors) {\n console.error('Aborting');\n process.exit(1); // eslint-disable-line flowtype-errors/uncovered\n}\n\n/** Step (3) */\n\nconst {resolved, errors} = resolveDocuments(files);\nif (errors.length) {\n errors.forEach((error) => {\n console.error(`Resolution error ${error.message} in ${error.loc.path}`);\n });\n console.error('Aborting');\n process.exit(1); // eslint-disable-line flowtype-errors/uncovered\n}\n\nconsole.log(Object.keys(resolved).length, 'resolved queries');\n\n/** Step (4) */\n\nconst schemaCache: {[key: string]: [GraphQLSchema, Schema]} = {};\nconst getCachedSchemas = (schemaFilePath: string) => {\n if (!schemaCache[schemaFilePath]) {\n schemaCache[schemaFilePath] = getSchemas(\n makeAbsPath(schemaFilePath, path.dirname(absConfigPath)),\n );\n }\n\n return schemaCache[schemaFilePath];\n};\n\nlet validationFailures: number = 0;\nconst printedOperations: Array<string> = [];\n\nObject.keys(resolved).forEach((filePathAndLine) => {\n const {document, raw} = resolved[filePathAndLine];\n\n const hasNonFragments = document.definitions.some(\n ({kind}) => kind !== 'FragmentDefinition',\n );\n const rawSource: string = raw.literals[0];\n\n const generateConfig = findApplicableConfig(\n // strip off the trailing line number, e.g. `:23`\n filePathAndLine.split(':')[0],\n config.generate,\n );\n if (!generateConfig) {\n return; // no generate config matches, bail\n }\n\n // eslint-disable-next-line flowtype-errors/uncovered\n const withTypeNames: DocumentNode = addTypenameToDocument(document);\n const printed = print(withTypeNames);\n if (hasNonFragments && !printedOperations.includes(printed)) {\n printedOperations.push(printed);\n }\n\n const pragmaResult = processPragmas(\n generateConfig,\n config.crawl,\n rawSource,\n );\n if (!pragmaResult.generate) {\n return;\n }\n if (pragmaResult.strict != null) {\n generateConfig.strictNullability = pragmaResult.strict;\n }\n\n const [schemaForValidation, schemaForTypeGeneration] = getCachedSchemas(\n generateConfig.schemaFilePath,\n );\n\n if (hasNonFragments) {\n /* eslint-disable flowtype-errors/uncovered */\n const errors = validate(schemaForValidation, withTypeNames);\n /* eslint-disable flowtype-errors/uncovered */\n if (errors.length) {\n errors.forEach((error) => {\n console.error(\n `Schema validation found errors for ${raw.loc.path}!`,\n );\n console.error(printed);\n console.error(error);\n validationFailures++;\n });\n }\n /* eslint-enable flowtype-errors/uncovered */\n }\n\n try {\n generateTypeFiles(\n raw.loc.path,\n schemaForTypeGeneration,\n withTypeNames,\n generateConfig,\n );\n // eslint-disable-next-line flowtype-errors/uncovered\n } catch (err) {\n console.error(`Error while generating operation from ${raw.loc.path}`);\n console.error(printed);\n // eslint-disable-next-line flowtype-errors/uncovered\n console.error(err);\n validationFailures++;\n }\n});\n\nif (validationFailures) {\n console.error(\n `Encountered ${validationFailures} validation failures while printing types.`,\n );\n // eslint-disable-next-line flowtype-errors/uncovered\n process.exit(1);\n}\n\nif (config.crawl.dumpOperations) {\n const dumpOperations = config.crawl.dumpOperations;\n const parent = dirname(dumpOperations);\n mkdirSync(parent, {recursive: true});\n writeFileSync(\n dumpOperations,\n JSON.stringify(printedOperations.sort(), null, 2),\n );\n}\n"],"file":"run.js"}
|
package/dist/enums.js.flow
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
/**
|
|
3
|
-
* Both input & output types can have enums & scalars.
|
|
4
|
-
*/
|
|
5
|
-
import * as babelTypes from '@babel/types';
|
|
6
|
-
import type {BabelNodeFlowType} from '@babel/types';
|
|
7
|
-
import type {Context} from './types';
|
|
8
|
-
import {maybeAddDescriptionComment} from './utils';
|
|
9
|
-
import type {IntrospectionEnumType} from 'graphql/utilities/introspectionQuery';
|
|
10
|
-
|
|
11
|
-
export const experimentalEnumTypeToFlow = (
|
|
12
|
-
ctx: Context,
|
|
13
|
-
enumConfig: IntrospectionEnumType,
|
|
14
|
-
description: string,
|
|
15
|
-
): BabelNodeFlowType => {
|
|
16
|
-
const enumDeclaration = babelTypes.enumDeclaration(
|
|
17
|
-
// pass id into generic type annotation
|
|
18
|
-
babelTypes.identifier(enumConfig.name),
|
|
19
|
-
babelTypes.enumStringBody(
|
|
20
|
-
enumConfig.enumValues.map((v) =>
|
|
21
|
-
babelTypes.enumDefaultedMember(babelTypes.identifier(v.name)),
|
|
22
|
-
),
|
|
23
|
-
),
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
if (ctx.experimentalEnumsMap) {
|
|
27
|
-
ctx.experimentalEnumsMap[enumConfig.name] = enumDeclaration;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return maybeAddDescriptionComment(
|
|
31
|
-
description,
|
|
32
|
-
babelTypes.genericTypeAnnotation(enumDeclaration.id),
|
|
33
|
-
);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export const enumTypeToFlow = (
|
|
37
|
-
ctx: Context,
|
|
38
|
-
name: string,
|
|
39
|
-
): BabelNodeFlowType => {
|
|
40
|
-
const enumConfig = ctx.schema.enumsByName[name];
|
|
41
|
-
let combinedDescription = enumConfig.enumValues
|
|
42
|
-
.map(
|
|
43
|
-
(n) =>
|
|
44
|
-
`- ${n.name}` +
|
|
45
|
-
(n.description
|
|
46
|
-
? '\n\n ' + n.description.replace(/\n/g, '\n ')
|
|
47
|
-
: ''),
|
|
48
|
-
)
|
|
49
|
-
.join('\n');
|
|
50
|
-
if (enumConfig.description) {
|
|
51
|
-
combinedDescription =
|
|
52
|
-
enumConfig.description + '\n\n' + combinedDescription;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return ctx.experimentalEnumsMap
|
|
56
|
-
? experimentalEnumTypeToFlow(ctx, enumConfig, combinedDescription)
|
|
57
|
-
: maybeAddDescriptionComment(
|
|
58
|
-
combinedDescription,
|
|
59
|
-
babelTypes.unionTypeAnnotation(
|
|
60
|
-
enumConfig.enumValues.map((n) =>
|
|
61
|
-
babelTypes.stringLiteralTypeAnnotation(n.name),
|
|
62
|
-
),
|
|
63
|
-
),
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const builtinScalars: {[key: string]: string} = {
|
|
68
|
-
Boolean: 'boolean',
|
|
69
|
-
String: 'string',
|
|
70
|
-
DateTime: 'string',
|
|
71
|
-
Date: 'string',
|
|
72
|
-
ID: 'string',
|
|
73
|
-
Int: 'number',
|
|
74
|
-
Float: 'number',
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const scalarTypeToFlow = (
|
|
78
|
-
ctx: Context,
|
|
79
|
-
name: string,
|
|
80
|
-
): BabelNodeFlowType => {
|
|
81
|
-
if (builtinScalars[name]) {
|
|
82
|
-
return babelTypes.genericTypeAnnotation(
|
|
83
|
-
babelTypes.identifier(builtinScalars[name]),
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
const underlyingType = ctx.scalars[name];
|
|
87
|
-
if (underlyingType != null) {
|
|
88
|
-
return babelTypes.genericTypeAnnotation(
|
|
89
|
-
babelTypes.identifier(underlyingType),
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
ctx.errors.push(
|
|
93
|
-
`Unexpected scalar '${name}'! Please add it to the "scalars" argument at the callsite of 'generateFlowTypes()'.`,
|
|
94
|
-
);
|
|
95
|
-
return babelTypes.genericTypeAnnotation(
|
|
96
|
-
babelTypes.identifier(`UNKNOWN_SCALAR["${name}"]`),
|
|
97
|
-
);
|
|
98
|
-
};
|
package/dist/enums.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/enums.js"],"names":["experimentalEnumTypeToFlow","ctx","enumConfig","description","enumDeclaration","babelTypes","identifier","name","enumStringBody","enumValues","map","v","enumDefaultedMember","experimentalEnumsMap","genericTypeAnnotation","id","enumTypeToFlow","schema","enumsByName","combinedDescription","n","replace","join","unionTypeAnnotation","stringLiteralTypeAnnotation","builtinScalars","Boolean","String","DateTime","Date","ID","Int","Float","scalarTypeToFlow","underlyingType","scalars","errors","push"],"mappings":";;;;;;;AAIA;;AAGA;;;;;;AANA;AACA;AACA;AAOO,MAAMA,0BAA0B,GAAG,CACtCC,GADsC,EAEtCC,UAFsC,EAGtCC,WAHsC,KAIlB;AACpB,QAAMC,eAAe,GAAGC,UAAU,CAACD,eAAX,EACpB;AACAC,EAAAA,UAAU,CAACC,UAAX,CAAsBJ,UAAU,CAACK,IAAjC,CAFoB,EAGpBF,UAAU,CAACG,cAAX,CACIN,UAAU,CAACO,UAAX,CAAsBC,GAAtB,CAA2BC,CAAD,IACtBN,UAAU,CAACO,mBAAX,CAA+BP,UAAU,CAACC,UAAX,CAAsBK,CAAC,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A,QAAQ,EAAE,QAHyC;AAInDC,EAAAA,IAAI,EAAE,QAJ6C;AAKnDC,EAAAA,EAAE,EAAE,QAL+C;AAMnDC,EAAAA,GAAG,EAAE,QAN8C;AAOnDC,EAAAA,KAAK,EAAE;AAP4C,CAAhD;;;AAUA,MAAMC,gBAAgB,GAAG,CAC5BhC,GAD4B,EAE5BM,IAF4B,KAGR;AACpB,MAAIkB,cAAc,CAAClB,IAAD,CAAlB,EAA0B;AACtB,WAAOF,UAAU,CAACS,qBAAX,CACHT,UAAU,CAACC,UAAX,CAAsBmB,cAAc,CAAClB,IAAD,CAApC,CADG,CAAP;AAGH;;AACD,QAAM2B,cAAc,GAAGjC,GAAG,CAACkC,OAAJ,CAAY5B,IAAZ,CAAvB;;AACA,MAAI2B,cAAc,IAAI,IAAtB,EAA4B;AACxB,WAAO7B,UAAU,CAACS,qBAAX,CACHT,UAAU,CAACC,UAAX,CAAsB4B,cAAtB,CADG,CAAP;AAGH;;AACDjC,EAAAA,GAAG,CAACmC,MAAJ,CAAWC,IAAX,CACK,sBAAqB9B,IAAK,sFAD/B;AAGA,SAAOF,UAAU,CAACS,qBAAX,CACHT,UAAU,CAACC,UAAX,CAAuB,mBAAkBC,IAAK,IAA9C,CADG,CAAP;AAGH,CArBM","sourcesContent":["// @flow\n/**\n * Both input & output types can have enums & scalars.\n */\nimport * as babelTypes from '@babel/types';\nimport type {BabelNodeFlowType} from '@babel/types';\nimport type {Context} from './types';\nimport {maybeAddDescriptionComment} from './utils';\nimport type {IntrospectionEnumType} from 'graphql/utilities/introspectionQuery';\n\nexport const experimentalEnumTypeToFlow = (\n ctx: Context,\n enumConfig: IntrospectionEnumType,\n description: string,\n): BabelNodeFlowType => {\n const enumDeclaration = babelTypes.enumDeclaration(\n // pass id into generic type annotation\n babelTypes.identifier(enumConfig.name),\n babelTypes.enumStringBody(\n enumConfig.enumValues.map((v) =>\n babelTypes.enumDefaultedMember(babelTypes.identifier(v.name)),\n ),\n ),\n );\n\n if (ctx.experimentalEnumsMap) {\n ctx.experimentalEnumsMap[enumConfig.name] = enumDeclaration;\n }\n\n return maybeAddDescriptionComment(\n description,\n babelTypes.genericTypeAnnotation(enumDeclaration.id),\n );\n};\n\nexport const enumTypeToFlow = (\n ctx: Context,\n name: string,\n): BabelNodeFlowType => {\n const enumConfig = ctx.schema.enumsByName[name];\n let combinedDescription = enumConfig.enumValues\n .map(\n (n) =>\n `- ${n.name}` +\n (n.description\n ? '\\n\\n ' + n.description.replace(/\\n/g, '\\n ')\n : ''),\n )\n .join('\\n');\n if (enumConfig.description) {\n combinedDescription =\n enumConfig.description + '\\n\\n' + combinedDescription;\n }\n\n return ctx.experimentalEnumsMap\n ? experimentalEnumTypeToFlow(ctx, enumConfig, combinedDescription)\n : maybeAddDescriptionComment(\n combinedDescription,\n babelTypes.unionTypeAnnotation(\n enumConfig.enumValues.map((n) =>\n babelTypes.stringLiteralTypeAnnotation(n.name),\n ),\n ),\n );\n};\n\nexport const builtinScalars: {[key: string]: string} = {\n Boolean: 'boolean',\n String: 'string',\n DateTime: 'string',\n Date: 'string',\n ID: 'string',\n Int: 'number',\n Float: 'number',\n};\n\nexport const scalarTypeToFlow = (\n ctx: Context,\n name: string,\n): BabelNodeFlowType => {\n if (builtinScalars[name]) {\n return babelTypes.genericTypeAnnotation(\n babelTypes.identifier(builtinScalars[name]),\n );\n }\n const underlyingType = ctx.scalars[name];\n if (underlyingType != null) {\n return babelTypes.genericTypeAnnotation(\n babelTypes.identifier(underlyingType),\n );\n }\n ctx.errors.push(\n `Unexpected scalar '${name}'! Please add it to the \"scalars\" argument at the callsite of 'generateFlowTypes()'.`,\n );\n return babelTypes.genericTypeAnnotation(\n babelTypes.identifier(`UNKNOWN_SCALAR[\"${name}\"]`),\n );\n};\n"],"file":"enums.js"}
|