@graphql-codegen/cli 2.11.9-alpha-20220831122019-e4f0e71b5 → 2.11.9-alpha-20220926151609-5af00bb63
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/cjs/codegen.js +17 -12
- package/cjs/config.js +6 -1
- package/cjs/hooks.js +2 -0
- package/cjs/init/helpers.js +34 -3
- package/cjs/init/index.js +4 -3
- package/cjs/init/plugins.js +6 -42
- package/cjs/init/questions.js +52 -12
- package/cjs/init/targets.js +5 -1
- package/cjs/init/types.js +2 -1
- package/cjs/presets.js +4 -3
- package/esm/codegen.js +17 -12
- package/esm/config.js +6 -1
- package/esm/hooks.js +2 -0
- package/esm/init/helpers.js +34 -3
- package/esm/init/index.js +4 -3
- package/esm/init/plugins.js +6 -42
- package/esm/init/questions.js +50 -11
- package/esm/init/targets.js +5 -1
- package/esm/init/types.js +2 -1
- package/esm/presets.js +4 -3
- package/package.json +7 -2
- package/typings/config.d.cts +1 -0
- package/typings/config.d.ts +1 -0
- package/typings/index.d.cts +1 -0
- package/typings/index.d.ts +1 -0
- package/typings/init/questions.d.cts +2 -1
- package/typings/init/questions.d.ts +2 -1
- package/typings/init/types.d.cts +4 -3
- package/typings/init/types.d.ts +4 -3
package/cjs/codegen.js
CHANGED
|
@@ -125,7 +125,9 @@ async function executeCodegen(input) {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
if (rootSchemas.length === 0 &&
|
|
128
|
-
Object.keys(generates).some(filename => !generates[filename].schema ||
|
|
128
|
+
Object.keys(generates).some(filename => !generates[filename].schema ||
|
|
129
|
+
(Array.isArray(generates[filename].schema === 'object') &&
|
|
130
|
+
generates[filename].schema.length === 0))) {
|
|
129
131
|
throw new plugin_helpers_1.DetailedError('Invalid Codegen Configuration!', `
|
|
130
132
|
Please make sure that your codegen config file contains either the "schema" field
|
|
131
133
|
or every generated file has its own "schema" field.
|
|
@@ -153,18 +155,26 @@ async function executeCodegen(input) {
|
|
|
153
155
|
const generateTasks = Object.keys(generates).map(filename => {
|
|
154
156
|
const outputConfig = generates[filename];
|
|
155
157
|
const hasPreset = !!outputConfig.preset;
|
|
156
|
-
const title =
|
|
157
|
-
? `Generate to ${filename} (using EXPERIMENTAL preset "${outputConfig.preset}")`
|
|
158
|
-
: `Generate ${filename}`;
|
|
158
|
+
const title = `Generate to ${filename}`;
|
|
159
159
|
return {
|
|
160
160
|
title,
|
|
161
|
-
task: (_, subTask) => {
|
|
161
|
+
task: async (_, subTask) => {
|
|
162
162
|
let outputSchemaAst;
|
|
163
163
|
let outputSchema;
|
|
164
164
|
const outputFileTemplateConfig = outputConfig.config || {};
|
|
165
165
|
let outputDocuments = [];
|
|
166
166
|
const outputSpecificSchemas = (0, plugin_helpers_1.normalizeInstanceOrArray)(outputConfig.schema);
|
|
167
|
-
|
|
167
|
+
let outputSpecificDocuments = (0, plugin_helpers_1.normalizeInstanceOrArray)(outputConfig.documents);
|
|
168
|
+
const preset = hasPreset
|
|
169
|
+
? typeof outputConfig.preset === 'string'
|
|
170
|
+
? await (0, presets_js_1.getPresetByName)(outputConfig.preset, makeDefaultLoader(context.cwd))
|
|
171
|
+
: outputConfig.preset
|
|
172
|
+
: null;
|
|
173
|
+
if (preset) {
|
|
174
|
+
if (preset.prepareDocuments) {
|
|
175
|
+
outputSpecificDocuments = await preset.prepareDocuments(filename, outputSpecificDocuments);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
168
178
|
return subTask.newListr([
|
|
169
179
|
{
|
|
170
180
|
title: 'Load GraphQL schemas',
|
|
@@ -224,11 +234,6 @@ async function executeCodegen(input) {
|
|
|
224
234
|
const normalizedPluginsArray = (0, plugin_helpers_1.normalizeConfig)(outputConfig.plugins);
|
|
225
235
|
const pluginLoader = config.pluginLoader || makeDefaultLoader(context.cwd);
|
|
226
236
|
const pluginPackages = await Promise.all(normalizedPluginsArray.map(plugin => (0, plugins_js_1.getPluginByName)(Object.keys(plugin)[0], pluginLoader)));
|
|
227
|
-
const preset = hasPreset
|
|
228
|
-
? typeof outputConfig.preset === 'string'
|
|
229
|
-
? await (0, presets_js_1.getPresetByName)(outputConfig.preset, makeDefaultLoader(context.cwd))
|
|
230
|
-
: outputConfig.preset
|
|
231
|
-
: null;
|
|
232
237
|
const pluginMap = Object.fromEntries(pluginPackages.map((pkg, i) => {
|
|
233
238
|
const plugin = normalizedPluginsArray[i];
|
|
234
239
|
const name = Object.keys(plugin)[0];
|
|
@@ -241,7 +246,7 @@ async function executeCodegen(input) {
|
|
|
241
246
|
: outputFileTemplateConfig),
|
|
242
247
|
emitLegacyCommonJSImports: (0, config_js_1.shouldEmitLegacyCommonJSImports)(config, filename),
|
|
243
248
|
};
|
|
244
|
-
const outputs =
|
|
249
|
+
const outputs = preset
|
|
245
250
|
? await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
246
251
|
baseOutputDir: filename,
|
|
247
252
|
presetConfig: outputConfig.presetConfig || {},
|
package/cjs/config.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.shouldEmitLegacyCommonJSImports = exports.ensureContext = exports.CodegenContext = exports.updateContextWithCliFlags = exports.createContext = exports.parseArgv = exports.buildOptions = exports.loadContext = exports.loadCodegenConfig = exports.generateSearchPlaces = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const cosmiconfig_1 = require("cosmiconfig");
|
|
6
|
+
const cosmiconfig_typescript_loader_1 = require("cosmiconfig-typescript-loader");
|
|
6
7
|
const path_1 = require("path");
|
|
7
8
|
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
8
9
|
const string_env_interpolation_1 = require("string-env-interpolation");
|
|
@@ -16,7 +17,7 @@ const fs_1 = require("fs");
|
|
|
16
17
|
const crypto_1 = require("crypto");
|
|
17
18
|
const { lstat } = fs_1.promises;
|
|
18
19
|
function generateSearchPlaces(moduleName) {
|
|
19
|
-
const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
|
|
20
|
+
const extensions = ['json', 'yaml', 'yml', 'js', 'ts', 'config.js'];
|
|
20
21
|
// gives codegen.json...
|
|
21
22
|
const regular = extensions.map(ext => `${moduleName}.${ext}`);
|
|
22
23
|
// gives .codegenrc.json... but no .codegenrc.config.js
|
|
@@ -45,6 +46,9 @@ function customLoader(ext) {
|
|
|
45
46
|
if (ext === 'js') {
|
|
46
47
|
return cosmiconfig_1.defaultLoaders['.js'](filepath, content);
|
|
47
48
|
}
|
|
49
|
+
if (ext === 'ts') {
|
|
50
|
+
return (0, cosmiconfig_typescript_loader_1.TypeScriptLoader)()(filepath, content);
|
|
51
|
+
}
|
|
48
52
|
}
|
|
49
53
|
return loader;
|
|
50
54
|
}
|
|
@@ -60,6 +64,7 @@ async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: add
|
|
|
60
64
|
'.yaml': customLoader('yaml'),
|
|
61
65
|
'.yml': customLoader('yaml'),
|
|
62
66
|
'.js': customLoader('js'),
|
|
67
|
+
'.ts': customLoader('ts'),
|
|
63
68
|
noExt: customLoader('yaml'),
|
|
64
69
|
...customLoaders,
|
|
65
70
|
},
|
package/cjs/hooks.js
CHANGED
package/cjs/init/helpers.js
CHANGED
|
@@ -7,13 +7,44 @@ const path_1 = require("path");
|
|
|
7
7
|
const fs_1 = require("fs");
|
|
8
8
|
const detect_indent_1 = tslib_1.__importDefault(require("detect-indent"));
|
|
9
9
|
const get_latest_version_js_1 = require("../utils/get-latest-version.js");
|
|
10
|
+
const template_1 = tslib_1.__importDefault(require("@babel/template"));
|
|
11
|
+
const generator_1 = tslib_1.__importDefault(require("@babel/generator"));
|
|
12
|
+
const t = tslib_1.__importStar(require("@babel/types"));
|
|
13
|
+
function jsObjectToBabelObjectExpression(obj) {
|
|
14
|
+
const objExp = t.objectExpression([]);
|
|
15
|
+
Object.entries(obj).forEach(([key, val]) => {
|
|
16
|
+
if (Array.isArray(val)) {
|
|
17
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), t.arrayExpression(val.map(v => (typeof v === 'object' ? jsObjectToBabelObjectExpression(v) : t.valueToNode(v))))));
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), typeof val === 'object' ? jsObjectToBabelObjectExpression(val) : t.valueToNode(val)));
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return objExp;
|
|
24
|
+
}
|
|
10
25
|
// Parses config and writes it to a file
|
|
11
26
|
async function writeConfig(answers, config) {
|
|
12
27
|
const YAML = await Promise.resolve().then(() => tslib_1.__importStar(require('json-to-pretty-yaml'))).then(m => ('default' in m ? m.default : m));
|
|
13
|
-
const ext = answers.config.toLocaleLowerCase().
|
|
14
|
-
const content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
|
|
28
|
+
const ext = answers.config.toLocaleLowerCase().split('.')[1];
|
|
15
29
|
const fullPath = (0, path_1.resolve)(process.cwd(), answers.config);
|
|
16
30
|
const relativePath = (0, path_1.relative)(process.cwd(), answers.config);
|
|
31
|
+
let content;
|
|
32
|
+
if (ext === 'ts') {
|
|
33
|
+
const buildRequire = template_1.default.statement(`%%config%%`);
|
|
34
|
+
const ast = buildRequire({
|
|
35
|
+
config: jsObjectToBabelObjectExpression(config),
|
|
36
|
+
});
|
|
37
|
+
content = `
|
|
38
|
+
import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
39
|
+
|
|
40
|
+
const config: CodegenConfig = ${(0, generator_1.default)(ast).code.replace(/\(|\)/g, '')}
|
|
41
|
+
|
|
42
|
+
export default config;
|
|
43
|
+
`;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
|
|
47
|
+
}
|
|
17
48
|
(0, fs_1.writeFileSync)(fullPath, content, {
|
|
18
49
|
encoding: 'utf-8',
|
|
19
50
|
});
|
|
@@ -40,7 +71,7 @@ async function writePackage(answers, configLocation) {
|
|
|
40
71
|
if (!pkg.devDependencies) {
|
|
41
72
|
pkg.devDependencies = {};
|
|
42
73
|
}
|
|
43
|
-
await Promise.all(answers.plugins.map(async (plugin) => {
|
|
74
|
+
await Promise.all((answers.plugins || []).map(async (plugin) => {
|
|
44
75
|
pkg.devDependencies[plugin.package] = await (0, get_latest_version_js_1.getLatestVersion)(plugin.package);
|
|
45
76
|
}));
|
|
46
77
|
if (answers.introspection) {
|
package/cjs/init/index.js
CHANGED
|
@@ -22,10 +22,11 @@ async function init() {
|
|
|
22
22
|
const config = {
|
|
23
23
|
overwrite: true,
|
|
24
24
|
schema: answers.schema,
|
|
25
|
-
documents: answers.targets.includes(types_js_1.Tags.
|
|
25
|
+
documents: answers.targets.includes(types_js_1.Tags.client) ? answers.documents : undefined,
|
|
26
26
|
generates: {
|
|
27
27
|
[answers.output]: {
|
|
28
|
-
|
|
28
|
+
preset: answers.targets.includes(types_js_1.Tags.client) ? 'client' : undefined,
|
|
29
|
+
plugins: answers.plugins ? answers.plugins.map(p => p.value) : [],
|
|
29
30
|
},
|
|
30
31
|
},
|
|
31
32
|
};
|
|
@@ -41,7 +42,7 @@ async function init() {
|
|
|
41
42
|
// Emit status to the terminal
|
|
42
43
|
log(`
|
|
43
44
|
Config file generated at ${(0, helpers_js_1.bold)(relativePath)}
|
|
44
|
-
|
|
45
|
+
|
|
45
46
|
${(0, helpers_js_1.bold)('$ npm install')}
|
|
46
47
|
|
|
47
48
|
To install the plugins.
|
package/cjs/init/plugins.js
CHANGED
|
@@ -18,7 +18,7 @@ exports.plugins = [
|
|
|
18
18
|
package: '@graphql-codegen/typescript-operations',
|
|
19
19
|
value: 'typescript-operations',
|
|
20
20
|
pathInRepo: 'typescript/operations',
|
|
21
|
-
available: tags => allOf(tags, types_js_1.Tags.
|
|
21
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
22
22
|
shouldBeSelected: tags => oneOf(tags, types_js_1.Tags.angular, types_js_1.Tags.stencil) || allOf(tags, types_js_1.Tags.typescript, types_js_1.Tags.react),
|
|
23
23
|
defaultExtension: '.ts',
|
|
24
24
|
},
|
|
@@ -45,7 +45,7 @@ exports.plugins = [
|
|
|
45
45
|
package: '@graphql-codegen/flow-operations',
|
|
46
46
|
value: 'flow-operations',
|
|
47
47
|
pathInRepo: 'flow/operations',
|
|
48
|
-
available: tags => allOf(tags, types_js_1.Tags.
|
|
48
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.flow),
|
|
49
49
|
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.typescript),
|
|
50
50
|
defaultExtension: '.js',
|
|
51
51
|
},
|
|
@@ -58,42 +58,6 @@ exports.plugins = [
|
|
|
58
58
|
shouldBeSelected: tags => noneOf(tags, types_js_1.Tags.typescript),
|
|
59
59
|
defaultExtension: '.js',
|
|
60
60
|
},
|
|
61
|
-
{
|
|
62
|
-
name: `TypeScript Apollo Angular ${(0, helpers_js_1.italic)('(typed GQL services)')}`,
|
|
63
|
-
package: '@graphql-codegen/typescript-apollo-angular',
|
|
64
|
-
value: 'typescript-apollo-angular',
|
|
65
|
-
pathInRepo: 'typescript/apollo-angular',
|
|
66
|
-
available: hasTag(types_js_1.Tags.angular),
|
|
67
|
-
shouldBeSelected: () => true,
|
|
68
|
-
defaultExtension: '.js',
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: `TypeScript Vue Apollo Composition API ${(0, helpers_js_1.italic)('(typed functions)')}`,
|
|
72
|
-
package: '@graphql-codegen/typescript-vue-apollo',
|
|
73
|
-
value: 'typescript-vue-apollo',
|
|
74
|
-
pathInRepo: 'typescript/vue-apollo',
|
|
75
|
-
available: tags => allOf(tags, types_js_1.Tags.vue, types_js_1.Tags.typescript),
|
|
76
|
-
shouldBeSelected: () => true,
|
|
77
|
-
defaultExtension: '.ts',
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
name: `TypeScript Vue Apollo Smart Operations ${(0, helpers_js_1.italic)('(typed functions)')}`,
|
|
81
|
-
package: '@graphql-codegen/typescript-vue-apollo-smart-ops',
|
|
82
|
-
value: 'typescript-vue-apollo-smart-ops',
|
|
83
|
-
pathInRepo: 'typescript/vue-apollo-smart-ops',
|
|
84
|
-
available: tags => allOf(tags, types_js_1.Tags.vue, types_js_1.Tags.typescript),
|
|
85
|
-
shouldBeSelected: () => true,
|
|
86
|
-
defaultExtension: '.ts',
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
name: `TypeScript React Apollo ${(0, helpers_js_1.italic)('(typed components and HOCs)')}`,
|
|
90
|
-
package: '@graphql-codegen/typescript-react-apollo',
|
|
91
|
-
value: 'typescript-react-apollo',
|
|
92
|
-
pathInRepo: 'typescript/react-apollo',
|
|
93
|
-
available: tags => allOf(tags, types_js_1.Tags.react, types_js_1.Tags.typescript),
|
|
94
|
-
shouldBeSelected: () => true,
|
|
95
|
-
defaultExtension: '.tsx',
|
|
96
|
-
},
|
|
97
61
|
{
|
|
98
62
|
name: `TypeScript Stencil Apollo ${(0, helpers_js_1.italic)('(typed components)')}`,
|
|
99
63
|
package: '@graphql-codegen/typescript-stencil-apollo',
|
|
@@ -117,7 +81,7 @@ exports.plugins = [
|
|
|
117
81
|
package: '@graphql-codegen/typescript-graphql-files-modules',
|
|
118
82
|
value: 'typescript-graphql-files-modules',
|
|
119
83
|
pathInRepo: 'typescript/graphql-files-modules',
|
|
120
|
-
available: tags => allOf(tags, types_js_1.Tags.
|
|
84
|
+
available: tags => allOf(tags, types_js_1.Tags.client, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
121
85
|
shouldBeSelected: () => false,
|
|
122
86
|
defaultExtension: '.ts',
|
|
123
87
|
},
|
|
@@ -126,7 +90,7 @@ exports.plugins = [
|
|
|
126
90
|
package: '@graphql-codegen/typescript-document-nodes',
|
|
127
91
|
value: 'typescript-document-nodes',
|
|
128
92
|
pathInRepo: 'typescript/document-nodes',
|
|
129
|
-
available: tags => allOf(tags, types_js_1.Tags.typescript),
|
|
93
|
+
available: tags => allOf(tags, types_js_1.Tags.typescript) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
130
94
|
shouldBeSelected: () => false,
|
|
131
95
|
defaultExtension: '.ts',
|
|
132
96
|
},
|
|
@@ -135,7 +99,7 @@ exports.plugins = [
|
|
|
135
99
|
package: '@graphql-codegen/fragment-matcher',
|
|
136
100
|
value: 'fragment-matcher',
|
|
137
101
|
pathInRepo: 'other/fragment-matcher',
|
|
138
|
-
available: hasTag(types_js_1.Tags.
|
|
102
|
+
available: tags => hasTag(types_js_1.Tags.client)(tags) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
139
103
|
shouldBeSelected: () => false,
|
|
140
104
|
defaultExtension: '.ts',
|
|
141
105
|
},
|
|
@@ -144,7 +108,7 @@ exports.plugins = [
|
|
|
144
108
|
package: '@graphql-codegen/urql-introspection',
|
|
145
109
|
value: 'urql-introspection',
|
|
146
110
|
pathInRepo: 'other/urql-introspection',
|
|
147
|
-
available: hasTag(types_js_1.Tags.
|
|
111
|
+
available: tags => hasTag(types_js_1.Tags.client)(tags) || hasTag(types_js_1.Tags.stencil)(tags),
|
|
148
112
|
shouldBeSelected: () => false,
|
|
149
113
|
defaultExtension: '.ts',
|
|
150
114
|
},
|
package/cjs/init/questions.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getOutputDefaultValue = exports.getPluginChoices = exports.getApplicationTypeChoices = exports.getQuestions = void 0;
|
|
3
|
+
exports.getDocumentsDefaultValue = exports.getOutputDefaultValue = exports.getPluginChoices = exports.getApplicationTypeChoices = exports.getQuestions = void 0;
|
|
4
4
|
const helpers_js_1 = require("./helpers.js");
|
|
5
5
|
const types_js_1 = require("./types.js");
|
|
6
6
|
const plugins_js_1 = require("./plugins.js");
|
|
7
7
|
function getQuestions(possibleTargets) {
|
|
8
8
|
return [
|
|
9
9
|
{
|
|
10
|
-
type: '
|
|
10
|
+
type: 'list',
|
|
11
11
|
name: 'targets',
|
|
12
12
|
message: `What type of application are you building?`,
|
|
13
13
|
choices: getApplicationTypeChoices(possibleTargets),
|
|
14
14
|
validate: ((targets) => targets.length > 0),
|
|
15
|
+
default: getApplicationTypeChoices(possibleTargets).findIndex(c => c.checked),
|
|
15
16
|
},
|
|
16
17
|
{
|
|
17
18
|
type: 'input',
|
|
@@ -29,14 +30,20 @@ function getQuestions(possibleTargets) {
|
|
|
29
30
|
// flatten targets
|
|
30
31
|
// I can't find an API in Inquirer that would do that
|
|
31
32
|
answers.targets = normalizeTargets(answers.targets);
|
|
32
|
-
return answers.targets.includes(types_js_1.Tags.
|
|
33
|
+
return answers.targets.includes(types_js_1.Tags.client) || answers.targets.includes(types_js_1.Tags.stencil);
|
|
33
34
|
},
|
|
34
|
-
default:
|
|
35
|
+
default: getDocumentsDefaultValue,
|
|
35
36
|
validate: (str) => str.length > 0,
|
|
36
37
|
},
|
|
37
38
|
{
|
|
38
39
|
type: 'checkbox',
|
|
39
40
|
name: 'plugins',
|
|
41
|
+
when: answers => {
|
|
42
|
+
// flatten targets
|
|
43
|
+
// I can't find an API in Inquirer that would do that
|
|
44
|
+
answers.targets = normalizeTargets(answers.targets);
|
|
45
|
+
return !answers.targets.includes(types_js_1.Tags.client);
|
|
46
|
+
},
|
|
40
47
|
message: 'Pick plugins:',
|
|
41
48
|
choices: getPluginChoices,
|
|
42
49
|
validate: ((plugins) => plugins.length > 0),
|
|
@@ -51,22 +58,24 @@ function getQuestions(possibleTargets) {
|
|
|
51
58
|
{
|
|
52
59
|
type: 'confirm',
|
|
53
60
|
name: 'introspection',
|
|
61
|
+
default: false,
|
|
54
62
|
message: 'Do you want to generate an introspection file?',
|
|
55
63
|
},
|
|
56
64
|
{
|
|
57
65
|
type: 'input',
|
|
58
66
|
name: 'config',
|
|
59
67
|
message: 'How to name the config file?',
|
|
60
|
-
default: 'codegen.yml',
|
|
68
|
+
default: answers => (answers.targets.includes(types_js_1.Tags.client) ? 'codegen.ts' : 'codegen.yml'),
|
|
61
69
|
validate: (str) => {
|
|
62
70
|
const isNotEmpty = str.length > 0;
|
|
63
|
-
const hasCorrectExtension = ['json', 'yml', 'yaml'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
|
|
71
|
+
const hasCorrectExtension = ['json', 'yml', 'yaml', 'js', 'ts'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
|
|
64
72
|
return isNotEmpty && hasCorrectExtension;
|
|
65
73
|
},
|
|
66
74
|
},
|
|
67
75
|
{
|
|
68
76
|
type: 'input',
|
|
69
77
|
name: 'script',
|
|
78
|
+
default: 'codegen',
|
|
70
79
|
message: 'What script in package.json should run the codegen?',
|
|
71
80
|
validate: (str) => str.length > 0,
|
|
72
81
|
},
|
|
@@ -81,8 +90,9 @@ function getApplicationTypeChoices(possibleTargets) {
|
|
|
81
90
|
else if (possibleTargets.Flow) {
|
|
82
91
|
tags.push(types_js_1.Tags.flow);
|
|
83
92
|
}
|
|
84
|
-
else {
|
|
85
|
-
tags.push(types_js_1.Tags.
|
|
93
|
+
else if (possibleTargets.Node) {
|
|
94
|
+
tags.push(types_js_1.Tags.typescript);
|
|
95
|
+
tags.push(types_js_1.Tags.flow);
|
|
86
96
|
}
|
|
87
97
|
return tags;
|
|
88
98
|
}
|
|
@@ -96,25 +106,37 @@ function getApplicationTypeChoices(possibleTargets) {
|
|
|
96
106
|
{
|
|
97
107
|
name: 'Application built with Angular',
|
|
98
108
|
key: 'angular',
|
|
99
|
-
value: [types_js_1.Tags.angular, types_js_1.Tags.
|
|
109
|
+
value: [types_js_1.Tags.angular, types_js_1.Tags.client],
|
|
100
110
|
checked: possibleTargets.Angular,
|
|
101
111
|
},
|
|
102
112
|
{
|
|
103
113
|
name: 'Application built with React',
|
|
104
114
|
key: 'react',
|
|
105
|
-
value: withFlowOrTypescript([types_js_1.Tags.react, types_js_1.Tags.
|
|
115
|
+
value: withFlowOrTypescript([types_js_1.Tags.react, types_js_1.Tags.client]),
|
|
106
116
|
checked: possibleTargets.React,
|
|
107
117
|
},
|
|
108
118
|
{
|
|
109
119
|
name: 'Application built with Stencil',
|
|
110
120
|
key: 'stencil',
|
|
111
|
-
value: [types_js_1.Tags.stencil, types_js_1.Tags.
|
|
121
|
+
value: [types_js_1.Tags.stencil, types_js_1.Tags.typescript],
|
|
112
122
|
checked: possibleTargets.Stencil,
|
|
113
123
|
},
|
|
124
|
+
{
|
|
125
|
+
name: 'Application built with Vue',
|
|
126
|
+
key: 'vue',
|
|
127
|
+
value: [types_js_1.Tags.vue, types_js_1.Tags.client],
|
|
128
|
+
checked: possibleTargets.Vue,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'Application using graphql-request',
|
|
132
|
+
key: 'graphqlRequest',
|
|
133
|
+
value: [types_js_1.Tags.graphqlRequest, types_js_1.Tags.client],
|
|
134
|
+
checked: possibleTargets.graphqlRequest,
|
|
135
|
+
},
|
|
114
136
|
{
|
|
115
137
|
name: 'Application built with other framework or vanilla JS',
|
|
116
138
|
key: 'client',
|
|
117
|
-
value: [types_js_1.Tags.
|
|
139
|
+
value: [types_js_1.Tags.typescript, types_js_1.Tags.flow],
|
|
118
140
|
checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil,
|
|
119
141
|
},
|
|
120
142
|
];
|
|
@@ -136,6 +158,9 @@ function normalizeTargets(targets) {
|
|
|
136
158
|
return [].concat(...targets);
|
|
137
159
|
}
|
|
138
160
|
function getOutputDefaultValue(answers) {
|
|
161
|
+
if (answers.targets.includes(types_js_1.Tags.client)) {
|
|
162
|
+
return 'src/gql';
|
|
163
|
+
}
|
|
139
164
|
if (answers.plugins.some(plugin => plugin.defaultExtension === '.tsx')) {
|
|
140
165
|
return 'src/generated/graphql.tsx';
|
|
141
166
|
}
|
|
@@ -145,3 +170,18 @@ function getOutputDefaultValue(answers) {
|
|
|
145
170
|
return 'src/generated/graphql.js';
|
|
146
171
|
}
|
|
147
172
|
exports.getOutputDefaultValue = getOutputDefaultValue;
|
|
173
|
+
function getDocumentsDefaultValue(answers) {
|
|
174
|
+
if (answers.targets.includes(types_js_1.Tags.vue)) {
|
|
175
|
+
return 'src/**/*.vue';
|
|
176
|
+
}
|
|
177
|
+
else if (answers.targets.includes(types_js_1.Tags.angular)) {
|
|
178
|
+
return 'src/**/*.ts';
|
|
179
|
+
}
|
|
180
|
+
else if (answers.targets.includes(types_js_1.Tags.client)) {
|
|
181
|
+
return 'src/**/*.tsx';
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return 'src/**/*.graphql';
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.getDocumentsDefaultValue = getDocumentsDefaultValue;
|
package/cjs/init/targets.js
CHANGED
|
@@ -17,10 +17,11 @@ async function guessTargets() {
|
|
|
17
17
|
[types_js_1.Tags.react]: isReact(dependencies),
|
|
18
18
|
[types_js_1.Tags.stencil]: isStencil(dependencies),
|
|
19
19
|
[types_js_1.Tags.vue]: isVue(dependencies),
|
|
20
|
-
[types_js_1.Tags.
|
|
20
|
+
[types_js_1.Tags.client]: false,
|
|
21
21
|
[types_js_1.Tags.node]: false,
|
|
22
22
|
[types_js_1.Tags.typescript]: isTypescript(dependencies),
|
|
23
23
|
[types_js_1.Tags.flow]: isFlow(dependencies),
|
|
24
|
+
[types_js_1.Tags.graphqlRequest]: isGraphqlRequest(dependencies),
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
27
|
exports.guessTargets = guessTargets;
|
|
@@ -42,3 +43,6 @@ function isTypescript(dependencies) {
|
|
|
42
43
|
function isFlow(dependencies) {
|
|
43
44
|
return dependencies.includes('flow');
|
|
44
45
|
}
|
|
46
|
+
function isGraphqlRequest(dependencies) {
|
|
47
|
+
return dependencies.includes('graphql-request');
|
|
48
|
+
}
|
package/cjs/init/types.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Tags = void 0;
|
|
4
4
|
var Tags;
|
|
5
5
|
(function (Tags) {
|
|
6
|
-
Tags["
|
|
6
|
+
Tags["client"] = "Browser";
|
|
7
7
|
Tags["node"] = "Node";
|
|
8
8
|
Tags["typescript"] = "TypeScript";
|
|
9
9
|
Tags["flow"] = "Flow";
|
|
@@ -11,4 +11,5 @@ var Tags;
|
|
|
11
11
|
Tags["stencil"] = "Stencil";
|
|
12
12
|
Tags["react"] = "React";
|
|
13
13
|
Tags["vue"] = "Vue";
|
|
14
|
+
Tags["graphqlRequest"] = "graphqlRequest";
|
|
14
15
|
})(Tags = exports.Tags || (exports.Tags = {}));
|
package/cjs/presets.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPresetByName = void 0;
|
|
4
|
-
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
4
|
const path_1 = require("path");
|
|
6
5
|
async function getPresetByName(name, loader) {
|
|
7
6
|
const possibleNames = [
|
|
@@ -27,7 +26,8 @@ async function getPresetByName(name, loader) {
|
|
|
27
26
|
err.code !== 'MODULE_NOT_FOUND' &&
|
|
28
27
|
/** ESM Error code */
|
|
29
28
|
err.code !== 'ERR_MODULE_NOT_FOUND') {
|
|
30
|
-
throw new
|
|
29
|
+
throw new Error(`Unable to load preset matching ${name}
|
|
30
|
+
|
|
31
31
|
Unable to load preset matching '${name}'.
|
|
32
32
|
Reason:
|
|
33
33
|
${err.message}
|
|
@@ -40,7 +40,8 @@ async function getPresetByName(name, loader) {
|
|
|
40
40
|
- ${name}
|
|
41
41
|
`.trimRight())
|
|
42
42
|
.join('');
|
|
43
|
-
throw new
|
|
43
|
+
throw new Error(`Unable to find preset matching ${name}
|
|
44
|
+
|
|
44
45
|
Unable to find preset matching '${name}'
|
|
45
46
|
Install one of the following packages:
|
|
46
47
|
|
package/esm/codegen.js
CHANGED
|
@@ -121,7 +121,9 @@ export async function executeCodegen(input) {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
if (rootSchemas.length === 0 &&
|
|
124
|
-
Object.keys(generates).some(filename => !generates[filename].schema ||
|
|
124
|
+
Object.keys(generates).some(filename => !generates[filename].schema ||
|
|
125
|
+
(Array.isArray(generates[filename].schema === 'object') &&
|
|
126
|
+
generates[filename].schema.length === 0))) {
|
|
125
127
|
throw new DetailedError('Invalid Codegen Configuration!', `
|
|
126
128
|
Please make sure that your codegen config file contains either the "schema" field
|
|
127
129
|
or every generated file has its own "schema" field.
|
|
@@ -149,18 +151,26 @@ export async function executeCodegen(input) {
|
|
|
149
151
|
const generateTasks = Object.keys(generates).map(filename => {
|
|
150
152
|
const outputConfig = generates[filename];
|
|
151
153
|
const hasPreset = !!outputConfig.preset;
|
|
152
|
-
const title =
|
|
153
|
-
? `Generate to ${filename} (using EXPERIMENTAL preset "${outputConfig.preset}")`
|
|
154
|
-
: `Generate ${filename}`;
|
|
154
|
+
const title = `Generate to ${filename}`;
|
|
155
155
|
return {
|
|
156
156
|
title,
|
|
157
|
-
task: (_, subTask) => {
|
|
157
|
+
task: async (_, subTask) => {
|
|
158
158
|
let outputSchemaAst;
|
|
159
159
|
let outputSchema;
|
|
160
160
|
const outputFileTemplateConfig = outputConfig.config || {};
|
|
161
161
|
let outputDocuments = [];
|
|
162
162
|
const outputSpecificSchemas = normalizeInstanceOrArray(outputConfig.schema);
|
|
163
|
-
|
|
163
|
+
let outputSpecificDocuments = normalizeInstanceOrArray(outputConfig.documents);
|
|
164
|
+
const preset = hasPreset
|
|
165
|
+
? typeof outputConfig.preset === 'string'
|
|
166
|
+
? await getPresetByName(outputConfig.preset, makeDefaultLoader(context.cwd))
|
|
167
|
+
: outputConfig.preset
|
|
168
|
+
: null;
|
|
169
|
+
if (preset) {
|
|
170
|
+
if (preset.prepareDocuments) {
|
|
171
|
+
outputSpecificDocuments = await preset.prepareDocuments(filename, outputSpecificDocuments);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
164
174
|
return subTask.newListr([
|
|
165
175
|
{
|
|
166
176
|
title: 'Load GraphQL schemas',
|
|
@@ -220,11 +230,6 @@ export async function executeCodegen(input) {
|
|
|
220
230
|
const normalizedPluginsArray = normalizeConfig(outputConfig.plugins);
|
|
221
231
|
const pluginLoader = config.pluginLoader || makeDefaultLoader(context.cwd);
|
|
222
232
|
const pluginPackages = await Promise.all(normalizedPluginsArray.map(plugin => getPluginByName(Object.keys(plugin)[0], pluginLoader)));
|
|
223
|
-
const preset = hasPreset
|
|
224
|
-
? typeof outputConfig.preset === 'string'
|
|
225
|
-
? await getPresetByName(outputConfig.preset, makeDefaultLoader(context.cwd))
|
|
226
|
-
: outputConfig.preset
|
|
227
|
-
: null;
|
|
228
233
|
const pluginMap = Object.fromEntries(pluginPackages.map((pkg, i) => {
|
|
229
234
|
const plugin = normalizedPluginsArray[i];
|
|
230
235
|
const name = Object.keys(plugin)[0];
|
|
@@ -237,7 +242,7 @@ export async function executeCodegen(input) {
|
|
|
237
242
|
: outputFileTemplateConfig),
|
|
238
243
|
emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config, filename),
|
|
239
244
|
};
|
|
240
|
-
const outputs =
|
|
245
|
+
const outputs = preset
|
|
241
246
|
? await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
242
247
|
baseOutputDir: filename,
|
|
243
248
|
presetConfig: outputConfig.presetConfig || {},
|
package/esm/config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
|
|
2
|
+
import { TypeScriptLoader } from 'cosmiconfig-typescript-loader';
|
|
2
3
|
import { resolve } from 'path';
|
|
3
4
|
import { DetailedError, createProfiler, createNoopProfiler, getCachedDocumentNodeFromSchema, } from '@graphql-codegen/plugin-helpers';
|
|
4
5
|
import { env } from 'string-env-interpolation';
|
|
@@ -12,7 +13,7 @@ import { promises } from 'fs';
|
|
|
12
13
|
import { createHash } from 'crypto';
|
|
13
14
|
const { lstat } = promises;
|
|
14
15
|
export function generateSearchPlaces(moduleName) {
|
|
15
|
-
const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
|
|
16
|
+
const extensions = ['json', 'yaml', 'yml', 'js', 'ts', 'config.js'];
|
|
16
17
|
// gives codegen.json...
|
|
17
18
|
const regular = extensions.map(ext => `${moduleName}.${ext}`);
|
|
18
19
|
// gives .codegenrc.json... but no .codegenrc.config.js
|
|
@@ -40,6 +41,9 @@ function customLoader(ext) {
|
|
|
40
41
|
if (ext === 'js') {
|
|
41
42
|
return defaultLoaders['.js'](filepath, content);
|
|
42
43
|
}
|
|
44
|
+
if (ext === 'ts') {
|
|
45
|
+
return TypeScriptLoader()(filepath, content);
|
|
46
|
+
}
|
|
43
47
|
}
|
|
44
48
|
return loader;
|
|
45
49
|
}
|
|
@@ -55,6 +59,7 @@ export async function loadCodegenConfig({ configFilePath, moduleName, searchPlac
|
|
|
55
59
|
'.yaml': customLoader('yaml'),
|
|
56
60
|
'.yml': customLoader('yaml'),
|
|
57
61
|
'.js': customLoader('js'),
|
|
62
|
+
'.ts': customLoader('ts'),
|
|
58
63
|
noExt: customLoader('yaml'),
|
|
59
64
|
...customLoaders,
|
|
60
65
|
},
|
package/esm/hooks.js
CHANGED
package/esm/init/helpers.js
CHANGED
|
@@ -3,13 +3,44 @@ import { resolve, relative } from 'path';
|
|
|
3
3
|
import { writeFileSync, readFileSync } from 'fs';
|
|
4
4
|
import detectIndent from 'detect-indent';
|
|
5
5
|
import { getLatestVersion } from '../utils/get-latest-version.js';
|
|
6
|
+
import template from '@babel/template';
|
|
7
|
+
import generate from '@babel/generator';
|
|
8
|
+
import * as t from '@babel/types';
|
|
9
|
+
function jsObjectToBabelObjectExpression(obj) {
|
|
10
|
+
const objExp = t.objectExpression([]);
|
|
11
|
+
Object.entries(obj).forEach(([key, val]) => {
|
|
12
|
+
if (Array.isArray(val)) {
|
|
13
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), t.arrayExpression(val.map(v => (typeof v === 'object' ? jsObjectToBabelObjectExpression(v) : t.valueToNode(v))))));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
objExp.properties.push(t.objectProperty(/^[a-zA-Z0-9]+$/.test(key) ? t.identifier(key) : t.stringLiteral(key), typeof val === 'object' ? jsObjectToBabelObjectExpression(val) : t.valueToNode(val)));
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return objExp;
|
|
20
|
+
}
|
|
6
21
|
// Parses config and writes it to a file
|
|
7
22
|
export async function writeConfig(answers, config) {
|
|
8
23
|
const YAML = await import('json-to-pretty-yaml').then(m => ('default' in m ? m.default : m));
|
|
9
|
-
const ext = answers.config.toLocaleLowerCase().
|
|
10
|
-
const content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
|
|
24
|
+
const ext = answers.config.toLocaleLowerCase().split('.')[1];
|
|
11
25
|
const fullPath = resolve(process.cwd(), answers.config);
|
|
12
26
|
const relativePath = relative(process.cwd(), answers.config);
|
|
27
|
+
let content;
|
|
28
|
+
if (ext === 'ts') {
|
|
29
|
+
const buildRequire = template.statement(`%%config%%`);
|
|
30
|
+
const ast = buildRequire({
|
|
31
|
+
config: jsObjectToBabelObjectExpression(config),
|
|
32
|
+
});
|
|
33
|
+
content = `
|
|
34
|
+
import type { CodegenConfig } from '@graphql-codegen/cli';
|
|
35
|
+
|
|
36
|
+
const config: CodegenConfig = ${generate(ast).code.replace(/\(|\)/g, '')}
|
|
37
|
+
|
|
38
|
+
export default config;
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
content = ext === 'json' ? JSON.stringify(config) : YAML.stringify(config);
|
|
43
|
+
}
|
|
13
44
|
writeFileSync(fullPath, content, {
|
|
14
45
|
encoding: 'utf-8',
|
|
15
46
|
});
|
|
@@ -35,7 +66,7 @@ export async function writePackage(answers, configLocation) {
|
|
|
35
66
|
if (!pkg.devDependencies) {
|
|
36
67
|
pkg.devDependencies = {};
|
|
37
68
|
}
|
|
38
|
-
await Promise.all(answers.plugins.map(async (plugin) => {
|
|
69
|
+
await Promise.all((answers.plugins || []).map(async (plugin) => {
|
|
39
70
|
pkg.devDependencies[plugin.package] = await getLatestVersion(plugin.package);
|
|
40
71
|
}));
|
|
41
72
|
if (answers.introspection) {
|
package/esm/init/index.js
CHANGED
|
@@ -18,10 +18,11 @@ export async function init() {
|
|
|
18
18
|
const config = {
|
|
19
19
|
overwrite: true,
|
|
20
20
|
schema: answers.schema,
|
|
21
|
-
documents: answers.targets.includes(Tags.
|
|
21
|
+
documents: answers.targets.includes(Tags.client) ? answers.documents : undefined,
|
|
22
22
|
generates: {
|
|
23
23
|
[answers.output]: {
|
|
24
|
-
|
|
24
|
+
preset: answers.targets.includes(Tags.client) ? 'client' : undefined,
|
|
25
|
+
plugins: answers.plugins ? answers.plugins.map(p => p.value) : [],
|
|
25
26
|
},
|
|
26
27
|
},
|
|
27
28
|
};
|
|
@@ -37,7 +38,7 @@ export async function init() {
|
|
|
37
38
|
// Emit status to the terminal
|
|
38
39
|
log(`
|
|
39
40
|
Config file generated at ${bold(relativePath)}
|
|
40
|
-
|
|
41
|
+
|
|
41
42
|
${bold('$ npm install')}
|
|
42
43
|
|
|
43
44
|
To install the plugins.
|
package/esm/init/plugins.js
CHANGED
|
@@ -15,7 +15,7 @@ export const plugins = [
|
|
|
15
15
|
package: '@graphql-codegen/typescript-operations',
|
|
16
16
|
value: 'typescript-operations',
|
|
17
17
|
pathInRepo: 'typescript/operations',
|
|
18
|
-
available: tags => allOf(tags, Tags.
|
|
18
|
+
available: tags => allOf(tags, Tags.client, Tags.typescript) || hasTag(Tags.stencil)(tags),
|
|
19
19
|
shouldBeSelected: tags => oneOf(tags, Tags.angular, Tags.stencil) || allOf(tags, Tags.typescript, Tags.react),
|
|
20
20
|
defaultExtension: '.ts',
|
|
21
21
|
},
|
|
@@ -42,7 +42,7 @@ export const plugins = [
|
|
|
42
42
|
package: '@graphql-codegen/flow-operations',
|
|
43
43
|
value: 'flow-operations',
|
|
44
44
|
pathInRepo: 'flow/operations',
|
|
45
|
-
available: tags => allOf(tags, Tags.
|
|
45
|
+
available: tags => allOf(tags, Tags.client, Tags.flow),
|
|
46
46
|
shouldBeSelected: tags => noneOf(tags, Tags.typescript),
|
|
47
47
|
defaultExtension: '.js',
|
|
48
48
|
},
|
|
@@ -55,42 +55,6 @@ export const plugins = [
|
|
|
55
55
|
shouldBeSelected: tags => noneOf(tags, Tags.typescript),
|
|
56
56
|
defaultExtension: '.js',
|
|
57
57
|
},
|
|
58
|
-
{
|
|
59
|
-
name: `TypeScript Apollo Angular ${italic('(typed GQL services)')}`,
|
|
60
|
-
package: '@graphql-codegen/typescript-apollo-angular',
|
|
61
|
-
value: 'typescript-apollo-angular',
|
|
62
|
-
pathInRepo: 'typescript/apollo-angular',
|
|
63
|
-
available: hasTag(Tags.angular),
|
|
64
|
-
shouldBeSelected: () => true,
|
|
65
|
-
defaultExtension: '.js',
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: `TypeScript Vue Apollo Composition API ${italic('(typed functions)')}`,
|
|
69
|
-
package: '@graphql-codegen/typescript-vue-apollo',
|
|
70
|
-
value: 'typescript-vue-apollo',
|
|
71
|
-
pathInRepo: 'typescript/vue-apollo',
|
|
72
|
-
available: tags => allOf(tags, Tags.vue, Tags.typescript),
|
|
73
|
-
shouldBeSelected: () => true,
|
|
74
|
-
defaultExtension: '.ts',
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
name: `TypeScript Vue Apollo Smart Operations ${italic('(typed functions)')}`,
|
|
78
|
-
package: '@graphql-codegen/typescript-vue-apollo-smart-ops',
|
|
79
|
-
value: 'typescript-vue-apollo-smart-ops',
|
|
80
|
-
pathInRepo: 'typescript/vue-apollo-smart-ops',
|
|
81
|
-
available: tags => allOf(tags, Tags.vue, Tags.typescript),
|
|
82
|
-
shouldBeSelected: () => true,
|
|
83
|
-
defaultExtension: '.ts',
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
name: `TypeScript React Apollo ${italic('(typed components and HOCs)')}`,
|
|
87
|
-
package: '@graphql-codegen/typescript-react-apollo',
|
|
88
|
-
value: 'typescript-react-apollo',
|
|
89
|
-
pathInRepo: 'typescript/react-apollo',
|
|
90
|
-
available: tags => allOf(tags, Tags.react, Tags.typescript),
|
|
91
|
-
shouldBeSelected: () => true,
|
|
92
|
-
defaultExtension: '.tsx',
|
|
93
|
-
},
|
|
94
58
|
{
|
|
95
59
|
name: `TypeScript Stencil Apollo ${italic('(typed components)')}`,
|
|
96
60
|
package: '@graphql-codegen/typescript-stencil-apollo',
|
|
@@ -114,7 +78,7 @@ export const plugins = [
|
|
|
114
78
|
package: '@graphql-codegen/typescript-graphql-files-modules',
|
|
115
79
|
value: 'typescript-graphql-files-modules',
|
|
116
80
|
pathInRepo: 'typescript/graphql-files-modules',
|
|
117
|
-
available: tags => allOf(tags, Tags.
|
|
81
|
+
available: tags => allOf(tags, Tags.client, Tags.typescript) || hasTag(Tags.stencil)(tags),
|
|
118
82
|
shouldBeSelected: () => false,
|
|
119
83
|
defaultExtension: '.ts',
|
|
120
84
|
},
|
|
@@ -123,7 +87,7 @@ export const plugins = [
|
|
|
123
87
|
package: '@graphql-codegen/typescript-document-nodes',
|
|
124
88
|
value: 'typescript-document-nodes',
|
|
125
89
|
pathInRepo: 'typescript/document-nodes',
|
|
126
|
-
available: tags => allOf(tags, Tags.typescript),
|
|
90
|
+
available: tags => allOf(tags, Tags.typescript) || hasTag(Tags.stencil)(tags),
|
|
127
91
|
shouldBeSelected: () => false,
|
|
128
92
|
defaultExtension: '.ts',
|
|
129
93
|
},
|
|
@@ -132,7 +96,7 @@ export const plugins = [
|
|
|
132
96
|
package: '@graphql-codegen/fragment-matcher',
|
|
133
97
|
value: 'fragment-matcher',
|
|
134
98
|
pathInRepo: 'other/fragment-matcher',
|
|
135
|
-
available: hasTag(Tags.
|
|
99
|
+
available: tags => hasTag(Tags.client)(tags) || hasTag(Tags.stencil)(tags),
|
|
136
100
|
shouldBeSelected: () => false,
|
|
137
101
|
defaultExtension: '.ts',
|
|
138
102
|
},
|
|
@@ -141,7 +105,7 @@ export const plugins = [
|
|
|
141
105
|
package: '@graphql-codegen/urql-introspection',
|
|
142
106
|
value: 'urql-introspection',
|
|
143
107
|
pathInRepo: 'other/urql-introspection',
|
|
144
|
-
available: hasTag(Tags.
|
|
108
|
+
available: tags => hasTag(Tags.client)(tags) || hasTag(Tags.stencil)(tags),
|
|
145
109
|
shouldBeSelected: () => false,
|
|
146
110
|
defaultExtension: '.ts',
|
|
147
111
|
},
|
package/esm/init/questions.js
CHANGED
|
@@ -4,11 +4,12 @@ import { plugins } from './plugins.js';
|
|
|
4
4
|
export function getQuestions(possibleTargets) {
|
|
5
5
|
return [
|
|
6
6
|
{
|
|
7
|
-
type: '
|
|
7
|
+
type: 'list',
|
|
8
8
|
name: 'targets',
|
|
9
9
|
message: `What type of application are you building?`,
|
|
10
10
|
choices: getApplicationTypeChoices(possibleTargets),
|
|
11
11
|
validate: ((targets) => targets.length > 0),
|
|
12
|
+
default: getApplicationTypeChoices(possibleTargets).findIndex(c => c.checked),
|
|
12
13
|
},
|
|
13
14
|
{
|
|
14
15
|
type: 'input',
|
|
@@ -26,14 +27,20 @@ export function getQuestions(possibleTargets) {
|
|
|
26
27
|
// flatten targets
|
|
27
28
|
// I can't find an API in Inquirer that would do that
|
|
28
29
|
answers.targets = normalizeTargets(answers.targets);
|
|
29
|
-
return answers.targets.includes(Tags.
|
|
30
|
+
return answers.targets.includes(Tags.client) || answers.targets.includes(Tags.stencil);
|
|
30
31
|
},
|
|
31
|
-
default:
|
|
32
|
+
default: getDocumentsDefaultValue,
|
|
32
33
|
validate: (str) => str.length > 0,
|
|
33
34
|
},
|
|
34
35
|
{
|
|
35
36
|
type: 'checkbox',
|
|
36
37
|
name: 'plugins',
|
|
38
|
+
when: answers => {
|
|
39
|
+
// flatten targets
|
|
40
|
+
// I can't find an API in Inquirer that would do that
|
|
41
|
+
answers.targets = normalizeTargets(answers.targets);
|
|
42
|
+
return !answers.targets.includes(Tags.client);
|
|
43
|
+
},
|
|
37
44
|
message: 'Pick plugins:',
|
|
38
45
|
choices: getPluginChoices,
|
|
39
46
|
validate: ((plugins) => plugins.length > 0),
|
|
@@ -48,22 +55,24 @@ export function getQuestions(possibleTargets) {
|
|
|
48
55
|
{
|
|
49
56
|
type: 'confirm',
|
|
50
57
|
name: 'introspection',
|
|
58
|
+
default: false,
|
|
51
59
|
message: 'Do you want to generate an introspection file?',
|
|
52
60
|
},
|
|
53
61
|
{
|
|
54
62
|
type: 'input',
|
|
55
63
|
name: 'config',
|
|
56
64
|
message: 'How to name the config file?',
|
|
57
|
-
default: 'codegen.yml',
|
|
65
|
+
default: answers => (answers.targets.includes(Tags.client) ? 'codegen.ts' : 'codegen.yml'),
|
|
58
66
|
validate: (str) => {
|
|
59
67
|
const isNotEmpty = str.length > 0;
|
|
60
|
-
const hasCorrectExtension = ['json', 'yml', 'yaml'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
|
|
68
|
+
const hasCorrectExtension = ['json', 'yml', 'yaml', 'js', 'ts'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
|
|
61
69
|
return isNotEmpty && hasCorrectExtension;
|
|
62
70
|
},
|
|
63
71
|
},
|
|
64
72
|
{
|
|
65
73
|
type: 'input',
|
|
66
74
|
name: 'script',
|
|
75
|
+
default: 'codegen',
|
|
67
76
|
message: 'What script in package.json should run the codegen?',
|
|
68
77
|
validate: (str) => str.length > 0,
|
|
69
78
|
},
|
|
@@ -77,8 +86,9 @@ export function getApplicationTypeChoices(possibleTargets) {
|
|
|
77
86
|
else if (possibleTargets.Flow) {
|
|
78
87
|
tags.push(Tags.flow);
|
|
79
88
|
}
|
|
80
|
-
else {
|
|
81
|
-
tags.push(Tags.
|
|
89
|
+
else if (possibleTargets.Node) {
|
|
90
|
+
tags.push(Tags.typescript);
|
|
91
|
+
tags.push(Tags.flow);
|
|
82
92
|
}
|
|
83
93
|
return tags;
|
|
84
94
|
}
|
|
@@ -92,25 +102,37 @@ export function getApplicationTypeChoices(possibleTargets) {
|
|
|
92
102
|
{
|
|
93
103
|
name: 'Application built with Angular',
|
|
94
104
|
key: 'angular',
|
|
95
|
-
value: [Tags.angular, Tags.
|
|
105
|
+
value: [Tags.angular, Tags.client],
|
|
96
106
|
checked: possibleTargets.Angular,
|
|
97
107
|
},
|
|
98
108
|
{
|
|
99
109
|
name: 'Application built with React',
|
|
100
110
|
key: 'react',
|
|
101
|
-
value: withFlowOrTypescript([Tags.react, Tags.
|
|
111
|
+
value: withFlowOrTypescript([Tags.react, Tags.client]),
|
|
102
112
|
checked: possibleTargets.React,
|
|
103
113
|
},
|
|
104
114
|
{
|
|
105
115
|
name: 'Application built with Stencil',
|
|
106
116
|
key: 'stencil',
|
|
107
|
-
value: [Tags.stencil, Tags.
|
|
117
|
+
value: [Tags.stencil, Tags.typescript],
|
|
108
118
|
checked: possibleTargets.Stencil,
|
|
109
119
|
},
|
|
120
|
+
{
|
|
121
|
+
name: 'Application built with Vue',
|
|
122
|
+
key: 'vue',
|
|
123
|
+
value: [Tags.vue, Tags.client],
|
|
124
|
+
checked: possibleTargets.Vue,
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'Application using graphql-request',
|
|
128
|
+
key: 'graphqlRequest',
|
|
129
|
+
value: [Tags.graphqlRequest, Tags.client],
|
|
130
|
+
checked: possibleTargets.graphqlRequest,
|
|
131
|
+
},
|
|
110
132
|
{
|
|
111
133
|
name: 'Application built with other framework or vanilla JS',
|
|
112
134
|
key: 'client',
|
|
113
|
-
value: [Tags.
|
|
135
|
+
value: [Tags.typescript, Tags.flow],
|
|
114
136
|
checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil,
|
|
115
137
|
},
|
|
116
138
|
];
|
|
@@ -130,6 +152,9 @@ function normalizeTargets(targets) {
|
|
|
130
152
|
return [].concat(...targets);
|
|
131
153
|
}
|
|
132
154
|
export function getOutputDefaultValue(answers) {
|
|
155
|
+
if (answers.targets.includes(Tags.client)) {
|
|
156
|
+
return 'src/gql';
|
|
157
|
+
}
|
|
133
158
|
if (answers.plugins.some(plugin => plugin.defaultExtension === '.tsx')) {
|
|
134
159
|
return 'src/generated/graphql.tsx';
|
|
135
160
|
}
|
|
@@ -138,3 +163,17 @@ export function getOutputDefaultValue(answers) {
|
|
|
138
163
|
}
|
|
139
164
|
return 'src/generated/graphql.js';
|
|
140
165
|
}
|
|
166
|
+
export function getDocumentsDefaultValue(answers) {
|
|
167
|
+
if (answers.targets.includes(Tags.vue)) {
|
|
168
|
+
return 'src/**/*.vue';
|
|
169
|
+
}
|
|
170
|
+
else if (answers.targets.includes(Tags.angular)) {
|
|
171
|
+
return 'src/**/*.ts';
|
|
172
|
+
}
|
|
173
|
+
else if (answers.targets.includes(Tags.client)) {
|
|
174
|
+
return 'src/**/*.tsx';
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
return 'src/**/*.graphql';
|
|
178
|
+
}
|
|
179
|
+
}
|
package/esm/init/targets.js
CHANGED
|
@@ -14,10 +14,11 @@ export async function guessTargets() {
|
|
|
14
14
|
[Tags.react]: isReact(dependencies),
|
|
15
15
|
[Tags.stencil]: isStencil(dependencies),
|
|
16
16
|
[Tags.vue]: isVue(dependencies),
|
|
17
|
-
[Tags.
|
|
17
|
+
[Tags.client]: false,
|
|
18
18
|
[Tags.node]: false,
|
|
19
19
|
[Tags.typescript]: isTypescript(dependencies),
|
|
20
20
|
[Tags.flow]: isFlow(dependencies),
|
|
21
|
+
[Tags.graphqlRequest]: isGraphqlRequest(dependencies),
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
24
|
function isAngular(dependencies) {
|
|
@@ -38,3 +39,6 @@ function isTypescript(dependencies) {
|
|
|
38
39
|
function isFlow(dependencies) {
|
|
39
40
|
return dependencies.includes('flow');
|
|
40
41
|
}
|
|
42
|
+
function isGraphqlRequest(dependencies) {
|
|
43
|
+
return dependencies.includes('graphql-request');
|
|
44
|
+
}
|
package/esm/init/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export var Tags;
|
|
2
2
|
(function (Tags) {
|
|
3
|
-
Tags["
|
|
3
|
+
Tags["client"] = "Browser";
|
|
4
4
|
Tags["node"] = "Node";
|
|
5
5
|
Tags["typescript"] = "TypeScript";
|
|
6
6
|
Tags["flow"] = "Flow";
|
|
@@ -8,4 +8,5 @@ export var Tags;
|
|
|
8
8
|
Tags["stencil"] = "Stencil";
|
|
9
9
|
Tags["react"] = "React";
|
|
10
10
|
Tags["vue"] = "Vue";
|
|
11
|
+
Tags["graphqlRequest"] = "graphqlRequest";
|
|
11
12
|
})(Tags || (Tags = {}));
|
package/esm/presets.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { DetailedError } from '@graphql-codegen/plugin-helpers';
|
|
2
1
|
import { resolve } from 'path';
|
|
3
2
|
export async function getPresetByName(name, loader) {
|
|
4
3
|
const possibleNames = [
|
|
@@ -24,7 +23,8 @@ export async function getPresetByName(name, loader) {
|
|
|
24
23
|
err.code !== 'MODULE_NOT_FOUND' &&
|
|
25
24
|
/** ESM Error code */
|
|
26
25
|
err.code !== 'ERR_MODULE_NOT_FOUND') {
|
|
27
|
-
throw new
|
|
26
|
+
throw new Error(`Unable to load preset matching ${name}
|
|
27
|
+
|
|
28
28
|
Unable to load preset matching '${name}'.
|
|
29
29
|
Reason:
|
|
30
30
|
${err.message}
|
|
@@ -37,7 +37,8 @@ export async function getPresetByName(name, loader) {
|
|
|
37
37
|
- ${name}
|
|
38
38
|
`.trimRight())
|
|
39
39
|
.join('');
|
|
40
|
-
throw new
|
|
40
|
+
throw new Error(`Unable to find preset matching ${name}
|
|
41
|
+
|
|
41
42
|
Unable to find preset matching '${name}'
|
|
42
43
|
Install one of the following packages:
|
|
43
44
|
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/cli",
|
|
3
|
-
"version": "2.11.9-alpha-
|
|
3
|
+
"version": "2.11.9-alpha-20220926151609-5af00bb63",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
+
"@babel/generator": "^7.18.13",
|
|
9
|
+
"@babel/template": "^7.18.10",
|
|
10
|
+
"@babel/types": "^7.18.13",
|
|
11
|
+
"@graphql-codegen/client-preset": "1.0.1-alpha-20220926151609-5af00bb63",
|
|
8
12
|
"@graphql-codegen/core": "2.6.2",
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "
|
|
13
|
+
"@graphql-codegen/plugin-helpers": "2.8.0-alpha-20220926151609-5af00bb63",
|
|
10
14
|
"@graphql-tools/apollo-engine-loader": "^7.3.6",
|
|
11
15
|
"@graphql-tools/code-file-loader": "^7.3.1",
|
|
12
16
|
"@graphql-tools/git-loader": "^7.2.1",
|
|
@@ -22,6 +26,7 @@
|
|
|
22
26
|
"chalk": "^4.1.0",
|
|
23
27
|
"chokidar": "^3.5.2",
|
|
24
28
|
"cosmiconfig": "^7.0.0",
|
|
29
|
+
"cosmiconfig-typescript-loader": "4.0.0",
|
|
25
30
|
"debounce": "^1.2.0",
|
|
26
31
|
"detect-indent": "^6.0.0",
|
|
27
32
|
"graphql-config": "^4.3.5",
|
package/typings/config.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Types, Profiler } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { GraphQLConfig } from 'graphql-config';
|
|
3
3
|
import { GraphQLSchema } from 'graphql';
|
|
4
|
+
export declare type CodegenConfig = Types.Config;
|
|
4
5
|
export declare type YamlCliFlags = {
|
|
5
6
|
config: string;
|
|
6
7
|
watch: boolean | string | string[];
|
package/typings/config.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Types, Profiler } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { GraphQLConfig } from 'graphql-config';
|
|
3
3
|
import { GraphQLSchema } from 'graphql';
|
|
4
|
+
export declare type CodegenConfig = Types.Config;
|
|
4
5
|
export declare type YamlCliFlags = {
|
|
5
6
|
config: string;
|
|
6
7
|
watch: boolean | string | string[];
|
package/typings/index.d.cts
CHANGED
package/typings/index.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export declare function getApplicationTypeChoices(possibleTargets: Record<Tags,
|
|
|
8
8
|
checked: boolean;
|
|
9
9
|
}[];
|
|
10
10
|
export declare function getPluginChoices(answers: Answers): inquirer.DistinctChoice<inquirer.AllChoiceMap<inquirer.Answers>, inquirer.AllChoiceMap<inquirer.AllChoiceMap<inquirer.Answers>>>[];
|
|
11
|
-
export declare function getOutputDefaultValue(answers: Answers): "src/generated/graphql.tsx" | "src/generated/graphql.ts" | "src/generated/graphql.cjs";
|
|
11
|
+
export declare function getOutputDefaultValue(answers: Answers): "src/gql" | "src/generated/graphql.tsx" | "src/generated/graphql.ts" | "src/generated/graphql.cjs";
|
|
12
|
+
export declare function getDocumentsDefaultValue(answers: Answers): "src/**/*.vue" | "src/**/*.ts" | "src/**/*.tsx" | "src/**/*.graphql";
|
|
@@ -8,4 +8,5 @@ export declare function getApplicationTypeChoices(possibleTargets: Record<Tags,
|
|
|
8
8
|
checked: boolean;
|
|
9
9
|
}[];
|
|
10
10
|
export declare function getPluginChoices(answers: Answers): inquirer.DistinctChoice<inquirer.AllChoiceMap<inquirer.Answers>, inquirer.AllChoiceMap<inquirer.AllChoiceMap<inquirer.Answers>>>[];
|
|
11
|
-
export declare function getOutputDefaultValue(answers: Answers): "src/generated/graphql.tsx" | "src/generated/graphql.ts" | "src/generated/graphql.js";
|
|
11
|
+
export declare function getOutputDefaultValue(answers: Answers): "src/gql" | "src/generated/graphql.tsx" | "src/generated/graphql.ts" | "src/generated/graphql.js";
|
|
12
|
+
export declare function getDocumentsDefaultValue(answers: Answers): "src/**/*.vue" | "src/**/*.ts" | "src/**/*.tsx" | "src/**/*.graphql";
|
package/typings/init/types.d.cts
CHANGED
|
@@ -10,7 +10,7 @@ export interface PluginOption {
|
|
|
10
10
|
export interface Answers {
|
|
11
11
|
targets: Tags[];
|
|
12
12
|
config: string;
|
|
13
|
-
plugins
|
|
13
|
+
plugins?: PluginOption[];
|
|
14
14
|
schema: string;
|
|
15
15
|
documents?: string;
|
|
16
16
|
output: string;
|
|
@@ -18,12 +18,13 @@ export interface Answers {
|
|
|
18
18
|
introspection: boolean;
|
|
19
19
|
}
|
|
20
20
|
export declare enum Tags {
|
|
21
|
-
|
|
21
|
+
client = "Browser",
|
|
22
22
|
node = "Node",
|
|
23
23
|
typescript = "TypeScript",
|
|
24
24
|
flow = "Flow",
|
|
25
25
|
angular = "Angular",
|
|
26
26
|
stencil = "Stencil",
|
|
27
27
|
react = "React",
|
|
28
|
-
vue = "Vue"
|
|
28
|
+
vue = "Vue",
|
|
29
|
+
graphqlRequest = "graphqlRequest"
|
|
29
30
|
}
|
package/typings/init/types.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export interface PluginOption {
|
|
|
10
10
|
export interface Answers {
|
|
11
11
|
targets: Tags[];
|
|
12
12
|
config: string;
|
|
13
|
-
plugins
|
|
13
|
+
plugins?: PluginOption[];
|
|
14
14
|
schema: string;
|
|
15
15
|
documents?: string;
|
|
16
16
|
output: string;
|
|
@@ -18,12 +18,13 @@ export interface Answers {
|
|
|
18
18
|
introspection: boolean;
|
|
19
19
|
}
|
|
20
20
|
export declare enum Tags {
|
|
21
|
-
|
|
21
|
+
client = "Browser",
|
|
22
22
|
node = "Node",
|
|
23
23
|
typescript = "TypeScript",
|
|
24
24
|
flow = "Flow",
|
|
25
25
|
angular = "Angular",
|
|
26
26
|
stencil = "Stencil",
|
|
27
27
|
react = "React",
|
|
28
|
-
vue = "Vue"
|
|
28
|
+
vue = "Vue",
|
|
29
|
+
graphqlRequest = "graphqlRequest"
|
|
29
30
|
}
|