@khanacademy/graphql-flow 0.2.2 → 0.2.5
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/changeset-release.yml +2 -16
- package/CHANGELOG.md +18 -0
- package/Readme.md +15 -1
- package/dist/cli/config.js +4 -3
- package/dist/cli/config.js.flow +13 -5
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/run.js +35 -9
- package/dist/cli/run.js.flow +30 -6
- package/dist/cli/run.js.map +1 -1
- package/dist/generateTypeFiles.js +18 -4
- package/dist/generateTypeFiles.js.flow +23 -5
- package/dist/generateTypeFiles.js.map +1 -1
- package/dist/types.js.flow +1 -0
- package/package.json +1 -1
- package/src/__test__/generateTypeFileContents.test.js +42 -0
- package/src/cli/config.js +13 -5
- package/src/cli/run.js +30 -6
- package/src/generateTypeFiles.js +23 -5
- package/src/types.js +1 -0
|
@@ -55,8 +55,8 @@ jobs:
|
|
|
55
55
|
if: steps.changesets.outputs.published == 'true'
|
|
56
56
|
uses: rtCamp/action-slack-notify@v2
|
|
57
57
|
env:
|
|
58
|
-
SLACK_WEBHOOK: ${{ secrets.
|
|
59
|
-
SLACK_CHANNEL:
|
|
58
|
+
SLACK_WEBHOOK: ${{ secrets.SLACK_FEI_FIREHOSE }}
|
|
59
|
+
SLACK_CHANNEL: fei-firehose
|
|
60
60
|
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
61
61
|
SLACK_USERNAME: GithubGoose
|
|
62
62
|
SLACK_ICON_EMOJI: ":goose:"
|
|
@@ -64,17 +64,3 @@ jobs:
|
|
|
64
64
|
SLACK_TITLE: "New Graphql-Flow release!"
|
|
65
65
|
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
66
66
|
MSG_MINIMAL: true
|
|
67
|
-
|
|
68
|
-
- name: Send a Slack notification for mobile if a publish happens
|
|
69
|
-
if: steps.changesets.outputs.published == 'true'
|
|
70
|
-
uses: rtCamp/action-slack-notify@v2
|
|
71
|
-
env:
|
|
72
|
-
SLACK_WEBHOOK: ${{ secrets.SLACK_FEIMOBILE_WEBHOOK }}
|
|
73
|
-
SLACK_CHANNEL: frontend-infra-mobile
|
|
74
|
-
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
75
|
-
SLACK_USERNAME: GithubGoose
|
|
76
|
-
SLACK_ICON_EMOJI: ":goose:"
|
|
77
|
-
SLACK_MESSAGE: "A new version of ${{ github.event.repository.name }} was published! 🎉 \nRelease notes → https://github.com/Khan/${{ github.event.repository.name }}/releases/"
|
|
78
|
-
SLACK_TITLE: "New Graphql-Flow release!"
|
|
79
|
-
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
80
|
-
MSG_MINIMAL: true
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @khanacademy/graphql-flow
|
|
2
2
|
|
|
3
|
+
## 0.2.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0df32ac: Allow generatedDir to be an absolute path
|
|
8
|
+
|
|
9
|
+
## 0.2.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 843839c: Add 'dumpOperations' config option, to enable safelisting
|
|
14
|
+
|
|
15
|
+
## 0.2.3
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- f5ea353: Add 'typeFileName' config option to fit the mobile repo's expectations
|
|
20
|
+
|
|
3
21
|
## 0.2.2
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/Readme.md
CHANGED
|
@@ -42,7 +42,17 @@ type Options = {
|
|
|
42
42
|
readOnlyArray: boolean = true,
|
|
43
43
|
scalars: {[key: string]: 'string' | 'boolean' | 'number'}
|
|
44
44
|
|
|
45
|
-
// Specify the name of the generated types directory
|
|
45
|
+
// Specify the name of the generated types directory. If this
|
|
46
|
+
// is a relative path, then this is used to suffix the output
|
|
47
|
+
// directory; if it's an absolute path it's used to prefix the
|
|
48
|
+
// output directory. For instance, if a gql directive is
|
|
49
|
+
// found in /foo/bar/baz/query.js and you run the cli (or
|
|
50
|
+
// jest) from directory /foo, then:
|
|
51
|
+
// * if `generatedDirectory` is "__generated__", the output will
|
|
52
|
+
// be in /foo/bar/baz/__generated__/index.js and sibling files
|
|
53
|
+
// * if `generatedDirectory` is "/tmp/__generated__", the output
|
|
54
|
+
// will be in /tmp/__generated__/bar/baz/index.js and sibling
|
|
55
|
+
// files.
|
|
46
56
|
generatedDirectory: string = '__generated__',
|
|
47
57
|
|
|
48
58
|
// The default generated type contains both the types of the response
|
|
@@ -70,6 +80,10 @@ type Options = {
|
|
|
70
80
|
// Names are generated by concatenating the attribute names
|
|
71
81
|
// of the path to the object type, separated by underscores.
|
|
72
82
|
exportAllObjectTypes?: boolean,
|
|
83
|
+
|
|
84
|
+
// A template for the name of generated files
|
|
85
|
+
// default: [operationName].js
|
|
86
|
+
typeFileName?: string,
|
|
73
87
|
}
|
|
74
88
|
```
|
|
75
89
|
|
package/dist/cli/config.js
CHANGED
|
@@ -20,7 +20,7 @@ const loadConfigFile = configFile => {
|
|
|
20
20
|
|
|
21
21
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
22
22
|
const data = JSON.parse(_fs.default.readFileSync(configFile, 'utf8'));
|
|
23
|
-
const toplevelKeys = ['excludes', 'schemaFilePath', 'options'];
|
|
23
|
+
const toplevelKeys = ['excludes', 'schemaFilePath', 'options', 'dumpOperations'];
|
|
24
24
|
Object.keys(data).forEach(k => {
|
|
25
25
|
if (!toplevelKeys.includes(k)) {
|
|
26
26
|
throw new Error(`Invalid attribute in config file ${configFile}: ${k}. Allowed attributes: ${toplevelKeys.join(', ')}`);
|
|
@@ -28,7 +28,7 @@ const loadConfigFile = configFile => {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
if (data.options) {
|
|
31
|
-
const externalOptionsKeys = ['pragma', 'loosePragma', 'ignorePragma', 'scalars', 'strictNullability', 'regenerateCommand', 'readOnlyArray', 'splitTypes', 'generatedDirectory', 'exportAllObjectTypes'];
|
|
31
|
+
const externalOptionsKeys = ['pragma', 'loosePragma', 'ignorePragma', 'scalars', 'strictNullability', 'regenerateCommand', 'readOnlyArray', 'splitTypes', 'generatedDirectory', 'exportAllObjectTypes', 'typeFileName'];
|
|
32
32
|
Object.keys(data.options).forEach(k => {
|
|
33
33
|
if (!externalOptionsKeys.includes(k)) {
|
|
34
34
|
throw new Error(`Invalid option in config file ${configFile}: ${k}. Allowed options: ${externalOptionsKeys.join(', ')}`);
|
|
@@ -39,7 +39,8 @@ const loadConfigFile = configFile => {
|
|
|
39
39
|
return {
|
|
40
40
|
options: (_data$options = data.options) !== null && _data$options !== void 0 ? _data$options : {},
|
|
41
41
|
excludes: (_data$excludes$map = (_data$excludes = data.excludes) === null || _data$excludes === void 0 ? void 0 : _data$excludes.map(string => new RegExp(string))) !== null && _data$excludes$map !== void 0 ? _data$excludes$map : [],
|
|
42
|
-
schemaFilePath: _path.default.join(_path.default.dirname(configFile), data.schemaFilePath)
|
|
42
|
+
schemaFilePath: _path.default.isAbsolute(data.schemaFilePath) ? data.schemaFilePath : _path.default.join(_path.default.dirname(configFile), data.schemaFilePath),
|
|
43
|
+
dumpOperations: data.dumpOperations
|
|
43
44
|
};
|
|
44
45
|
};
|
|
45
46
|
/**
|
package/dist/cli/config.js.flow
CHANGED
|
@@ -18,6 +18,7 @@ import path from 'path';
|
|
|
18
18
|
export type CliConfig = {
|
|
19
19
|
excludes: Array<RegExp>,
|
|
20
20
|
schemaFilePath: string,
|
|
21
|
+
dumpOperations?: string,
|
|
21
22
|
options: ExternalOptions,
|
|
22
23
|
};
|
|
23
24
|
|
|
@@ -29,12 +30,18 @@ type JSONConfig = {
|
|
|
29
30
|
excludes?: Array<string>,
|
|
30
31
|
schemaFilePath: string,
|
|
31
32
|
options?: ExternalOptions,
|
|
33
|
+
dumpOperations?: string,
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
export const loadConfigFile = (configFile: string): CliConfig => {
|
|
35
37
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
36
38
|
const data: JSONConfig = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
37
|
-
const toplevelKeys = [
|
|
39
|
+
const toplevelKeys = [
|
|
40
|
+
'excludes',
|
|
41
|
+
'schemaFilePath',
|
|
42
|
+
'options',
|
|
43
|
+
'dumpOperations',
|
|
44
|
+
];
|
|
38
45
|
Object.keys(data).forEach((k) => {
|
|
39
46
|
if (!toplevelKeys.includes(k)) {
|
|
40
47
|
throw new Error(
|
|
@@ -56,6 +63,7 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
56
63
|
'splitTypes',
|
|
57
64
|
'generatedDirectory',
|
|
58
65
|
'exportAllObjectTypes',
|
|
66
|
+
'typeFileName',
|
|
59
67
|
];
|
|
60
68
|
Object.keys(data.options).forEach((k) => {
|
|
61
69
|
if (!externalOptionsKeys.includes(k)) {
|
|
@@ -70,10 +78,10 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
70
78
|
return {
|
|
71
79
|
options: data.options ?? {},
|
|
72
80
|
excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],
|
|
73
|
-
schemaFilePath: path.
|
|
74
|
-
|
|
75
|
-
data.schemaFilePath,
|
|
76
|
-
|
|
81
|
+
schemaFilePath: path.isAbsolute(data.schemaFilePath)
|
|
82
|
+
? data.schemaFilePath
|
|
83
|
+
: path.join(path.dirname(configFile), data.schemaFilePath),
|
|
84
|
+
dumpOperations: data.dumpOperations,
|
|
77
85
|
};
|
|
78
86
|
};
|
|
79
87
|
|
package/dist/cli/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/config.js"],"names":["loadConfigFile","configFile","data","JSON","parse","fs","readFileSync","toplevelKeys","Object","keys","forEach","k","includes","Error","join","options","externalOptionsKeys","excludes","map","string","RegExp","schemaFilePath","path","dirname","getSchemas","raw","endsWith","schemaForValidation","queryResponse","descriptions","schemaForTypeGeneration","introspectionData"],"mappings":";;;;;;;AAKA;;AAEA;;AACA;;AAOA;;;;
|
|
1
|
+
{"version":3,"sources":["../../src/cli/config.js"],"names":["loadConfigFile","configFile","data","JSON","parse","fs","readFileSync","toplevelKeys","Object","keys","forEach","k","includes","Error","join","options","externalOptionsKeys","excludes","map","string","RegExp","schemaFilePath","path","isAbsolute","dirname","dumpOperations","getSchemas","raw","endsWith","schemaForValidation","queryResponse","descriptions","schemaForTypeGeneration","introspectionData"],"mappings":";;;;;;;AAKA;;AAEA;;AACA;;AAOA;;;;AAoBO,MAAMA,cAAc,GAAIC,UAAD,IAAmC;AAAA;;AAC7D;AACA,QAAMC,IAAgB,GAAGC,IAAI,CAACC,KAAL,CAAWC,YAAGC,YAAH,CAAgBL,UAAhB,EAA4B,MAA5B,CAAX,CAAzB;AACA,QAAMM,YAAY,GAAG,CACjB,UADiB,EAEjB,gBAFiB,EAGjB,SAHiB,EAIjB,gBAJiB,CAArB;AAMAC,EAAAA,MAAM,CAACC,IAAP,CAAYP,IAAZ,EAAkBQ,OAAlB,CAA2BC,CAAD,IAAO;AAC7B,QAAI,CAACJ,YAAY,CAACK,QAAb,CAAsBD,CAAtB,CAAL,EAA+B;AAC3B,YAAM,IAAIE,KAAJ,CACD,oCAAmCZ,UAAW,KAAIU,CAAE,yBAAwBJ,YAAY,CAACO,IAAb,CACzE,IADyE,CAE3E,EAHA,CAAN;AAKH;AACJ,GARD;;AASA,MAAIZ,IAAI,CAACa,OAAT,EAAkB;AACd,UAAMC,mBAAmB,GAAG,CACxB,QADwB,EAExB,aAFwB,EAGxB,cAHwB,EAIxB,SAJwB,EAKxB,mBALwB,EAMxB,mBANwB,EAOxB,eAPwB,EAQxB,YARwB,EASxB,oBATwB,EAUxB,sBAVwB,EAWxB,cAXwB,CAA5B;AAaAR,IAAAA,MAAM,CAACC,IAAP,CAAYP,IAAI,CAACa,OAAjB,EAA0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AMC,iBAAqC,GAAG9B,IAAI,CAACC,KAAL,CAAWuB,GAAX,CAA9C;AACA,UAAME,mBAAmB,GAAG,gCAAkBI,iBAAlB,CAA5B;AACA,UAAMD,uBAAuB,GACzB,8DAA4BC,iBAA5B,CADJ;AAEA,WAAO,CAACJ,mBAAD,EAAsBG,uBAAtB,CAAP;AACH;AACJ,CArBM","sourcesContent":["// @flow\nimport type {ExternalOptions} from '../generateTypeFiles';\nimport type {Schema} from '../types';\nimport type {GraphQLSchema} from 'graphql/type/schema';\n\nimport {schemaFromIntrospectionData} from '../schemaFromIntrospectionData';\n\nimport fs from 'fs';\nimport {\n buildClientSchema,\n buildSchema,\n getIntrospectionQuery,\n graphqlSync,\n type IntrospectionQuery,\n} from 'graphql';\nimport path from 'path';\n\nexport type CliConfig = {\n excludes: Array<RegExp>,\n schemaFilePath: string,\n dumpOperations?: string,\n options: ExternalOptions,\n};\n\n/**\n * This is the json-compatible form of the config\n * object.\n */\ntype JSONConfig = {\n excludes?: Array<string>,\n schemaFilePath: string,\n options?: ExternalOptions,\n dumpOperations?: string,\n};\n\nexport const loadConfigFile = (configFile: string): CliConfig => {\n // eslint-disable-next-line flowtype-errors/uncovered\n const data: JSONConfig = JSON.parse(fs.readFileSync(configFile, 'utf8'));\n const toplevelKeys = [\n 'excludes',\n 'schemaFilePath',\n 'options',\n 'dumpOperations',\n ];\n Object.keys(data).forEach((k) => {\n if (!toplevelKeys.includes(k)) {\n throw new Error(\n `Invalid attribute in config file ${configFile}: ${k}. Allowed attributes: ${toplevelKeys.join(\n ', ',\n )}`,\n );\n }\n });\n if (data.options) {\n const externalOptionsKeys = [\n 'pragma',\n 'loosePragma',\n 'ignorePragma',\n 'scalars',\n 'strictNullability',\n 'regenerateCommand',\n 'readOnlyArray',\n 'splitTypes',\n 'generatedDirectory',\n 'exportAllObjectTypes',\n 'typeFileName',\n ];\n Object.keys(data.options).forEach((k) => {\n if (!externalOptionsKeys.includes(k)) {\n throw new Error(\n `Invalid option in config file ${configFile}: ${k}. Allowed options: ${externalOptionsKeys.join(\n ', ',\n )}`,\n );\n }\n });\n }\n return {\n options: data.options ?? {},\n excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],\n schemaFilePath: path.isAbsolute(data.schemaFilePath)\n ? data.schemaFilePath\n : path.join(path.dirname(configFile), data.schemaFilePath),\n dumpOperations: data.dumpOperations,\n };\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"],"file":"config.js"}
|
package/dist/cli/run.js
CHANGED
|
@@ -21,9 +21,11 @@ var _printer = require("graphql/language/printer");
|
|
|
21
21
|
|
|
22
22
|
var _validation = require("graphql/validation");
|
|
23
23
|
|
|
24
|
-
var _path =
|
|
24
|
+
var _path = _interopRequireWildcard(require("path"));
|
|
25
25
|
|
|
26
|
-
function
|
|
26
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
+
|
|
28
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
27
29
|
|
|
28
30
|
// eslint-disable-line flowtype-errors/uncovered
|
|
29
31
|
|
|
@@ -61,7 +63,17 @@ const [schemaForValidation, schemaForTypeGeneration] = (0, _config.getSchemas)(c
|
|
|
61
63
|
const inputFiles = cliFiles.length ? cliFiles : findGraphqlTagReferences(process.cwd());
|
|
62
64
|
/** Step (2) */
|
|
63
65
|
|
|
64
|
-
const files = (0, _parse.processFiles)(inputFiles, f =>
|
|
66
|
+
const files = (0, _parse.processFiles)(inputFiles, f => {
|
|
67
|
+
if ((0, _fs.existsSync)(f)) {
|
|
68
|
+
return (0, _fs.readFileSync)(f, 'utf8');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if ((0, _fs.existsSync)(f + '.js')) {
|
|
72
|
+
return (0, _fs.readFileSync)(f + '.js', 'utf8');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
throw new Error(`Unable to find ${f}`);
|
|
76
|
+
});
|
|
65
77
|
let filesHadErrors = false;
|
|
66
78
|
Object.keys(files).forEach(key => {
|
|
67
79
|
const file = files[key];
|
|
@@ -99,6 +111,7 @@ console.log(Object.keys(resolved).length, 'resolved queries');
|
|
|
99
111
|
/** Step (4) */
|
|
100
112
|
|
|
101
113
|
let validationFailures = 0;
|
|
114
|
+
const printedOperations = [];
|
|
102
115
|
Object.keys(resolved).forEach(k => {
|
|
103
116
|
const {
|
|
104
117
|
document,
|
|
@@ -112,16 +125,20 @@ Object.keys(resolved).forEach(k => {
|
|
|
112
125
|
const hasNonFragments = document.definitions.some(({
|
|
113
126
|
kind
|
|
114
127
|
}) => kind !== 'FragmentDefinition');
|
|
115
|
-
const rawSource = raw.literals[0];
|
|
128
|
+
const rawSource = raw.literals[0]; // eslint-disable-next-line flowtype-errors/uncovered
|
|
129
|
+
|
|
130
|
+
const withTypeNames = (0, _apolloUtilities.addTypenameToDocument)(document);
|
|
131
|
+
const printed = (0, _printer.print)(withTypeNames);
|
|
132
|
+
|
|
133
|
+
if (hasNonFragments && !printedOperations.includes(printed)) {
|
|
134
|
+
printedOperations.push(printed);
|
|
135
|
+
}
|
|
136
|
+
|
|
116
137
|
const processedOptions = (0, _generateTypeFiles.processPragmas)(config.options, rawSource);
|
|
117
138
|
|
|
118
139
|
if (!processedOptions) {
|
|
119
140
|
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const withTypeNames = (0, _apolloUtilities.addTypenameToDocument)(document);
|
|
124
|
-
const printed = (0, _printer.print)(withTypeNames);
|
|
141
|
+
}
|
|
125
142
|
|
|
126
143
|
if (hasNonFragments) {
|
|
127
144
|
/* eslint-disable flowtype-errors/uncovered */
|
|
@@ -156,4 +173,13 @@ if (validationFailures) {
|
|
|
156
173
|
|
|
157
174
|
process.exit(1);
|
|
158
175
|
}
|
|
176
|
+
|
|
177
|
+
if (config.dumpOperations) {
|
|
178
|
+
const dumpOperations = config.dumpOperations;
|
|
179
|
+
const parent = (0, _path.dirname)(dumpOperations);
|
|
180
|
+
(0, _fs.mkdirSync)(parent, {
|
|
181
|
+
recursive: true
|
|
182
|
+
});
|
|
183
|
+
(0, _fs.writeFileSync)(dumpOperations, JSON.stringify(printedOperations.sort(), null, 2));
|
|
184
|
+
}
|
|
159
185
|
//# sourceMappingURL=run.js.map
|
package/dist/cli/run.js.flow
CHANGED
|
@@ -9,11 +9,12 @@ import {getSchemas, loadConfigFile} from './config';
|
|
|
9
9
|
import {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line flowtype-errors/uncovered
|
|
10
10
|
|
|
11
11
|
import {execSync} from 'child_process';
|
|
12
|
-
import {readFileSync} from 'fs';
|
|
12
|
+
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
|
|
13
13
|
import {type DocumentNode} from 'graphql';
|
|
14
14
|
import {print} from 'graphql/language/printer';
|
|
15
15
|
import {validate} from 'graphql/validation';
|
|
16
16
|
import path from 'path';
|
|
17
|
+
import {dirname} from 'path';
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* This CLI tool executes the following steps:
|
|
@@ -68,7 +69,15 @@ const inputFiles = cliFiles.length
|
|
|
68
69
|
|
|
69
70
|
/** Step (2) */
|
|
70
71
|
|
|
71
|
-
const files = processFiles(inputFiles, (f) =>
|
|
72
|
+
const files = processFiles(inputFiles, (f) => {
|
|
73
|
+
if (existsSync(f)) {
|
|
74
|
+
return readFileSync(f, 'utf8');
|
|
75
|
+
}
|
|
76
|
+
if (existsSync(f + '.js')) {
|
|
77
|
+
return readFileSync(f + '.js', 'utf8');
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Unable to find ${f}`);
|
|
80
|
+
});
|
|
72
81
|
|
|
73
82
|
let filesHadErrors = false;
|
|
74
83
|
Object.keys(files).forEach((key) => {
|
|
@@ -103,6 +112,7 @@ console.log(Object.keys(resolved).length, 'resolved queries');
|
|
|
103
112
|
/** Step (4) */
|
|
104
113
|
|
|
105
114
|
let validationFailures: number = 0;
|
|
115
|
+
const printedOperations: Array<string> = [];
|
|
106
116
|
|
|
107
117
|
Object.keys(resolved).forEach((k) => {
|
|
108
118
|
const {document, raw} = resolved[k];
|
|
@@ -113,14 +123,18 @@ Object.keys(resolved).forEach((k) => {
|
|
|
113
123
|
({kind}) => kind !== 'FragmentDefinition',
|
|
114
124
|
);
|
|
115
125
|
const rawSource: string = raw.literals[0];
|
|
116
|
-
const processedOptions = processPragmas(config.options, rawSource);
|
|
117
|
-
if (!processedOptions) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
126
|
|
|
121
127
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
122
128
|
const withTypeNames: DocumentNode = addTypenameToDocument(document);
|
|
123
129
|
const printed = print(withTypeNames);
|
|
130
|
+
if (hasNonFragments && !printedOperations.includes(printed)) {
|
|
131
|
+
printedOperations.push(printed);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const processedOptions = processPragmas(config.options, rawSource);
|
|
135
|
+
if (!processedOptions) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
124
138
|
|
|
125
139
|
if (hasNonFragments) {
|
|
126
140
|
/* eslint-disable flowtype-errors/uncovered */
|
|
@@ -163,3 +177,13 @@ if (validationFailures) {
|
|
|
163
177
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
164
178
|
process.exit(1);
|
|
165
179
|
}
|
|
180
|
+
|
|
181
|
+
if (config.dumpOperations) {
|
|
182
|
+
const dumpOperations = config.dumpOperations;
|
|
183
|
+
const parent = dirname(dumpOperations);
|
|
184
|
+
mkdirSync(parent, {recursive: true});
|
|
185
|
+
writeFileSync(
|
|
186
|
+
dumpOperations,
|
|
187
|
+
JSON.stringify(printedOperations.sort(), null, 2),
|
|
188
|
+
);
|
|
189
|
+
}
|
package/dist/cli/run.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/run.js"],"names":["findGraphqlTagReferences","root","response","encoding","cwd","trim","split","map","relative","path","join","_","__","configFile","cliFiles","process","argv","console","log","exit","config","schemaForValidation","schemaForTypeGeneration","schemaFilePath","inputFiles","length","files","f","filesHadErrors","Object","keys","forEach","key","file","errors","error","message","resolved","loc","validationFailures","k","document","raw","excludes","some","rx","test","hasNonFragments","definitions","kind","rawSource","literals","processedOptions","options","withTypeNames","printed","err"],"mappings":"AAAA;;AAEA;;;AACA;;AAC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Ae,yCAAwCO,GAAG,CAACJ,GAAJ,CAAQ7B,IAAK,EAApE;AACAQ,IAAAA,OAAO,CAACkB,KAAR,CAAcoB,OAAd,EAFU,CAGV;;AACAtC,IAAAA,OAAO,CAACkB,KAAR,CAAcqB,GAAd;AACAjB,IAAAA,kBAAkB;AACrB;AACJ,CAlDD;;AAoDA,IAAIA,kBAAJ,EAAwB;AACpBtB,EAAAA,OAAO,CAACkB,KAAR,CACK,eAAcI,kBAAmB,4CADtC,EADoB,CAIpB;;AACAxB,EAAAA,OAAO,CAACI,IAAR,CAAa,CAAb;AACH","sourcesContent":["#!/usr/bin/env node\n// @flow\n/* eslint-disable no-console */\nimport {generateTypeFiles, processPragmas} from '../generateTypeFiles';\nimport {processFiles} from '../parser/parse';\nimport {resolveDocuments} from '../parser/resolve';\nimport {getSchemas, loadConfigFile} from './config';\n\nimport {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line flowtype-errors/uncovered\n\nimport {execSync} from 'child_process';\nimport {readFileSync} from 'fs';\nimport {type DocumentNode} from 'graphql';\nimport {print} from 'graphql/language/printer';\nimport {validate} from 'graphql/validation';\nimport path from 'path';\n\n/**\n * This CLI tool executes the following steps:\n * 1) process options\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'\",\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 [_, __, configFile, ...cliFiles] = process.argv;\n\nif (\n configFile === '-h' ||\n configFile === '--help' ||\n configFile === 'help' ||\n !configFile\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 config = loadConfigFile(configFile);\n\nconst [schemaForValidation, schemaForTypeGeneration] = getSchemas(\n config.schemaFilePath,\n);\n\nconst inputFiles = cliFiles.length\n ? cliFiles\n : findGraphqlTagReferences(process.cwd());\n\n/** Step (2) */\n\nconst files = processFiles(inputFiles, (f) => readFileSync(f, 'utf8'));\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\nlet validationFailures: number = 0;\n\nObject.keys(resolved).forEach((k) => {\n const {document, raw} = resolved[k];\n if (config.excludes.some((rx) => rx.test(raw.loc.path))) {\n return; // skip\n }\n const hasNonFragments = document.definitions.some(\n ({kind}) => kind !== 'FragmentDefinition',\n );\n const rawSource: string = raw.literals[0];\n const processedOptions = processPragmas(config.options, rawSource);\n if (!processedOptions) {\n return;\n }\n\n // eslint-disable-next-line flowtype-errors/uncovered\n const withTypeNames: DocumentNode = addTypenameToDocument(document);\n const printed = print(withTypeNames);\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 processedOptions,\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"],"file":"run.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/run.js"],"names":["findGraphqlTagReferences","root","response","encoding","cwd","trim","split","map","relative","path","join","_","__","configFile","cliFiles","process","argv","console","log","exit","config","schemaForValidation","schemaForTypeGeneration","schemaFilePath","inputFiles","length","files","f","Error","filesHadErrors","Object","keys","forEach","key","file","errors","error","message","resolved","loc","validationFailures","printedOperations","k","document","raw","excludes","some","rx","test","hasNonFragments","definitions","kind","rawSource","literals","withTypeNames","printed","includes","push","processedOptions","options","err","dumpOperations","parent","recursive","JSON","stringify","sort"],"mappings":"AAAA;;AAEA;;;AACA;;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,uFADa,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,EAAQ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,KAAR,CAAcmB,OAAd,EAFU,CAGV;;AACAtC,IAAAA,OAAO,CAACmB,KAAR,CAAcwB,GAAd;AACApB,IAAAA,kBAAkB;AACrB;AACJ,CAtDD;;AAwDA,IAAIA,kBAAJ,EAAwB;AACpBvB,EAAAA,OAAO,CAACmB,KAAR,CACK,eAAcI,kBAAmB,4CADtC,EADoB,CAIpB;;AACAzB,EAAAA,OAAO,CAACI,IAAR,CAAa,CAAb;AACH;;AAED,IAAIC,MAAM,CAACyC,cAAX,EAA2B;AACvB,QAAMA,cAAc,GAAGzC,MAAM,CAACyC,cAA9B;AACA,QAAMC,MAAM,GAAG,mBAAQD,cAAR,CAAf;AACA,qBAAUC,MAAV,EAAkB;AAACC,IAAAA,SAAS,EAAE;AAAZ,GAAlB;AACA,yBACIF,cADJ,EAEIG,IAAI,CAACC,SAAL,CAAexB,iBAAiB,CAACyB,IAAlB,EAAf,EAAyC,IAAzC,EAA+C,CAA/C,CAFJ;AAIH","sourcesContent":["#!/usr/bin/env node\n// @flow\n/* eslint-disable no-console */\nimport {generateTypeFiles, processPragmas} from '../generateTypeFiles';\nimport {processFiles} from '../parser/parse';\nimport {resolveDocuments} from '../parser/resolve';\nimport {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) process options\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'\",\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 [_, __, configFile, ...cliFiles] = process.argv;\n\nif (\n configFile === '-h' ||\n configFile === '--help' ||\n configFile === 'help' ||\n !configFile\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 config = loadConfigFile(configFile);\n\nconst [schemaForValidation, schemaForTypeGeneration] = getSchemas(\n config.schemaFilePath,\n);\n\nconst inputFiles = cliFiles.length\n ? cliFiles\n : findGraphqlTagReferences(process.cwd());\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 readFileSync(f + '.js', 'utf8');\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\nlet validationFailures: number = 0;\nconst printedOperations: Array<string> = [];\n\nObject.keys(resolved).forEach((k) => {\n const {document, raw} = resolved[k];\n if (config.excludes.some((rx) => rx.test(raw.loc.path))) {\n return; // skip\n }\n const hasNonFragments = document.definitions.some(\n ({kind}) => kind !== 'FragmentDefinition',\n );\n const rawSource: string = raw.literals[0];\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 processedOptions = processPragmas(config.options, rawSource);\n if (!processedOptions) {\n return;\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 processedOptions,\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.dumpOperations) {\n const dumpOperations = config.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"}
|
|
@@ -56,7 +56,7 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
56
56
|
}) => {
|
|
57
57
|
// We write all generated files to a `__generated__` subdir to keep
|
|
58
58
|
// things tidy.
|
|
59
|
-
const targetFileName = `${name}.js`;
|
|
59
|
+
const targetFileName = options.typeFileName ? options.typeFileName.replace('[operationName]', name) : `${name}.js`;
|
|
60
60
|
|
|
61
61
|
const targetPath = _path.default.join(generatedDir, targetFileName);
|
|
62
62
|
|
|
@@ -82,10 +82,23 @@ const generateTypeFileContents = (fileName, schema, document, options, generated
|
|
|
82
82
|
|
|
83
83
|
exports.generateTypeFileContents = generateTypeFileContents;
|
|
84
84
|
|
|
85
|
-
const
|
|
85
|
+
const getGeneratedDir = (fileName, options) => {
|
|
86
86
|
var _options$generatedDir;
|
|
87
87
|
|
|
88
|
-
const
|
|
88
|
+
const generatedDirectory = (_options$generatedDir = options.generatedDirectory) !== null && _options$generatedDir !== void 0 ? _options$generatedDir : '__generated__';
|
|
89
|
+
|
|
90
|
+
if (_path.default.isAbsolute(generatedDirectory)) {
|
|
91
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
92
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
93
|
+
// to `__` so this doesn't escape the output directory.
|
|
94
|
+
return _path.default.join(generatedDirectory, _path.default.relative(process.cwd(), _path.default.dirname(fileName)).replace(/\.\.\//g, '__/'));
|
|
95
|
+
} else {
|
|
96
|
+
return _path.default.join(_path.default.dirname(fileName), generatedDirectory);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const generateTypeFiles = (fileName, schema, document, options) => {
|
|
101
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
89
102
|
|
|
90
103
|
const indexFile = _path.default.join(generatedDir, 'index.js');
|
|
91
104
|
|
|
@@ -132,7 +145,8 @@ const processPragmas = (options, rawSource) => {
|
|
|
132
145
|
scalars: options.scalars,
|
|
133
146
|
splitTypes: options.splitTypes,
|
|
134
147
|
generatedDirectory: options.generatedDirectory,
|
|
135
|
-
exportAllObjectTypes: options.exportAllObjectTypes
|
|
148
|
+
exportAllObjectTypes: options.exportAllObjectTypes,
|
|
149
|
+
typeFileName: options.typeFileName
|
|
136
150
|
};
|
|
137
151
|
} else {
|
|
138
152
|
return null;
|
|
@@ -20,6 +20,7 @@ export type ExternalOptions = {
|
|
|
20
20
|
splitTypes?: boolean,
|
|
21
21
|
generatedDirectory?: string,
|
|
22
22
|
exportAllObjectTypes?: boolean,
|
|
23
|
+
typeFileName?: string,
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
export const indexPrelude = (regenerateCommand?: string): string => `// @flow
|
|
@@ -64,7 +65,9 @@ export const generateTypeFileContents = (
|
|
|
64
65
|
generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {
|
|
65
66
|
// We write all generated files to a `__generated__` subdir to keep
|
|
66
67
|
// things tidy.
|
|
67
|
-
const targetFileName =
|
|
68
|
+
const targetFileName = options.typeFileName
|
|
69
|
+
? options.typeFileName.replace('[operationName]', name)
|
|
70
|
+
: `${name}.js`;
|
|
68
71
|
const targetPath = path.join(generatedDir, targetFileName);
|
|
69
72
|
|
|
70
73
|
let fileContents =
|
|
@@ -97,16 +100,30 @@ export const generateTypeFileContents = (
|
|
|
97
100
|
return {files, indexContents};
|
|
98
101
|
};
|
|
99
102
|
|
|
103
|
+
const getGeneratedDir = (fileName: string, options: Options) => {
|
|
104
|
+
const generatedDirectory = options.generatedDirectory ?? '__generated__';
|
|
105
|
+
if (path.isAbsolute(generatedDirectory)) {
|
|
106
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
107
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
108
|
+
// to `__` so this doesn't escape the output directory.
|
|
109
|
+
return path.join(
|
|
110
|
+
generatedDirectory,
|
|
111
|
+
path
|
|
112
|
+
.relative(process.cwd(), path.dirname(fileName))
|
|
113
|
+
.replace(/\.\.\//g, '__/'),
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
return path.join(path.dirname(fileName), generatedDirectory);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
100
120
|
export const generateTypeFiles = (
|
|
101
121
|
fileName: string,
|
|
102
122
|
schema: Schema,
|
|
103
123
|
document: DocumentNode,
|
|
104
124
|
options: Options,
|
|
105
125
|
) => {
|
|
106
|
-
const generatedDir =
|
|
107
|
-
path.dirname(fileName),
|
|
108
|
-
options.generatedDirectory ?? '__generated__',
|
|
109
|
-
);
|
|
126
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
110
127
|
const indexFile = path.join(generatedDir, 'index.js');
|
|
111
128
|
|
|
112
129
|
if (!fs.existsSync(generatedDir)) {
|
|
@@ -160,6 +177,7 @@ export const processPragmas = (
|
|
|
160
177
|
splitTypes: options.splitTypes,
|
|
161
178
|
generatedDirectory: options.generatedDirectory,
|
|
162
179
|
exportAllObjectTypes: options.exportAllObjectTypes,
|
|
180
|
+
typeFileName: options.typeFileName,
|
|
163
181
|
};
|
|
164
182
|
} else {
|
|
165
183
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generateTypeFiles.js"],"names":["indexPrelude","regenerateCommand","generateTypeFileContents","fileName","schema","document","options","generatedDir","indexContents","files","addToIndex","filePath","typeName","newLine","path","basename","indexOf","lines","split","map","line","includes","join","generated","forEach","name","code","isFragment","extraTypes","targetFileName","targetPath","fileContents","splitTypes","Object","keys","replace","generateTypeFiles","dirname","generatedDirectory","indexFile","fs","existsSync","mkdirSync","recursive","writeFileSync","readFileSync","key","processPragmas","rawSource","ignorePragma","autogen","loosePragma","autogenStrict","pragma","noPragmas","strictNullability","readOnlyArray","scalars","exportAllObjectTypes"],"mappings":";;;;;;;AAIA;;AACA;;AACA;;;;AALA;AAuBO,MAAMA,YAAY,GAAIC,iBAAD,IAAyC;AACrE;AACA;AACA;AACA;AACA,IAAIA,iBAAiB,GAAG,8BAA8BA,iBAAjC,GAAqD,EAAG;AAC7E;AACA;AACA,CARO;;;;AAUA,MAAMC,wBAAwB,GAAG,CACpCC,QADoC,EAEpCC,MAFoC,EAGpCC,QAHoC,EAIpCC,OAJoC,EAKpCC,YALoC,EAMpCC,aANoC,KAOsB;AAC1D,QAAMC,KAAK,GAAG,EAAd,CAD0D,CAG1D;;AACA,QAAMC,UAAU,GAAG,CAACC,QAAD,EAAWC,QAAX,KAAwB;AACvC,UAAMC,OAAO,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HC,MAAAA,OAAO,EAAEnD,OAAO,CAACmD,OANd;AAOHzB,MAAAA,UAAU,EAAE1B,OAAO,CAAC0B,UAPjB;AAQHM,MAAAA,kBAAkB,EAAEhC,OAAO,CAACgC,kBARzB;AASHoB,MAAAA,oBAAoB,EAAEpD,OAAO,CAACoD;AAT3B,KAAP;AAWH,GAZD,MAYO;AACH,WAAO,IAAP;AACH;AACJ,CA/BM","sourcesContent":["// @flow\n// Import this in your jest setup, to mock out graphql-tag!\nimport type {DocumentNode} from 'graphql';\nimport type {Options, Schema, Scalars} from './types';\nimport fs from 'fs';\nimport path from 'path';\nimport {documentToFlowTypes} from '.';\n\nexport type ExternalOptions = {\n pragma?: string,\n loosePragma?: string,\n ignorePragma?: string,\n scalars?: Scalars,\n strictNullability?: boolean,\n /**\n * The command that users should run to regenerate the types files.\n */\n regenerateCommand?: string,\n readOnlyArray?: boolean,\n splitTypes?: boolean,\n generatedDirectory?: string,\n exportAllObjectTypes?: boolean,\n};\n\nexport const indexPrelude = (regenerateCommand?: string): string => `// @flow\n//\n// AUTOGENERATED\n// NOTE: New response types are added to this file automatically.\n// Outdated response types can be removed manually as they are deprecated.\n//${regenerateCommand ? ' To regenerate, run ' + regenerateCommand : ''}\n//\n\n`;\n\nexport const generateTypeFileContents = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n generatedDir: string,\n indexContents: string,\n): {indexContents: string, files: {[key: string]: string}} => {\n const files = {};\n\n /// Write export for __generated__/index.js if it doesn't exist\n const addToIndex = (filePath, typeName) => {\n const newLine = `export type {${typeName}} from './${path.basename(\n filePath,\n )}';`;\n if (indexContents.indexOf('./' + path.basename(filePath)) === -1) {\n indexContents += newLine + '\\n';\n } else {\n const lines = indexContents.split('\\n').map((line) => {\n if (line.includes('./' + path.basename(filePath))) {\n return newLine;\n }\n return line;\n });\n indexContents = lines.join('\\n');\n }\n };\n\n const generated = documentToFlowTypes(document, schema, options);\n generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {\n // We write all generated files to a `__generated__` subdir to keep\n // things tidy.\n const targetFileName = `${name}.js`;\n const targetPath = path.join(generatedDir, targetFileName);\n\n let fileContents =\n '// @' +\n `flow\\n// AUTOGENERATED -- DO NOT EDIT\\n` +\n `// Generated for operation '${name}' in file '../${path.basename(\n fileName,\n )}'\\n` +\n (options.regenerateCommand\n ? `// To regenerate, run '${options.regenerateCommand}'.\\n`\n : '') +\n code;\n if (options.splitTypes && !isFragment) {\n fileContents +=\n `\\nexport type ${name} = ${typeName}['response'];\\n` +\n `export type ${name}Variables = ${typeName}['variables'];\\n`;\n }\n Object.keys(extraTypes).forEach((name) => {\n fileContents += `\\n\\nexport type ${name} = ${extraTypes[name]};`;\n });\n\n addToIndex(targetPath, typeName);\n files[targetPath] =\n fileContents\n // Remove whitespace from the ends of lines; babel's generate sometimes\n // leaves them hanging around.\n .replace(/\\s+$/gm, '') + '\\n';\n });\n\n return {files, indexContents};\n};\n\nexport const generateTypeFiles = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n) => {\n const generatedDir = path.join(\n path.dirname(fileName),\n options.generatedDirectory ?? '__generated__',\n );\n const indexFile = path.join(generatedDir, 'index.js');\n\n if (!fs.existsSync(generatedDir)) {\n fs.mkdirSync(generatedDir, {recursive: true});\n }\n if (!fs.existsSync(indexFile)) {\n fs.writeFileSync(indexFile, indexPrelude(options.regenerateCommand));\n }\n\n const {indexContents, files} = generateTypeFileContents(\n fileName,\n schema,\n document,\n options,\n generatedDir,\n fs.readFileSync(indexFile, 'utf8'),\n );\n\n fs.writeFileSync(indexFile, indexContents);\n Object.keys(files).forEach((key) => {\n fs.writeFileSync(key, files[key]);\n });\n\n fs.writeFileSync(indexFile, indexContents);\n};\n\nexport const processPragmas = (\n options: ExternalOptions,\n rawSource: string,\n): null | Options => {\n if (options.ignorePragma && rawSource.includes(options.ignorePragma)) {\n return null;\n }\n\n const autogen = options.loosePragma\n ? rawSource.includes(options.loosePragma)\n : false;\n const autogenStrict = options.pragma\n ? rawSource.includes(options.pragma)\n : false;\n const noPragmas = !options.loosePragma && !options.pragma;\n\n if (autogen || autogenStrict || noPragmas) {\n return {\n regenerateCommand: options.regenerateCommand,\n strictNullability: noPragmas\n ? options.strictNullability\n : autogenStrict || !autogen,\n readOnlyArray: options.readOnlyArray,\n scalars: options.scalars,\n splitTypes: options.splitTypes,\n generatedDirectory: options.generatedDirectory,\n exportAllObjectTypes: options.exportAllObjectTypes,\n };\n } else {\n return null;\n }\n};\n"],"file":"generateTypeFiles.js"}
|
|
1
|
+
{"version":3,"sources":["../src/generateTypeFiles.js"],"names":["indexPrelude","regenerateCommand","generateTypeFileContents","fileName","schema","document","options","generatedDir","indexContents","files","addToIndex","filePath","typeName","newLine","path","basename","indexOf","lines","split","map","line","includes","join","generated","forEach","name","code","isFragment","extraTypes","targetFileName","typeFileName","replace","targetPath","fileContents","splitTypes","Object","keys","getGeneratedDir","generatedDirectory","isAbsolute","relative","process","cwd","dirname","generateTypeFiles","indexFile","fs","existsSync","mkdirSync","recursive","writeFileSync","readFileSync","key","processPragmas","rawSource","ignorePragma","autogen","loosePragma","autogenStrict","pragma","noPragmas","strictNullability","readOnlyArray","scalars","exportAllObjectTypes"],"mappings":";;;;;;;AAIA;;AACA;;AACA;;;;AALA;AAwBO,MAAMA,YAAY,GAAIC,iBAAD,IAAyC;AACrE;AACA;AACA;AACA;AACA,IAAIA,iBAAiB,GAAG,8BAA8BA,iBAAjC,GAAqD,EAAG;AAC7E;AACA;AACA,CARO;;;;AAUA,MAAMC,wBAAwB,GAAG,CACpCC,QADoC,EAEpCC,MAFoC,EAGpCC,QAHoC,EAIpCC,OAJoC,EAKpCC,YALoC,EAMpCC,aANoC,KAOsB;AAC1D,QAAMC,KAAK,GAAG,EAAd,CAD0D,CAG1D;;AACA,QAAMC,UAAU,GAAG,CAACC,QAAD,EAAWC,QAAX,KAAwB;AACvC,UAAMC,OAAO,GAAI,gBAAeD,QAAS,aAAYE,cAAKC,QAAL,CACjDJ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AAO,CAACmD,WAA3B,CADU,GAEV,KAFN;AAGA,QAAMC,aAAa,GAAGpD,OAAO,CAACqD,MAAR,GAChBL,SAAS,CAACjC,QAAV,CAAmBf,OAAO,CAACqD,MAA3B,CADgB,GAEhB,KAFN;AAGA,QAAMC,SAAS,GAAG,CAACtD,OAAO,CAACmD,WAAT,IAAwB,CAACnD,OAAO,CAACqD,MAAnD;;AAEA,MAAIH,OAAO,IAAIE,aAAX,IAA4BE,SAAhC,EAA2C;AACvC,WAAO;AACH3D,MAAAA,iBAAiB,EAAEK,OAAO,CAACL,iBADxB;AAEH4D,MAAAA,iBAAiB,EAAED,SAAS,GACtBtD,OAAO,CAACuD,iBADc,GAEtBH,aAAa,IAAI,CAACF,OAJrB;AAKHM,MAAAA,aAAa,EAAExD,OAAO,CAACwD,aALpB;AAMHC,MAAAA,OAAO,EAAEzD,OAAO,CAACyD,OANd;AAOH7B,MAAAA,UAAU,EAAE5B,OAAO,CAAC4B,UAPjB;AAQHI,MAAAA,kBAAkB,EAAEhC,OAAO,CAACgC,kBARzB;AASH0B,MAAAA,oBAAoB,EAAE1D,OAAO,CAAC0D,oBAT3B;AAUHlC,MAAAA,YAAY,EAAExB,OAAO,CAACwB;AAVnB,KAAP;AAYH,GAbD,MAaO;AACH,WAAO,IAAP;AACH;AACJ,CAhCM","sourcesContent":["// @flow\n// Import this in your jest setup, to mock out graphql-tag!\nimport type {DocumentNode} from 'graphql';\nimport type {Options, Schema, Scalars} from './types';\nimport fs from 'fs';\nimport path from 'path';\nimport {documentToFlowTypes} from '.';\n\nexport type ExternalOptions = {\n pragma?: string,\n loosePragma?: string,\n ignorePragma?: string,\n scalars?: Scalars,\n strictNullability?: boolean,\n /**\n * The command that users should run to regenerate the types files.\n */\n regenerateCommand?: string,\n readOnlyArray?: boolean,\n splitTypes?: boolean,\n generatedDirectory?: string,\n exportAllObjectTypes?: boolean,\n typeFileName?: string,\n};\n\nexport const indexPrelude = (regenerateCommand?: string): string => `// @flow\n//\n// AUTOGENERATED\n// NOTE: New response types are added to this file automatically.\n// Outdated response types can be removed manually as they are deprecated.\n//${regenerateCommand ? ' To regenerate, run ' + regenerateCommand : ''}\n//\n\n`;\n\nexport const generateTypeFileContents = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n generatedDir: string,\n indexContents: string,\n): {indexContents: string, files: {[key: string]: string}} => {\n const files = {};\n\n /// Write export for __generated__/index.js if it doesn't exist\n const addToIndex = (filePath, typeName) => {\n const newLine = `export type {${typeName}} from './${path.basename(\n filePath,\n )}';`;\n if (indexContents.indexOf('./' + path.basename(filePath)) === -1) {\n indexContents += newLine + '\\n';\n } else {\n const lines = indexContents.split('\\n').map((line) => {\n if (line.includes('./' + path.basename(filePath))) {\n return newLine;\n }\n return line;\n });\n indexContents = lines.join('\\n');\n }\n };\n\n const generated = documentToFlowTypes(document, schema, options);\n generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {\n // We write all generated files to a `__generated__` subdir to keep\n // things tidy.\n const targetFileName = options.typeFileName\n ? options.typeFileName.replace('[operationName]', name)\n : `${name}.js`;\n const targetPath = path.join(generatedDir, targetFileName);\n\n let fileContents =\n '// @' +\n `flow\\n// AUTOGENERATED -- DO NOT EDIT\\n` +\n `// Generated for operation '${name}' in file '../${path.basename(\n fileName,\n )}'\\n` +\n (options.regenerateCommand\n ? `// To regenerate, run '${options.regenerateCommand}'.\\n`\n : '') +\n code;\n if (options.splitTypes && !isFragment) {\n fileContents +=\n `\\nexport type ${name} = ${typeName}['response'];\\n` +\n `export type ${name}Variables = ${typeName}['variables'];\\n`;\n }\n Object.keys(extraTypes).forEach((name) => {\n fileContents += `\\n\\nexport type ${name} = ${extraTypes[name]};`;\n });\n\n addToIndex(targetPath, typeName);\n files[targetPath] =\n fileContents\n // Remove whitespace from the ends of lines; babel's generate sometimes\n // leaves them hanging around.\n .replace(/\\s+$/gm, '') + '\\n';\n });\n\n return {files, indexContents};\n};\n\nconst getGeneratedDir = (fileName: string, options: Options) => {\n const generatedDirectory = options.generatedDirectory ?? '__generated__';\n if (path.isAbsolute(generatedDirectory)) {\n // fileName is absolute here, so we make it relative to cwd\n // for more reasonable filenames. We convert leading ..'s\n // to `__` so this doesn't escape the output directory.\n return path.join(\n generatedDirectory,\n path\n .relative(process.cwd(), path.dirname(fileName))\n .replace(/\\.\\.\\//g, '__/'),\n );\n } else {\n return path.join(path.dirname(fileName), generatedDirectory);\n }\n};\n\nexport const generateTypeFiles = (\n fileName: string,\n schema: Schema,\n document: DocumentNode,\n options: Options,\n) => {\n const generatedDir = getGeneratedDir(fileName, options);\n const indexFile = path.join(generatedDir, 'index.js');\n\n if (!fs.existsSync(generatedDir)) {\n fs.mkdirSync(generatedDir, {recursive: true});\n }\n if (!fs.existsSync(indexFile)) {\n fs.writeFileSync(indexFile, indexPrelude(options.regenerateCommand));\n }\n\n const {indexContents, files} = generateTypeFileContents(\n fileName,\n schema,\n document,\n options,\n generatedDir,\n fs.readFileSync(indexFile, 'utf8'),\n );\n\n fs.writeFileSync(indexFile, indexContents);\n Object.keys(files).forEach((key) => {\n fs.writeFileSync(key, files[key]);\n });\n\n fs.writeFileSync(indexFile, indexContents);\n};\n\nexport const processPragmas = (\n options: ExternalOptions,\n rawSource: string,\n): null | Options => {\n if (options.ignorePragma && rawSource.includes(options.ignorePragma)) {\n return null;\n }\n\n const autogen = options.loosePragma\n ? rawSource.includes(options.loosePragma)\n : false;\n const autogenStrict = options.pragma\n ? rawSource.includes(options.pragma)\n : false;\n const noPragmas = !options.loosePragma && !options.pragma;\n\n if (autogen || autogenStrict || noPragmas) {\n return {\n regenerateCommand: options.regenerateCommand,\n strictNullability: noPragmas\n ? options.strictNullability\n : autogenStrict || !autogen,\n readOnlyArray: options.readOnlyArray,\n scalars: options.scalars,\n splitTypes: options.splitTypes,\n generatedDirectory: options.generatedDirectory,\n exportAllObjectTypes: options.exportAllObjectTypes,\n typeFileName: options.typeFileName,\n };\n } else {\n return null;\n }\n};\n"],"file":"generateTypeFiles.js"}
|
package/dist/types.js.flow
CHANGED
package/package.json
CHANGED
|
@@ -58,4 +58,46 @@ describe('generateTypeFileContents', () => {
|
|
|
58
58
|
"
|
|
59
59
|
`);
|
|
60
60
|
});
|
|
61
|
+
|
|
62
|
+
it('should respect the typeFileName option', () => {
|
|
63
|
+
const {files} = generateTypeFileContents(
|
|
64
|
+
'hello.js',
|
|
65
|
+
exampleSchema,
|
|
66
|
+
gql`
|
|
67
|
+
query Hello {
|
|
68
|
+
human(id: "Han") {
|
|
69
|
+
id
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
`,
|
|
73
|
+
{
|
|
74
|
+
splitTypes: true,
|
|
75
|
+
typeFileName: 'prefix-[operationName]-suffix.js',
|
|
76
|
+
},
|
|
77
|
+
'__generated__',
|
|
78
|
+
indexPrelude('yarn queries'),
|
|
79
|
+
);
|
|
80
|
+
expect(
|
|
81
|
+
Object.keys(files)
|
|
82
|
+
.map((k) => `// ${k}\n${files[k]}`)
|
|
83
|
+
.join('\n\n'),
|
|
84
|
+
).toMatchInlineSnapshot(`
|
|
85
|
+
"// __generated__/prefix-Hello-suffix.js
|
|
86
|
+
// @flow
|
|
87
|
+
// AUTOGENERATED -- DO NOT EDIT
|
|
88
|
+
// Generated for operation 'Hello' in file '../hello.js'
|
|
89
|
+
export type HelloType = {|
|
|
90
|
+
variables: {||},
|
|
91
|
+
response: {|
|
|
92
|
+
/** A human character*/
|
|
93
|
+
human: ?{|
|
|
94
|
+
id: string
|
|
95
|
+
|}
|
|
96
|
+
|}
|
|
97
|
+
|};
|
|
98
|
+
export type Hello = HelloType['response'];
|
|
99
|
+
export type HelloVariables = HelloType['variables'];
|
|
100
|
+
"
|
|
101
|
+
`);
|
|
102
|
+
});
|
|
61
103
|
});
|
package/src/cli/config.js
CHANGED
|
@@ -18,6 +18,7 @@ import path from 'path';
|
|
|
18
18
|
export type CliConfig = {
|
|
19
19
|
excludes: Array<RegExp>,
|
|
20
20
|
schemaFilePath: string,
|
|
21
|
+
dumpOperations?: string,
|
|
21
22
|
options: ExternalOptions,
|
|
22
23
|
};
|
|
23
24
|
|
|
@@ -29,12 +30,18 @@ type JSONConfig = {
|
|
|
29
30
|
excludes?: Array<string>,
|
|
30
31
|
schemaFilePath: string,
|
|
31
32
|
options?: ExternalOptions,
|
|
33
|
+
dumpOperations?: string,
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
export const loadConfigFile = (configFile: string): CliConfig => {
|
|
35
37
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
36
38
|
const data: JSONConfig = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
37
|
-
const toplevelKeys = [
|
|
39
|
+
const toplevelKeys = [
|
|
40
|
+
'excludes',
|
|
41
|
+
'schemaFilePath',
|
|
42
|
+
'options',
|
|
43
|
+
'dumpOperations',
|
|
44
|
+
];
|
|
38
45
|
Object.keys(data).forEach((k) => {
|
|
39
46
|
if (!toplevelKeys.includes(k)) {
|
|
40
47
|
throw new Error(
|
|
@@ -56,6 +63,7 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
56
63
|
'splitTypes',
|
|
57
64
|
'generatedDirectory',
|
|
58
65
|
'exportAllObjectTypes',
|
|
66
|
+
'typeFileName',
|
|
59
67
|
];
|
|
60
68
|
Object.keys(data.options).forEach((k) => {
|
|
61
69
|
if (!externalOptionsKeys.includes(k)) {
|
|
@@ -70,10 +78,10 @@ export const loadConfigFile = (configFile: string): CliConfig => {
|
|
|
70
78
|
return {
|
|
71
79
|
options: data.options ?? {},
|
|
72
80
|
excludes: data.excludes?.map((string) => new RegExp(string)) ?? [],
|
|
73
|
-
schemaFilePath: path.
|
|
74
|
-
|
|
75
|
-
data.schemaFilePath,
|
|
76
|
-
|
|
81
|
+
schemaFilePath: path.isAbsolute(data.schemaFilePath)
|
|
82
|
+
? data.schemaFilePath
|
|
83
|
+
: path.join(path.dirname(configFile), data.schemaFilePath),
|
|
84
|
+
dumpOperations: data.dumpOperations,
|
|
77
85
|
};
|
|
78
86
|
};
|
|
79
87
|
|
package/src/cli/run.js
CHANGED
|
@@ -9,11 +9,12 @@ import {getSchemas, loadConfigFile} from './config';
|
|
|
9
9
|
import {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line flowtype-errors/uncovered
|
|
10
10
|
|
|
11
11
|
import {execSync} from 'child_process';
|
|
12
|
-
import {readFileSync} from 'fs';
|
|
12
|
+
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
|
|
13
13
|
import {type DocumentNode} from 'graphql';
|
|
14
14
|
import {print} from 'graphql/language/printer';
|
|
15
15
|
import {validate} from 'graphql/validation';
|
|
16
16
|
import path from 'path';
|
|
17
|
+
import {dirname} from 'path';
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* This CLI tool executes the following steps:
|
|
@@ -68,7 +69,15 @@ const inputFiles = cliFiles.length
|
|
|
68
69
|
|
|
69
70
|
/** Step (2) */
|
|
70
71
|
|
|
71
|
-
const files = processFiles(inputFiles, (f) =>
|
|
72
|
+
const files = processFiles(inputFiles, (f) => {
|
|
73
|
+
if (existsSync(f)) {
|
|
74
|
+
return readFileSync(f, 'utf8');
|
|
75
|
+
}
|
|
76
|
+
if (existsSync(f + '.js')) {
|
|
77
|
+
return readFileSync(f + '.js', 'utf8');
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Unable to find ${f}`);
|
|
80
|
+
});
|
|
72
81
|
|
|
73
82
|
let filesHadErrors = false;
|
|
74
83
|
Object.keys(files).forEach((key) => {
|
|
@@ -103,6 +112,7 @@ console.log(Object.keys(resolved).length, 'resolved queries');
|
|
|
103
112
|
/** Step (4) */
|
|
104
113
|
|
|
105
114
|
let validationFailures: number = 0;
|
|
115
|
+
const printedOperations: Array<string> = [];
|
|
106
116
|
|
|
107
117
|
Object.keys(resolved).forEach((k) => {
|
|
108
118
|
const {document, raw} = resolved[k];
|
|
@@ -113,14 +123,18 @@ Object.keys(resolved).forEach((k) => {
|
|
|
113
123
|
({kind}) => kind !== 'FragmentDefinition',
|
|
114
124
|
);
|
|
115
125
|
const rawSource: string = raw.literals[0];
|
|
116
|
-
const processedOptions = processPragmas(config.options, rawSource);
|
|
117
|
-
if (!processedOptions) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
126
|
|
|
121
127
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
122
128
|
const withTypeNames: DocumentNode = addTypenameToDocument(document);
|
|
123
129
|
const printed = print(withTypeNames);
|
|
130
|
+
if (hasNonFragments && !printedOperations.includes(printed)) {
|
|
131
|
+
printedOperations.push(printed);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const processedOptions = processPragmas(config.options, rawSource);
|
|
135
|
+
if (!processedOptions) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
124
138
|
|
|
125
139
|
if (hasNonFragments) {
|
|
126
140
|
/* eslint-disable flowtype-errors/uncovered */
|
|
@@ -163,3 +177,13 @@ if (validationFailures) {
|
|
|
163
177
|
// eslint-disable-next-line flowtype-errors/uncovered
|
|
164
178
|
process.exit(1);
|
|
165
179
|
}
|
|
180
|
+
|
|
181
|
+
if (config.dumpOperations) {
|
|
182
|
+
const dumpOperations = config.dumpOperations;
|
|
183
|
+
const parent = dirname(dumpOperations);
|
|
184
|
+
mkdirSync(parent, {recursive: true});
|
|
185
|
+
writeFileSync(
|
|
186
|
+
dumpOperations,
|
|
187
|
+
JSON.stringify(printedOperations.sort(), null, 2),
|
|
188
|
+
);
|
|
189
|
+
}
|
package/src/generateTypeFiles.js
CHANGED
|
@@ -20,6 +20,7 @@ export type ExternalOptions = {
|
|
|
20
20
|
splitTypes?: boolean,
|
|
21
21
|
generatedDirectory?: string,
|
|
22
22
|
exportAllObjectTypes?: boolean,
|
|
23
|
+
typeFileName?: string,
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
export const indexPrelude = (regenerateCommand?: string): string => `// @flow
|
|
@@ -64,7 +65,9 @@ export const generateTypeFileContents = (
|
|
|
64
65
|
generated.forEach(({name, typeName, code, isFragment, extraTypes}) => {
|
|
65
66
|
// We write all generated files to a `__generated__` subdir to keep
|
|
66
67
|
// things tidy.
|
|
67
|
-
const targetFileName =
|
|
68
|
+
const targetFileName = options.typeFileName
|
|
69
|
+
? options.typeFileName.replace('[operationName]', name)
|
|
70
|
+
: `${name}.js`;
|
|
68
71
|
const targetPath = path.join(generatedDir, targetFileName);
|
|
69
72
|
|
|
70
73
|
let fileContents =
|
|
@@ -97,16 +100,30 @@ export const generateTypeFileContents = (
|
|
|
97
100
|
return {files, indexContents};
|
|
98
101
|
};
|
|
99
102
|
|
|
103
|
+
const getGeneratedDir = (fileName: string, options: Options) => {
|
|
104
|
+
const generatedDirectory = options.generatedDirectory ?? '__generated__';
|
|
105
|
+
if (path.isAbsolute(generatedDirectory)) {
|
|
106
|
+
// fileName is absolute here, so we make it relative to cwd
|
|
107
|
+
// for more reasonable filenames. We convert leading ..'s
|
|
108
|
+
// to `__` so this doesn't escape the output directory.
|
|
109
|
+
return path.join(
|
|
110
|
+
generatedDirectory,
|
|
111
|
+
path
|
|
112
|
+
.relative(process.cwd(), path.dirname(fileName))
|
|
113
|
+
.replace(/\.\.\//g, '__/'),
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
return path.join(path.dirname(fileName), generatedDirectory);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
100
120
|
export const generateTypeFiles = (
|
|
101
121
|
fileName: string,
|
|
102
122
|
schema: Schema,
|
|
103
123
|
document: DocumentNode,
|
|
104
124
|
options: Options,
|
|
105
125
|
) => {
|
|
106
|
-
const generatedDir =
|
|
107
|
-
path.dirname(fileName),
|
|
108
|
-
options.generatedDirectory ?? '__generated__',
|
|
109
|
-
);
|
|
126
|
+
const generatedDir = getGeneratedDir(fileName, options);
|
|
110
127
|
const indexFile = path.join(generatedDir, 'index.js');
|
|
111
128
|
|
|
112
129
|
if (!fs.existsSync(generatedDir)) {
|
|
@@ -160,6 +177,7 @@ export const processPragmas = (
|
|
|
160
177
|
splitTypes: options.splitTypes,
|
|
161
178
|
generatedDirectory: options.generatedDirectory,
|
|
162
179
|
exportAllObjectTypes: options.exportAllObjectTypes,
|
|
180
|
+
typeFileName: options.typeFileName,
|
|
163
181
|
};
|
|
164
182
|
} else {
|
|
165
183
|
return null;
|