@graphql-codegen/typescript-oclif 2.2.14 → 2.2.15-alpha-5d16266dd.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +35 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +55 -0
- package/cjs/visitor.js +86 -0
- package/esm/index.js +30 -0
- package/esm/utils.js +50 -0
- package/{index.mjs → esm/visitor.js} +4 -85
- package/package.json +21 -14
- package/{index.d.ts → typings/index.d.ts} +1 -1
- package/{utils.d.ts → typings/utils.d.ts} +0 -0
- package/{visitor.d.ts → typings/visitor.d.ts} +1 -1
- package/index.js +0 -170
package/cjs/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphQLRequestVisitor = exports.validate = exports.plugin = void 0;
|
|
4
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const visitor_js_1 = require("./visitor.js");
|
|
7
|
+
Object.defineProperty(exports, "GraphQLRequestVisitor", { enumerable: true, get: function () { return visitor_js_1.GraphQLRequestVisitor; } });
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const plugin = (schema, documents, config, info) => {
|
|
10
|
+
const allAst = (0, graphql_1.concatAST)(documents.reduce((prev, v) => {
|
|
11
|
+
return [...prev, v.document];
|
|
12
|
+
}, []));
|
|
13
|
+
const allFragments = [
|
|
14
|
+
...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
15
|
+
node: fragmentDef,
|
|
16
|
+
name: fragmentDef.name.value,
|
|
17
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
18
|
+
isExternal: false,
|
|
19
|
+
})),
|
|
20
|
+
...(config.externalFragments || []),
|
|
21
|
+
];
|
|
22
|
+
const visitor = new visitor_js_1.GraphQLRequestVisitor(schema, allFragments, config, info);
|
|
23
|
+
(0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
|
|
24
|
+
return {
|
|
25
|
+
prepend: visitor.getImports(),
|
|
26
|
+
content: visitor.cliContent,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
exports.plugin = plugin;
|
|
30
|
+
const validate = async (schema, documents, config, outputFile) => {
|
|
31
|
+
if ((0, path_1.extname)(outputFile) !== '.ts') {
|
|
32
|
+
throw new Error(`Plugin "typescript-oclif" requires output file extensions to be ".ts"!`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.validate = validate;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.omitOclifDirectives = exports.getFlagConfigForVariableDefinition = void 0;
|
|
4
|
+
const getFlagConfigForVariableDefinition = (definition) => {
|
|
5
|
+
const { list, required, innerType } = getInnerType(definition.type);
|
|
6
|
+
const oclifType = mapVariableTypeToOclifType(innerType);
|
|
7
|
+
const parser = getParserForType(innerType);
|
|
8
|
+
return `${definition.variable.name.value}: flags.${oclifType}({
|
|
9
|
+
multiple: ${list},
|
|
10
|
+
required: ${required},${parser ? `\n parse: ${parser}` : ''}
|
|
11
|
+
})`;
|
|
12
|
+
};
|
|
13
|
+
exports.getFlagConfigForVariableDefinition = getFlagConfigForVariableDefinition;
|
|
14
|
+
// Supply a custom parser for oclif flag configuration
|
|
15
|
+
const getParserForType = (type) => {
|
|
16
|
+
if (type.name.value === 'Float') {
|
|
17
|
+
return 'input => Number(input)';
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const mapVariableTypeToOclifType = (type) => {
|
|
21
|
+
if (type.name.value === 'Boolean') {
|
|
22
|
+
return 'boolean';
|
|
23
|
+
}
|
|
24
|
+
if (['Float', 'Int'].includes(type.name.value)) {
|
|
25
|
+
// A quirk of oclif is that "integer" allows for any `number`-typed response, and then
|
|
26
|
+
// we supply our own parsing function to make sure it's a float and not an integer
|
|
27
|
+
return 'integer';
|
|
28
|
+
}
|
|
29
|
+
return 'string';
|
|
30
|
+
};
|
|
31
|
+
// Retrieve the inner type if nested within List and/or NonNull
|
|
32
|
+
const getInnerType = (type) => {
|
|
33
|
+
const result = {
|
|
34
|
+
list: false,
|
|
35
|
+
required: false,
|
|
36
|
+
};
|
|
37
|
+
let _type = type;
|
|
38
|
+
while (_type.kind !== 'NamedType') {
|
|
39
|
+
if (_type.kind === 'ListType') {
|
|
40
|
+
result.list = true;
|
|
41
|
+
}
|
|
42
|
+
else if (_type.kind === 'NonNullType') {
|
|
43
|
+
result.required = true;
|
|
44
|
+
}
|
|
45
|
+
_type = _type.type;
|
|
46
|
+
}
|
|
47
|
+
result.innerType = _type;
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
// remove all @oclif directives from the document for transmission to the server
|
|
51
|
+
const omitOclifDirectives = (node) => {
|
|
52
|
+
const directives = node.directives.filter(directive => directive.name.value !== 'oclif');
|
|
53
|
+
return Object.assign({}, node, { directives });
|
|
54
|
+
};
|
|
55
|
+
exports.omitOclifDirectives = omitOclifDirectives;
|
package/cjs/visitor.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphQLRequestVisitor = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
6
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
7
|
+
const graphql_1 = require("graphql");
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
9
|
+
class GraphQLRequestVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
10
|
+
constructor(schema, fragments, rawConfig, info) {
|
|
11
|
+
super(schema, fragments, rawConfig, {});
|
|
12
|
+
this._operationsToInclude = [];
|
|
13
|
+
this._info = info;
|
|
14
|
+
const { handlerPath = '../../handler' } = rawConfig;
|
|
15
|
+
// FIXME: This is taken in part from
|
|
16
|
+
// presets/near-operation-file/src/index.ts:139. How do I build a path relative to the outputFile in the same way?
|
|
17
|
+
// A plugin doesn't appear to have access to the same "options.baseOutputDir" that the preset does.
|
|
18
|
+
// const absClientPath = resolve(info.outputFile, join(options.baseOutputDir, options.presetConfig.baseTypesPath));
|
|
19
|
+
(0, auto_bind_1.default)(this);
|
|
20
|
+
this._additionalImports.push(`import { Command, flags } from '@oclif/command'`);
|
|
21
|
+
this._additionalImports.push(`import handler from '${handlerPath}'`);
|
|
22
|
+
}
|
|
23
|
+
buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {
|
|
24
|
+
this._operationsToInclude.push({
|
|
25
|
+
node,
|
|
26
|
+
documentVariableName,
|
|
27
|
+
operationType,
|
|
28
|
+
operationResultType,
|
|
29
|
+
operationVariablesTypes,
|
|
30
|
+
});
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
// Clean client-side content (ie directives) out of the GraphQL document prior to sending to the server
|
|
34
|
+
get definition() {
|
|
35
|
+
const operation = this._operationsToInclude[0];
|
|
36
|
+
const clientOperation = (0, graphql_1.print)((0, utils_js_1.omitOclifDirectives)(operation.node));
|
|
37
|
+
return `const ${operation.documentVariableName} = \`\n${clientOperation}\``;
|
|
38
|
+
}
|
|
39
|
+
// Generate the code required for this CLI operation
|
|
40
|
+
get cliContent() {
|
|
41
|
+
if (this._operationsToInclude.length !== 1) {
|
|
42
|
+
throw new Error(`Each graphql document should have exactly one operation; found ${this._operationsToInclude.length} while generating ${this._info.outputFile}.`);
|
|
43
|
+
}
|
|
44
|
+
const operation = this._operationsToInclude[0];
|
|
45
|
+
// Find the @oclif directive in the client document, if it's there
|
|
46
|
+
const directive = operation.node.directives.find(directive => directive.name.value === 'oclif');
|
|
47
|
+
// Remap the directive's fields ie @oclif(description: "a name") to a more usable format
|
|
48
|
+
const directiveValues = {};
|
|
49
|
+
if (directive) {
|
|
50
|
+
directiveValues.examples = [];
|
|
51
|
+
directive.arguments.forEach(arg => {
|
|
52
|
+
const value = 'value' in arg.value ? arg.value.value.toString() : null;
|
|
53
|
+
const { value: name } = arg.name;
|
|
54
|
+
if (name === 'description') {
|
|
55
|
+
directiveValues.description = value;
|
|
56
|
+
}
|
|
57
|
+
else if (name === 'example') {
|
|
58
|
+
directiveValues.examples.push(value);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw new Error(`Invalid field supplied to @oclif directive: ${name}`);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const { description, examples } = directiveValues;
|
|
66
|
+
const flags = operation.node.variableDefinitions.map(utils_js_1.getFlagConfigForVariableDefinition);
|
|
67
|
+
return `
|
|
68
|
+
${this.definition}
|
|
69
|
+
|
|
70
|
+
export default class ${operation.node.name.value} extends Command {
|
|
71
|
+
${description ? `\nstatic description = "${description}";\n` : ''}
|
|
72
|
+
${examples ? `\nstatic examples: string[] = ${JSON.stringify(examples)};\n` : ''}
|
|
73
|
+
static flags = {
|
|
74
|
+
help: flags.help({ char: 'h' }),
|
|
75
|
+
${(0, visitor_plugin_common_1.indentMultiline)(flags.join(',\n'), 2)}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
async run() {
|
|
79
|
+
const { flags } = this.parse(${operation.node.name.value});
|
|
80
|
+
await handler({ command: this, query: ${operation.documentVariableName}, variables: flags });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.GraphQLRequestVisitor = GraphQLRequestVisitor;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { concatAST, Kind } from 'graphql';
|
|
3
|
+
import { GraphQLRequestVisitor } from './visitor.js';
|
|
4
|
+
import { extname } from 'path';
|
|
5
|
+
export const plugin = (schema, documents, config, info) => {
|
|
6
|
+
const allAst = concatAST(documents.reduce((prev, v) => {
|
|
7
|
+
return [...prev, v.document];
|
|
8
|
+
}, []));
|
|
9
|
+
const allFragments = [
|
|
10
|
+
...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
11
|
+
node: fragmentDef,
|
|
12
|
+
name: fragmentDef.name.value,
|
|
13
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
14
|
+
isExternal: false,
|
|
15
|
+
})),
|
|
16
|
+
...(config.externalFragments || []),
|
|
17
|
+
];
|
|
18
|
+
const visitor = new GraphQLRequestVisitor(schema, allFragments, config, info);
|
|
19
|
+
oldVisit(allAst, { leave: visitor });
|
|
20
|
+
return {
|
|
21
|
+
prepend: visitor.getImports(),
|
|
22
|
+
content: visitor.cliContent,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export const validate = async (schema, documents, config, outputFile) => {
|
|
26
|
+
if (extname(outputFile) !== '.ts') {
|
|
27
|
+
throw new Error(`Plugin "typescript-oclif" requires output file extensions to be ".ts"!`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export { GraphQLRequestVisitor };
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export const getFlagConfigForVariableDefinition = (definition) => {
|
|
2
|
+
const { list, required, innerType } = getInnerType(definition.type);
|
|
3
|
+
const oclifType = mapVariableTypeToOclifType(innerType);
|
|
4
|
+
const parser = getParserForType(innerType);
|
|
5
|
+
return `${definition.variable.name.value}: flags.${oclifType}({
|
|
6
|
+
multiple: ${list},
|
|
7
|
+
required: ${required},${parser ? `\n parse: ${parser}` : ''}
|
|
8
|
+
})`;
|
|
9
|
+
};
|
|
10
|
+
// Supply a custom parser for oclif flag configuration
|
|
11
|
+
const getParserForType = (type) => {
|
|
12
|
+
if (type.name.value === 'Float') {
|
|
13
|
+
return 'input => Number(input)';
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const mapVariableTypeToOclifType = (type) => {
|
|
17
|
+
if (type.name.value === 'Boolean') {
|
|
18
|
+
return 'boolean';
|
|
19
|
+
}
|
|
20
|
+
if (['Float', 'Int'].includes(type.name.value)) {
|
|
21
|
+
// A quirk of oclif is that "integer" allows for any `number`-typed response, and then
|
|
22
|
+
// we supply our own parsing function to make sure it's a float and not an integer
|
|
23
|
+
return 'integer';
|
|
24
|
+
}
|
|
25
|
+
return 'string';
|
|
26
|
+
};
|
|
27
|
+
// Retrieve the inner type if nested within List and/or NonNull
|
|
28
|
+
const getInnerType = (type) => {
|
|
29
|
+
const result = {
|
|
30
|
+
list: false,
|
|
31
|
+
required: false,
|
|
32
|
+
};
|
|
33
|
+
let _type = type;
|
|
34
|
+
while (_type.kind !== 'NamedType') {
|
|
35
|
+
if (_type.kind === 'ListType') {
|
|
36
|
+
result.list = true;
|
|
37
|
+
}
|
|
38
|
+
else if (_type.kind === 'NonNullType') {
|
|
39
|
+
result.required = true;
|
|
40
|
+
}
|
|
41
|
+
_type = _type.type;
|
|
42
|
+
}
|
|
43
|
+
result.innerType = _type;
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
// remove all @oclif directives from the document for transmission to the server
|
|
47
|
+
export const omitOclifDirectives = (node) => {
|
|
48
|
+
const directives = node.directives.filter(directive => directive.name.value !== 'oclif');
|
|
49
|
+
return Object.assign({}, node, { directives });
|
|
50
|
+
};
|
|
@@ -1,61 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { print, concatAST, Kind } from 'graphql';
|
|
3
|
-
import { ClientSideBaseVisitor, indentMultiline } from '@graphql-codegen/visitor-plugin-common';
|
|
1
|
+
import { ClientSideBaseVisitor, indentMultiline, } from '@graphql-codegen/visitor-plugin-common';
|
|
4
2
|
import autoBind from 'auto-bind';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const { list, required, innerType } = getInnerType(definition.type);
|
|
9
|
-
const oclifType = mapVariableTypeToOclifType(innerType);
|
|
10
|
-
const parser = getParserForType(innerType);
|
|
11
|
-
return `${definition.variable.name.value}: flags.${oclifType}({
|
|
12
|
-
multiple: ${list},
|
|
13
|
-
required: ${required},${parser ? `\n parse: ${parser}` : ''}
|
|
14
|
-
})`;
|
|
15
|
-
};
|
|
16
|
-
// Supply a custom parser for oclif flag configuration
|
|
17
|
-
const getParserForType = (type) => {
|
|
18
|
-
if (type.name.value === 'Float') {
|
|
19
|
-
return 'input => Number(input)';
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
const mapVariableTypeToOclifType = (type) => {
|
|
23
|
-
if (type.name.value === 'Boolean') {
|
|
24
|
-
return 'boolean';
|
|
25
|
-
}
|
|
26
|
-
if (['Float', 'Int'].includes(type.name.value)) {
|
|
27
|
-
// A quirk of oclif is that "integer" allows for any `number`-typed response, and then
|
|
28
|
-
// we supply our own parsing function to make sure it's a float and not an integer
|
|
29
|
-
return 'integer';
|
|
30
|
-
}
|
|
31
|
-
return 'string';
|
|
32
|
-
};
|
|
33
|
-
// Retrieve the inner type if nested within List and/or NonNull
|
|
34
|
-
const getInnerType = (type) => {
|
|
35
|
-
const result = {
|
|
36
|
-
list: false,
|
|
37
|
-
required: false,
|
|
38
|
-
};
|
|
39
|
-
let _type = type;
|
|
40
|
-
while (_type.kind !== 'NamedType') {
|
|
41
|
-
if (_type.kind === 'ListType') {
|
|
42
|
-
result.list = true;
|
|
43
|
-
}
|
|
44
|
-
else if (_type.kind === 'NonNullType') {
|
|
45
|
-
result.required = true;
|
|
46
|
-
}
|
|
47
|
-
_type = _type.type;
|
|
48
|
-
}
|
|
49
|
-
result.innerType = _type;
|
|
50
|
-
return result;
|
|
51
|
-
};
|
|
52
|
-
// remove all @oclif directives from the document for transmission to the server
|
|
53
|
-
const omitOclifDirectives = (node) => {
|
|
54
|
-
const directives = node.directives.filter(directive => directive.name.value !== 'oclif');
|
|
55
|
-
return Object.assign({}, node, { directives });
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
class GraphQLRequestVisitor extends ClientSideBaseVisitor {
|
|
3
|
+
import { print } from 'graphql';
|
|
4
|
+
import { getFlagConfigForVariableDefinition, omitOclifDirectives } from './utils.js';
|
|
5
|
+
export class GraphQLRequestVisitor extends ClientSideBaseVisitor {
|
|
59
6
|
constructor(schema, fragments, rawConfig, info) {
|
|
60
7
|
super(schema, fragments, rawConfig, {});
|
|
61
8
|
this._operationsToInclude = [];
|
|
@@ -132,31 +79,3 @@ ${indentMultiline(flags.join(',\n'), 2)}
|
|
|
132
79
|
`;
|
|
133
80
|
}
|
|
134
81
|
}
|
|
135
|
-
|
|
136
|
-
const plugin = (schema, documents, config, info) => {
|
|
137
|
-
const allAst = concatAST(documents.reduce((prev, v) => {
|
|
138
|
-
return [...prev, v.document];
|
|
139
|
-
}, []));
|
|
140
|
-
const allFragments = [
|
|
141
|
-
...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
142
|
-
node: fragmentDef,
|
|
143
|
-
name: fragmentDef.name.value,
|
|
144
|
-
onType: fragmentDef.typeCondition.name.value,
|
|
145
|
-
isExternal: false,
|
|
146
|
-
})),
|
|
147
|
-
...(config.externalFragments || []),
|
|
148
|
-
];
|
|
149
|
-
const visitor = new GraphQLRequestVisitor(schema, allFragments, config, info);
|
|
150
|
-
oldVisit(allAst, { leave: visitor });
|
|
151
|
-
return {
|
|
152
|
-
prepend: visitor.getImports(),
|
|
153
|
-
content: visitor.cliContent,
|
|
154
|
-
};
|
|
155
|
-
};
|
|
156
|
-
const validate = async (schema, documents, config, outputFile) => {
|
|
157
|
-
if (extname(outputFile) !== '.ts') {
|
|
158
|
-
throw new Error(`Plugin "typescript-oclif" requires output file extensions to be ".ts"!`);
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
export { GraphQLRequestVisitor, plugin, validate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-oclif",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.15-alpha-5d16266dd.0",
|
|
4
4
|
"description": "GraphQL Code Generator plugin for generating a CLI tool with oclif",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"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",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@graphql-codegen/plugin-helpers": "^2.4.0",
|
|
11
|
-
"@graphql-codegen/visitor-plugin-common": "2.
|
|
11
|
+
"@graphql-codegen/visitor-plugin-common": "2.11.0-alpha-5d16266dd.0",
|
|
12
12
|
"auto-bind": "~4.0.0",
|
|
13
13
|
"tslib": "~2.4.0"
|
|
14
14
|
},
|
|
@@ -19,21 +19,28 @@
|
|
|
19
19
|
},
|
|
20
20
|
"author": "Kalan Snyder <hello@kalan.io>",
|
|
21
21
|
"license": "MIT",
|
|
22
|
-
"main": "index.js",
|
|
23
|
-
"module": "index.
|
|
24
|
-
"typings": "index.d.ts",
|
|
22
|
+
"main": "cjs/index.js",
|
|
23
|
+
"module": "esm/index.js",
|
|
24
|
+
"typings": "typings/index.d.ts",
|
|
25
25
|
"typescript": {
|
|
26
|
-
"definition": "index.d.ts"
|
|
26
|
+
"definition": "typings/index.d.ts"
|
|
27
27
|
},
|
|
28
|
+
"type": "module",
|
|
28
29
|
"exports": {
|
|
29
|
-
"./package.json": "./package.json",
|
|
30
30
|
".": {
|
|
31
|
-
"require":
|
|
32
|
-
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./typings/index.d.ts",
|
|
33
|
+
"default": "./cjs/index.js"
|
|
34
|
+
},
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./typings/index.d.ts",
|
|
37
|
+
"default": "./esm/index.js"
|
|
38
|
+
},
|
|
39
|
+
"default": {
|
|
40
|
+
"types": "./typings/index.d.ts",
|
|
41
|
+
"default": "./esm/index.js"
|
|
42
|
+
}
|
|
33
43
|
},
|
|
34
|
-
"
|
|
35
|
-
"require": "./*.js",
|
|
36
|
-
"import": "./*.mjs"
|
|
37
|
-
}
|
|
44
|
+
"./package.json": "./package.json"
|
|
38
45
|
}
|
|
39
|
-
}
|
|
46
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common';
|
|
3
|
-
import { GraphQLRequestVisitor } from './visitor';
|
|
3
|
+
import { GraphQLRequestVisitor } from './visitor.js';
|
|
4
4
|
export interface Config extends RawClientSideBasePluginConfig {
|
|
5
5
|
handlerPath?: string;
|
|
6
6
|
}
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ClientSideBaseVisitor, ClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
|
|
2
2
|
import { GraphQLSchema, OperationDefinitionNode } from 'graphql';
|
|
3
|
-
import { Config, Info } from '.';
|
|
3
|
+
import { Config, Info } from './index.js';
|
|
4
4
|
export declare class GraphQLRequestVisitor extends ClientSideBaseVisitor<Config, ClientSideBasePluginConfig> {
|
|
5
5
|
private _operationsToInclude;
|
|
6
6
|
private _info;
|
package/index.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
const pluginHelpers = require('@graphql-codegen/plugin-helpers');
|
|
8
|
-
const graphql = require('graphql');
|
|
9
|
-
const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
|
|
10
|
-
const autoBind = _interopDefault(require('auto-bind'));
|
|
11
|
-
const path = require('path');
|
|
12
|
-
|
|
13
|
-
const getFlagConfigForVariableDefinition = (definition) => {
|
|
14
|
-
const { list, required, innerType } = getInnerType(definition.type);
|
|
15
|
-
const oclifType = mapVariableTypeToOclifType(innerType);
|
|
16
|
-
const parser = getParserForType(innerType);
|
|
17
|
-
return `${definition.variable.name.value}: flags.${oclifType}({
|
|
18
|
-
multiple: ${list},
|
|
19
|
-
required: ${required},${parser ? `\n parse: ${parser}` : ''}
|
|
20
|
-
})`;
|
|
21
|
-
};
|
|
22
|
-
// Supply a custom parser for oclif flag configuration
|
|
23
|
-
const getParserForType = (type) => {
|
|
24
|
-
if (type.name.value === 'Float') {
|
|
25
|
-
return 'input => Number(input)';
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
const mapVariableTypeToOclifType = (type) => {
|
|
29
|
-
if (type.name.value === 'Boolean') {
|
|
30
|
-
return 'boolean';
|
|
31
|
-
}
|
|
32
|
-
if (['Float', 'Int'].includes(type.name.value)) {
|
|
33
|
-
// A quirk of oclif is that "integer" allows for any `number`-typed response, and then
|
|
34
|
-
// we supply our own parsing function to make sure it's a float and not an integer
|
|
35
|
-
return 'integer';
|
|
36
|
-
}
|
|
37
|
-
return 'string';
|
|
38
|
-
};
|
|
39
|
-
// Retrieve the inner type if nested within List and/or NonNull
|
|
40
|
-
const getInnerType = (type) => {
|
|
41
|
-
const result = {
|
|
42
|
-
list: false,
|
|
43
|
-
required: false,
|
|
44
|
-
};
|
|
45
|
-
let _type = type;
|
|
46
|
-
while (_type.kind !== 'NamedType') {
|
|
47
|
-
if (_type.kind === 'ListType') {
|
|
48
|
-
result.list = true;
|
|
49
|
-
}
|
|
50
|
-
else if (_type.kind === 'NonNullType') {
|
|
51
|
-
result.required = true;
|
|
52
|
-
}
|
|
53
|
-
_type = _type.type;
|
|
54
|
-
}
|
|
55
|
-
result.innerType = _type;
|
|
56
|
-
return result;
|
|
57
|
-
};
|
|
58
|
-
// remove all @oclif directives from the document for transmission to the server
|
|
59
|
-
const omitOclifDirectives = (node) => {
|
|
60
|
-
const directives = node.directives.filter(directive => directive.name.value !== 'oclif');
|
|
61
|
-
return Object.assign({}, node, { directives });
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
class GraphQLRequestVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
65
|
-
constructor(schema, fragments, rawConfig, info) {
|
|
66
|
-
super(schema, fragments, rawConfig, {});
|
|
67
|
-
this._operationsToInclude = [];
|
|
68
|
-
this._info = info;
|
|
69
|
-
const { handlerPath = '../../handler' } = rawConfig;
|
|
70
|
-
// FIXME: This is taken in part from
|
|
71
|
-
// presets/near-operation-file/src/index.ts:139. How do I build a path relative to the outputFile in the same way?
|
|
72
|
-
// A plugin doesn't appear to have access to the same "options.baseOutputDir" that the preset does.
|
|
73
|
-
// const absClientPath = resolve(info.outputFile, join(options.baseOutputDir, options.presetConfig.baseTypesPath));
|
|
74
|
-
autoBind(this);
|
|
75
|
-
this._additionalImports.push(`import { Command, flags } from '@oclif/command'`);
|
|
76
|
-
this._additionalImports.push(`import handler from '${handlerPath}'`);
|
|
77
|
-
}
|
|
78
|
-
buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {
|
|
79
|
-
this._operationsToInclude.push({
|
|
80
|
-
node,
|
|
81
|
-
documentVariableName,
|
|
82
|
-
operationType,
|
|
83
|
-
operationResultType,
|
|
84
|
-
operationVariablesTypes,
|
|
85
|
-
});
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
// Clean client-side content (ie directives) out of the GraphQL document prior to sending to the server
|
|
89
|
-
get definition() {
|
|
90
|
-
const operation = this._operationsToInclude[0];
|
|
91
|
-
const clientOperation = graphql.print(omitOclifDirectives(operation.node));
|
|
92
|
-
return `const ${operation.documentVariableName} = \`\n${clientOperation}\``;
|
|
93
|
-
}
|
|
94
|
-
// Generate the code required for this CLI operation
|
|
95
|
-
get cliContent() {
|
|
96
|
-
if (this._operationsToInclude.length !== 1) {
|
|
97
|
-
throw new Error(`Each graphql document should have exactly one operation; found ${this._operationsToInclude.length} while generating ${this._info.outputFile}.`);
|
|
98
|
-
}
|
|
99
|
-
const operation = this._operationsToInclude[0];
|
|
100
|
-
// Find the @oclif directive in the client document, if it's there
|
|
101
|
-
const directive = operation.node.directives.find(directive => directive.name.value === 'oclif');
|
|
102
|
-
// Remap the directive's fields ie @oclif(description: "a name") to a more usable format
|
|
103
|
-
const directiveValues = {};
|
|
104
|
-
if (directive) {
|
|
105
|
-
directiveValues.examples = [];
|
|
106
|
-
directive.arguments.forEach(arg => {
|
|
107
|
-
const value = 'value' in arg.value ? arg.value.value.toString() : null;
|
|
108
|
-
const { value: name } = arg.name;
|
|
109
|
-
if (name === 'description') {
|
|
110
|
-
directiveValues.description = value;
|
|
111
|
-
}
|
|
112
|
-
else if (name === 'example') {
|
|
113
|
-
directiveValues.examples.push(value);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
throw new Error(`Invalid field supplied to @oclif directive: ${name}`);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
const { description, examples } = directiveValues;
|
|
121
|
-
const flags = operation.node.variableDefinitions.map(getFlagConfigForVariableDefinition);
|
|
122
|
-
return `
|
|
123
|
-
${this.definition}
|
|
124
|
-
|
|
125
|
-
export default class ${operation.node.name.value} extends Command {
|
|
126
|
-
${description ? `\nstatic description = "${description}";\n` : ''}
|
|
127
|
-
${examples ? `\nstatic examples: string[] = ${JSON.stringify(examples)};\n` : ''}
|
|
128
|
-
static flags = {
|
|
129
|
-
help: flags.help({ char: 'h' }),
|
|
130
|
-
${visitorPluginCommon.indentMultiline(flags.join(',\n'), 2)}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
async run() {
|
|
134
|
-
const { flags } = this.parse(${operation.node.name.value});
|
|
135
|
-
await handler({ command: this, query: ${operation.documentVariableName}, variables: flags });
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
`;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const plugin = (schema, documents, config, info) => {
|
|
143
|
-
const allAst = graphql.concatAST(documents.reduce((prev, v) => {
|
|
144
|
-
return [...prev, v.document];
|
|
145
|
-
}, []));
|
|
146
|
-
const allFragments = [
|
|
147
|
-
...allAst.definitions.filter(d => d.kind === graphql.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
148
|
-
node: fragmentDef,
|
|
149
|
-
name: fragmentDef.name.value,
|
|
150
|
-
onType: fragmentDef.typeCondition.name.value,
|
|
151
|
-
isExternal: false,
|
|
152
|
-
})),
|
|
153
|
-
...(config.externalFragments || []),
|
|
154
|
-
];
|
|
155
|
-
const visitor = new GraphQLRequestVisitor(schema, allFragments, config, info);
|
|
156
|
-
pluginHelpers.oldVisit(allAst, { leave: visitor });
|
|
157
|
-
return {
|
|
158
|
-
prepend: visitor.getImports(),
|
|
159
|
-
content: visitor.cliContent,
|
|
160
|
-
};
|
|
161
|
-
};
|
|
162
|
-
const validate = async (schema, documents, config, outputFile) => {
|
|
163
|
-
if (path.extname(outputFile) !== '.ts') {
|
|
164
|
-
throw new Error(`Plugin "typescript-oclif" requires output file extensions to be ".ts"!`);
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
exports.GraphQLRequestVisitor = GraphQLRequestVisitor;
|
|
169
|
-
exports.plugin = plugin;
|
|
170
|
-
exports.validate = validate;
|