@khanacademy/graphql-flow 1.1.2 → 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 +16 -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 +13 -35
- package/dist/generateVariablesType.js +15 -31
- package/dist/index.js +11 -17
- package/dist/parser/parse.js +10 -8
- package/dist/parser/resolve.js +11 -8
- package/dist/parser/utils.js +36 -0
- package/dist/schemaFromIntrospectionData.js +1 -2
- package/dist/types.js +1 -2
- package/dist/utils.js +43 -3
- package/package.json +8 -7
- package/{src/cli/schema.json → schema.json} +3 -0
- 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/{dist/generateResponseType.js.flow → src/generateResponseType.ts} +167 -182
- package/src/{generateTypeFiles.js → generateTypeFiles.ts} +24 -40
- package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
- package/{dist/index.js.flow → src/index.ts} +33 -24
- package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
- package/{dist/parser/parse.js.flow → src/parser/parse.ts} +69 -48
- package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +25 -19
- package/src/parser/utils.ts +24 -0
- 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/cli/schema.json +0 -94
- package/dist/enums.js.flow +0 -98
- package/dist/enums.js.map +0 -1
- package/dist/generateResponseType.js.map +0 -1
- package/dist/generateTypeFiles.js.flow +0 -197
- 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.map +0 -1
- package/dist/parser/resolve.js.map +0 -1
- package/dist/schemaFromIntrospectionData.js.map +0 -1
- package/dist/types.js.flow +0 -87
- 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/generateResponseType.js +0 -583
- package/src/index.js +0 -159
- package/src/parser/__test__/parse.test.js +0 -249
- package/src/parser/parse.js +0 -414
- package/src/parser/resolve.js +0 -117
- package/src/schemaFromIntrospectionData.js +0 -68
- package/src/types.js +0 -87
- package/src/utils.js +0 -50
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
scalar PositiveNumber
|
|
2
|
-
|
|
3
|
-
enum Episode { NEW_HOPE, EMPIRE, JEDI }
|
|
4
|
-
|
|
5
|
-
"""
|
|
6
|
-
A movie character
|
|
7
|
-
"""
|
|
8
|
-
interface Character {
|
|
9
|
-
id: String!
|
|
10
|
-
name: String
|
|
11
|
-
friends: [Character]
|
|
12
|
-
appearsIn: [Episode]
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
"""
|
|
16
|
-
A human character
|
|
17
|
-
"""
|
|
18
|
-
type Human implements Character {
|
|
19
|
-
id: String!
|
|
20
|
-
"""The person's name"""
|
|
21
|
-
name: String
|
|
22
|
-
friends: [Character]
|
|
23
|
-
appearsIn: [Episode]
|
|
24
|
-
homePlanet: String
|
|
25
|
-
alive: Boolean
|
|
26
|
-
hands: PositiveNumber
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
"""
|
|
30
|
-
A robot character
|
|
31
|
-
"""
|
|
32
|
-
type Droid implements Character {
|
|
33
|
-
id: String!
|
|
34
|
-
name: String
|
|
35
|
-
friends: [Character]
|
|
36
|
-
appearsIn: [Episode]
|
|
37
|
-
"""The robot's primary function"""
|
|
38
|
-
primaryFunction: String!
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type Animal {
|
|
42
|
-
name: String
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
union Friendable = Human | Droid | Animal
|
|
46
|
-
|
|
47
|
-
type Query {
|
|
48
|
-
hero(episode: Episode): Character
|
|
49
|
-
human(id: String!): Human
|
|
50
|
-
droid(id: String!): Droid
|
|
51
|
-
friend(id: String!): Friendable
|
|
52
|
-
candies(number: PositiveNumber!): String
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
"""A character to add"""
|
|
56
|
-
input CharacterInput {
|
|
57
|
-
"""The new character's name"""
|
|
58
|
-
name: String!
|
|
59
|
-
"""The character's friends"""
|
|
60
|
-
friends: [ID!]
|
|
61
|
-
appearsIn: [Episode!]
|
|
62
|
-
candies: PositiveNumber!
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
type Mutation {
|
|
66
|
-
addCharacter(character: CharacterInput!): Character
|
|
67
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import type {CrawlConfig, GenerateConfig} from '../types';
|
|
3
|
-
|
|
4
|
-
import {processPragmas} from '../generateTypeFiles';
|
|
5
|
-
|
|
6
|
-
const pragma = '# @autogen\n';
|
|
7
|
-
const loosePragma = '# @autogen-loose\n';
|
|
8
|
-
|
|
9
|
-
const baseGenerate: GenerateConfig = {schemaFilePath: ''};
|
|
10
|
-
const baseCrawl: CrawlConfig = {root: ''};
|
|
11
|
-
|
|
12
|
-
describe('processPragmas', () => {
|
|
13
|
-
it('should work with no pragmas', () => {
|
|
14
|
-
expect(
|
|
15
|
-
processPragmas(baseGenerate, baseCrawl, `query X { Y }`),
|
|
16
|
-
).toEqual({
|
|
17
|
-
generate: true,
|
|
18
|
-
strict: undefined,
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should reject query without required pragma', () => {
|
|
23
|
-
expect(
|
|
24
|
-
processPragmas(
|
|
25
|
-
baseGenerate,
|
|
26
|
-
{...baseCrawl, pragma},
|
|
27
|
-
`query X { Y }`,
|
|
28
|
-
),
|
|
29
|
-
).toEqual({generate: false});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should accept query with required pragma', () => {
|
|
33
|
-
expect(
|
|
34
|
-
processPragmas(
|
|
35
|
-
baseGenerate,
|
|
36
|
-
{...baseCrawl, pragma},
|
|
37
|
-
`query X {
|
|
38
|
-
# @autogen
|
|
39
|
-
Y
|
|
40
|
-
}`,
|
|
41
|
-
),
|
|
42
|
-
).toEqual({
|
|
43
|
-
strict: true,
|
|
44
|
-
generate: true,
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should accept query with loose pragma', () => {
|
|
49
|
-
expect(
|
|
50
|
-
processPragmas(
|
|
51
|
-
baseGenerate,
|
|
52
|
-
{...baseCrawl, pragma, loosePragma},
|
|
53
|
-
`query X {
|
|
54
|
-
# @autogen-loose
|
|
55
|
-
Y
|
|
56
|
-
}`,
|
|
57
|
-
),
|
|
58
|
-
).toEqual({
|
|
59
|
-
strict: false,
|
|
60
|
-
generate: true,
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should reject query with ignore pragma', () => {
|
|
65
|
-
expect(
|
|
66
|
-
processPragmas(
|
|
67
|
-
baseGenerate,
|
|
68
|
-
{...baseCrawl, ignorePragma: '# @ignore\n'},
|
|
69
|
-
`query X {
|
|
70
|
-
# @ignore
|
|
71
|
-
Y
|
|
72
|
-
}`,
|
|
73
|
-
),
|
|
74
|
-
).toEqual({generate: false});
|
|
75
|
-
});
|
|
76
|
-
});
|
package/dist/cli/config.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/config.js"],"names":["validateOrThrow","value","jsonSchema","result","valid","Error","errors","map","error","toString","join","loadConfigFile","configFile","data","require","configSchema","getSchemas","schemaFilePath","raw","fs","readFileSync","endsWith","schemaForValidation","queryResponse","descriptions","schemaForTypeGeneration","introspectionData","JSON","parse","findApplicableConfig","path","configs","Array","isArray","find","config","exclude","some","RegExp","test","match","matcher"],"mappings":";;;;;;;AAIA;;AACA;;AAEA;;AACA;;AAQA;;;;AAX0C;AAWL;AAE9B,MAAMA,eAAe,GAAG,CAACC,KAAD,EAAeC,UAAf,KAAqC;AAChE;AACA,QAAMC,MAAM,GAAG,0BAASF,KAAT,EAAgBC,UAAhB,CAAf;;AACA,MAAI,CAACC,MAAM,CAACC,KAAZ,EAAmB;AACf,UAAM,IAAIC,KAAJ,CACFF,MAAM,CAACG,MAAP,CAAcC,GAAd,CAAmBC,KAAD,IAAWA,KAAK,CAACC,QAAN,EAA7B,EAA+CC,IAA/C,CAAoD,IAApD,CADE,CAAN;AAGH;AACD;;AACH,CATM;;;;AAWA,MAAMC,cAAc,GAAIC,UAAD,IAAgC;AAC1D;AACA,QAAMC,IAAY,GAAGC,OAAO,CAACF,UAAD,CAA5B,CAF0D,CAEhB;;;AAC1CZ,EAAAA,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AuE;AACnE,aAAO,KAAP;AACH;;AACD,QAAI,CAACK,MAAM,CAACK,KAAZ,EAAmB;AACf,aAAO,IAAP;AACH;;AACD,WAAOL,MAAM,CAACK,KAAP,CAAaH,IAAb,CAAmBI,OAAD,IAAa,IAAIH,MAAJ,CAAWG,OAAX,EAAoBF,IAApB,CAAyBT,IAAzB,CAA/B,CAAP;AACH,GARM,CAAP;AASH,CAhBM","sourcesContent":["// @flow\nimport type {Schema} from '../types';\nimport type {GraphQLSchema} from 'graphql/type/schema';\n\nimport {schemaFromIntrospectionData} from '../schemaFromIntrospectionData';\nimport configSchema from './schema.json'; // eslint-disable-line flowtype-errors/uncovered\n\nimport fs from 'fs';\nimport {\n buildClientSchema,\n buildSchema,\n getIntrospectionQuery,\n graphqlSync,\n type IntrospectionQuery,\n} from 'graphql';\nimport type {Config, GenerateConfig} from '../types';\nimport {validate} from 'jsonschema'; // eslint-disable-line flowtype-errors/uncovered\n\nexport const validateOrThrow = (value: mixed, jsonSchema: mixed) => {\n /* eslint-disable flowtype-errors/uncovered */\n const result = validate(value, jsonSchema);\n if (!result.valid) {\n throw new Error(\n result.errors.map((error) => error.toString()).join('\\n'),\n );\n }\n /* eslint-enable flowtype-errors/uncovered */\n};\n\nexport const loadConfigFile = (configFile: string): Config => {\n // $FlowIgnore\n const data: Config = require(configFile); // eslint-disable-line flowtype-errors/uncovered\n validateOrThrow(data, configSchema); // eslint-disable-line flowtype-errors/uncovered\n return data;\n};\n\n/**\n * Loads a .json 'introspection query response', or a .graphql schema definition.\n */\nexport const getSchemas = (schemaFilePath: string): [GraphQLSchema, Schema] => {\n const raw = fs.readFileSync(schemaFilePath, 'utf8');\n if (schemaFilePath.endsWith('.graphql')) {\n const schemaForValidation = buildSchema(raw);\n const queryResponse = graphqlSync(\n schemaForValidation,\n getIntrospectionQuery({descriptions: true}),\n );\n const schemaForTypeGeneration = schemaFromIntrospectionData(\n // eslint-disable-next-line flowtype-errors/uncovered\n ((queryResponse.data: any): IntrospectionQuery),\n );\n return [schemaForValidation, schemaForTypeGeneration];\n } else {\n // eslint-disable-next-line flowtype-errors/uncovered\n const introspectionData: IntrospectionQuery = JSON.parse(raw);\n const schemaForValidation = buildClientSchema(introspectionData);\n const schemaForTypeGeneration =\n schemaFromIntrospectionData(introspectionData);\n return [schemaForValidation, schemaForTypeGeneration];\n }\n};\n\n/**\n * Find the first item of the `config.generate` array where both:\n * - no item of `exclude` matches\n * - at least one item of `match` matches\n */\nexport const findApplicableConfig = (\n path: string,\n configs: Array<GenerateConfig> | GenerateConfig,\n): ?GenerateConfig => {\n if (!Array.isArray(configs)) {\n configs = [configs];\n }\n return configs.find((config) => {\n if (config.exclude?.some((exclude) => new RegExp(exclude).test(path))) {\n return false;\n }\n if (!config.match) {\n return true;\n }\n return config.match.some((matcher) => new RegExp(matcher).test(path));\n });\n};\n"],"file":"config.js"}
|
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,IA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AAK,GADvD;AAGAQ,QAAAA,OAAO,CAACyB,KAAR,CAAcqB,OAAd;AACA9C,QAAAA,OAAO,CAACyB,KAAR,CAAcA,KAAd;AACAO,QAAAA,kBAAkB;AACrB,OAPD;AAQH;AACD;;AACH;;AAED,MAAI;AACA,8CACII,GAAG,CAACR,GAAJ,CAAQpC,IADZ,EAEI6D,uBAFJ,EAGIR,aAHJ,EAIIF,cAJJ,EADA,CAOA;AACH,GARD,CAQE,OAAOW,GAAP,EAAY;AACVtD,IAAAA,OAAO,CAACyB,KAAR,CAAe,yCAAwCW,GAAG,CAACR,GAAJ,CAAQpC,IAAK,EAApE;AACAQ,IAAAA,OAAO,CAACyB,KAAR,CAAcqB,OAAd,EAFU,CAGV;;AACA9C,IAAAA,OAAO,CAACyB,KAAR,CAAc6B,GAAd;AACAtB,IAAAA,kBAAkB;AACrB;AACJ,CAxED;;AA0EA,IAAIA,kBAAJ,EAAwB;AACpBhC,EAAAA,OAAO,CAACyB,KAAR,CACK,eAAcO,kBAAmB,4CADtC,EADoB,CAIpB;;AACAlC,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/cli/schema.json
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema":"http://json-schema.org/draft-07/schema",
|
|
3
|
-
"type": "object",
|
|
4
|
-
"additionalProperties": false,
|
|
5
|
-
"definitions": {
|
|
6
|
-
"GenerateConfig": {
|
|
7
|
-
"type": "object",
|
|
8
|
-
"additionalProperties": false,
|
|
9
|
-
"properties": {
|
|
10
|
-
"schemaFilePath": {
|
|
11
|
-
"type": "string"
|
|
12
|
-
},
|
|
13
|
-
"match": {
|
|
14
|
-
"type": "array",
|
|
15
|
-
"items": {
|
|
16
|
-
"oneOf": [
|
|
17
|
-
{ "type": "object" },
|
|
18
|
-
{ "type": "string" }
|
|
19
|
-
]
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
"exclude": {
|
|
23
|
-
"type": "array",
|
|
24
|
-
"items": {
|
|
25
|
-
"oneOf": [
|
|
26
|
-
{ "type": "object" },
|
|
27
|
-
{ "type": "string" }
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"scalars": {
|
|
32
|
-
"type": "object"
|
|
33
|
-
},
|
|
34
|
-
"typeScript": {
|
|
35
|
-
"type": "boolean"
|
|
36
|
-
},
|
|
37
|
-
"strictNullability": {
|
|
38
|
-
"type": "boolean"
|
|
39
|
-
},
|
|
40
|
-
"regenerateCommand": {
|
|
41
|
-
"type": "string"
|
|
42
|
-
},
|
|
43
|
-
"readOnlyArray": {
|
|
44
|
-
"type": "boolean"
|
|
45
|
-
},
|
|
46
|
-
"splitTypes": {
|
|
47
|
-
"type": "boolean"
|
|
48
|
-
},
|
|
49
|
-
"generatedDirectory": {
|
|
50
|
-
"type": "string"
|
|
51
|
-
},
|
|
52
|
-
"exportAllObjectTypes": {
|
|
53
|
-
"type": "boolean"
|
|
54
|
-
},
|
|
55
|
-
"typeFileName": {
|
|
56
|
-
"type": "string"
|
|
57
|
-
},
|
|
58
|
-
"experimentalEnums": {
|
|
59
|
-
"type": "boolean"
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
"required": [
|
|
63
|
-
"schemaFilePath"
|
|
64
|
-
]
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
"properties": {
|
|
68
|
-
"crawl": {
|
|
69
|
-
"type": "object",
|
|
70
|
-
"additionalProperties": false,
|
|
71
|
-
"properties": {
|
|
72
|
-
"root": {"type": "string"},
|
|
73
|
-
"pragma": {
|
|
74
|
-
"type": "string"
|
|
75
|
-
},
|
|
76
|
-
"loosePragma": {
|
|
77
|
-
"type": "string"
|
|
78
|
-
},
|
|
79
|
-
"ignorePragma": {
|
|
80
|
-
"type": "string"
|
|
81
|
-
},
|
|
82
|
-
"dumpOperations": {
|
|
83
|
-
"type": "string"
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
"required": [ "root" ]
|
|
87
|
-
},
|
|
88
|
-
"generate": {"oneOf": [
|
|
89
|
-
{"$ref": "#/definitions/GenerateConfig"},
|
|
90
|
-
{"type": "array", "items": {"$ref": "#/definitions/GenerateConfig"}}
|
|
91
|
-
]}
|
|
92
|
-
},
|
|
93
|
-
"required": [ "crawl", "generate" ]
|
|
94
|
-
}
|
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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CJ,CAAC,CAACb,IAAzC,CADJ,CADJ,CAFJ,CAFN;AAUH,CA7BM;;;AA+BA,MAAMkB,cAAuC,GAAG;AACnDC,EAAAA,OAAO,EAAE,SAD0C;AAEnDC,EAAAA,MAAM,EAAE,QAF2C;AAGnDC,EAAAA,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"}
|