@devticon-os/graphql-codegen-axios 0.3.0-test → 0.3.1
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/lib/_types.js +3 -0
- package/lib/_types.js.map +1 -0
- package/lib/enums.js +138 -0
- package/lib/enums.js.map +1 -0
- package/lib/fragments.js +49 -0
- package/lib/fragments.js.map +1 -0
- package/lib/functions.js +71 -0
- package/lib/functions.js.map +1 -0
- package/lib/graphql.js +208 -0
- package/lib/graphql.js.map +1 -0
- package/lib/helpers.js +69 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.js +86 -0
- package/lib/index.js.map +1 -0
- package/lib/input.js +142 -0
- package/lib/input.js.map +1 -0
- package/lib/operation.js +315 -0
- package/lib/operation.js.map +1 -0
- package/lib/prettier.js +17 -0
- package/lib/prettier.js.map +1 -0
- package/lib/print.js +231 -0
- package/lib/print.js.map +1 -0
- package/lib/query.js +66 -0
- package/lib/query.js.map +1 -0
- package/lib/render.js +186 -0
- package/lib/render.js.map +1 -0
- package/lib/results.js +64 -0
- package/lib/results.js.map +1 -0
- package/lib/scalar.js +9 -0
- package/lib/scalar.js.map +1 -0
- package/lib/types.js +81 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.js +6 -0
- package/lib/utils.js.map +1 -0
- package/lib/variables.js +44 -0
- package/lib/variables.js.map +1 -0
- package/package.json +4 -3
- package/templates/helpers.ts +95 -0
- package/src/_types.ts +0 -60
- package/src/enums.ts +0 -50
- package/src/fragments.ts +0 -15
- package/src/graphql.ts +0 -101
- package/src/index.ts +0 -70
- package/src/input.ts +0 -65
- package/src/operation.ts +0 -212
- package/src/prettier.ts +0 -13
- package/src/print.ts +0 -139
- package/src/scalar.ts +0 -5
- package/src/utils.ts +0 -1
package/src/operation.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CodegenDocuments,
|
|
3
|
-
Config,
|
|
4
|
-
Directive,
|
|
5
|
-
FieldsMap,
|
|
6
|
-
NamedTsType,
|
|
7
|
-
Operation,
|
|
8
|
-
TsType,
|
|
9
|
-
TsTypeField,
|
|
10
|
-
TsTypeFieldObject,
|
|
11
|
-
} from './_types';
|
|
12
|
-
import { Kind } from 'graphql/language/kinds';
|
|
13
|
-
import { FieldNode, FragmentDefinitionNode, OperationTypeNode, SelectionNode, TypeNode } from 'graphql/language/ast';
|
|
14
|
-
import { capitalize } from './utils';
|
|
15
|
-
import { getGraphqlTypeWrappers, graphqlTypeToTypescript, selectionSetToTsType } from './graphql';
|
|
16
|
-
import { GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema } from 'graphql/type';
|
|
17
|
-
import { GraphQLType } from 'graphql/type/definition';
|
|
18
|
-
import { findUsageFragments } from './fragments';
|
|
19
|
-
|
|
20
|
-
export const pluginDirectives = ['firstOrFail', 'first', 'singleResult', 'required'];
|
|
21
|
-
export const findUsageOperation = (documents: CodegenDocuments, schema: GraphQLSchema, config: Config) => {
|
|
22
|
-
const allFragments = findUsageFragments(documents);
|
|
23
|
-
const operations: Operation[] = [];
|
|
24
|
-
for (let { document } of documents) {
|
|
25
|
-
for (let definition of document.definitions) {
|
|
26
|
-
if (definition.kind === Kind.OPERATION_DEFINITION) {
|
|
27
|
-
const name = definition.name.value;
|
|
28
|
-
let root: GraphQLObjectType;
|
|
29
|
-
if (definition.operation === OperationTypeNode.QUERY) {
|
|
30
|
-
root = schema.getQueryType();
|
|
31
|
-
} else if (definition.operation === OperationTypeNode.MUTATION) {
|
|
32
|
-
root = schema.getMutationType();
|
|
33
|
-
} else if (definition.operation === OperationTypeNode.SUBSCRIPTION) {
|
|
34
|
-
throw new Error('subscription operation is not supported');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const directives = findDirectives(definition.selectionSet.selections);
|
|
38
|
-
if (
|
|
39
|
-
config.autoSingleResult !== false &&
|
|
40
|
-
definition.selectionSet.selections.length === 1 &&
|
|
41
|
-
!directives.some(d => d.name === 'singleResult')
|
|
42
|
-
) {
|
|
43
|
-
const fields = definition.selectionSet.selections.filter(f => f.kind === Kind.FIELD) as FieldNode[];
|
|
44
|
-
directives.push({
|
|
45
|
-
name: 'singleResult',
|
|
46
|
-
path: fields[0].name.value,
|
|
47
|
-
args: {},
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
let resultsType: NamedTsType = {
|
|
52
|
-
name: capitalize(name) + 'Results',
|
|
53
|
-
...selectionSetToTsType(root, definition.selectionSet.selections, config),
|
|
54
|
-
};
|
|
55
|
-
for (let directive of directives) {
|
|
56
|
-
switch (directive.name) {
|
|
57
|
-
case 'first':
|
|
58
|
-
changeTsTypeField(resultsType, directive.path, { isList: false });
|
|
59
|
-
break;
|
|
60
|
-
case 'firstOrFail':
|
|
61
|
-
changeTsTypeField(resultsType, directive.path, { isList: false, isNullable: false });
|
|
62
|
-
break;
|
|
63
|
-
case 'required':
|
|
64
|
-
changeTsTypeField(resultsType, directive.path, { isNullable: false });
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const singleResult = directives.find(d => d.name === 'singleResult');
|
|
69
|
-
|
|
70
|
-
if (singleResult) {
|
|
71
|
-
const type = getNestedType(resultsType, singleResult.path);
|
|
72
|
-
resultsType = {
|
|
73
|
-
...type,
|
|
74
|
-
name: resultsType.name,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
operations.push({
|
|
79
|
-
name,
|
|
80
|
-
definition,
|
|
81
|
-
directives,
|
|
82
|
-
singleResultKey: singleResult ? singleResult.path : '',
|
|
83
|
-
variables: definition.variableDefinitions.length
|
|
84
|
-
? {
|
|
85
|
-
name: capitalize(name) + 'Variables',
|
|
86
|
-
fields: definition.variableDefinitions.map(variableDefinition => {
|
|
87
|
-
const type = parseVariableNode(variableDefinition.type, schema);
|
|
88
|
-
return {
|
|
89
|
-
name: variableDefinition.variable.name.value,
|
|
90
|
-
kind: 'inLine',
|
|
91
|
-
type: graphqlTypeToTypescript(type, config),
|
|
92
|
-
...getGraphqlTypeWrappers(type),
|
|
93
|
-
};
|
|
94
|
-
}),
|
|
95
|
-
}
|
|
96
|
-
: undefined,
|
|
97
|
-
results: resultsType,
|
|
98
|
-
fragments: findFragmentsUsedInSelection(definition.selectionSet.selections, allFragments),
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return operations;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const findDirectives = (selections: readonly SelectionNode[], key = '') => {
|
|
107
|
-
const directives: Directive[] = [];
|
|
108
|
-
for (let selection of selections) {
|
|
109
|
-
if (selection.kind === Kind.FIELD) {
|
|
110
|
-
const path = key + selection.name.value;
|
|
111
|
-
for (let directive of selection.directives) {
|
|
112
|
-
if (pluginDirectives.includes(directive.name.value)) {
|
|
113
|
-
directives.push({
|
|
114
|
-
name: directive.name.value,
|
|
115
|
-
path,
|
|
116
|
-
args: directive.arguments.reduce((args, arg) => {
|
|
117
|
-
if ('value' in arg.value) {
|
|
118
|
-
args[arg.name.value] = arg.value.value;
|
|
119
|
-
}
|
|
120
|
-
return args;
|
|
121
|
-
}, {} as Record<string, any>),
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (selection.selectionSet?.selections.length) {
|
|
126
|
-
directives.push(...findDirectives(selection.selectionSet.selections, path + '.'));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return directives;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const getNestedType = (type: TsType, path: string) => {
|
|
134
|
-
let t: TsType = type;
|
|
135
|
-
for (let key of path.split('.')) {
|
|
136
|
-
const field = t.fields.find(f => f.name === key);
|
|
137
|
-
if (!field) {
|
|
138
|
-
throw new Error(`cannot find field ${key}`);
|
|
139
|
-
}
|
|
140
|
-
t = field as TsType;
|
|
141
|
-
}
|
|
142
|
-
return t;
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const changeTsTypeField = <T extends any>(type: TsType, path: string, value: Partial<TsTypeField>) => {
|
|
146
|
-
const p = path.split('.');
|
|
147
|
-
let t: { fields: TsTypeField[] } = type;
|
|
148
|
-
while (p.length) {
|
|
149
|
-
const k = p.shift();
|
|
150
|
-
const f = t.fields.find(f => f.name === k);
|
|
151
|
-
if (!f) {
|
|
152
|
-
throw new Error(`cannot find field ${k}`);
|
|
153
|
-
}
|
|
154
|
-
if (p.length === 0) {
|
|
155
|
-
Object.assign(f, value);
|
|
156
|
-
} else if (f.kind === 'object') {
|
|
157
|
-
t = f;
|
|
158
|
-
} else {
|
|
159
|
-
throw new Error('logic error');
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const toFieldsMap = (f: TsTypeFieldObject) => {
|
|
165
|
-
const fields: FieldsMap = {};
|
|
166
|
-
for (let field of f.fields) {
|
|
167
|
-
fields[field.name] = {
|
|
168
|
-
...field,
|
|
169
|
-
fields: field.kind === 'object' ? toFieldsMap(field) : {},
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
return fields;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const parseVariableNode = (type: TypeNode, schema: GraphQLSchema, isNullable = true, isList = false): GraphQLType => {
|
|
176
|
-
if (type.kind === Kind.NON_NULL_TYPE) {
|
|
177
|
-
return parseVariableNode(type.type, schema, false, isList);
|
|
178
|
-
}
|
|
179
|
-
if (type.kind === Kind.LIST_TYPE) {
|
|
180
|
-
return parseVariableNode(type.type, schema, isNullable, true);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
let gqlType: GraphQLType = schema.getType(type.name.value);
|
|
184
|
-
if (isList) {
|
|
185
|
-
gqlType = new GraphQLList(gqlType);
|
|
186
|
-
}
|
|
187
|
-
if (!isNullable) {
|
|
188
|
-
gqlType = new GraphQLNonNull(gqlType);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return gqlType;
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
const findFragmentsUsedInSelection = (
|
|
195
|
-
selections: readonly SelectionNode[],
|
|
196
|
-
allFragments: FragmentDefinitionNode[],
|
|
197
|
-
fragments = new Set<string>(),
|
|
198
|
-
) => {
|
|
199
|
-
for (let selection of selections) {
|
|
200
|
-
if (selection.kind === Kind.FRAGMENT_SPREAD) {
|
|
201
|
-
const name = selection.name.value;
|
|
202
|
-
const fragment = allFragments.find(f => f.name.value === name);
|
|
203
|
-
fragments.add(name);
|
|
204
|
-
findFragmentsUsedInSelection(fragment.selectionSet.selections, allFragments, fragments);
|
|
205
|
-
}
|
|
206
|
-
if (selection.kind === Kind.FIELD && selection.selectionSet?.selections.length) {
|
|
207
|
-
findFragmentsUsedInSelection(selection.selectionSet.selections, allFragments, fragments);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return [...fragments];
|
|
212
|
-
};
|
package/src/prettier.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Config } from './_types';
|
|
2
|
-
|
|
3
|
-
export const runPrettierIfExists = (config: Config, content: string) => {
|
|
4
|
-
if (config.prettier === false) {
|
|
5
|
-
return content;
|
|
6
|
-
}
|
|
7
|
-
try {
|
|
8
|
-
const prettier = require('prettier');
|
|
9
|
-
return prettier.format(content, { parser: 'typescript' });
|
|
10
|
-
} catch (e) {
|
|
11
|
-
return content;
|
|
12
|
-
}
|
|
13
|
-
};
|
package/src/print.ts
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assertObjectType,
|
|
3
|
-
GraphQLEnumType,
|
|
4
|
-
GraphQLInputObjectType,
|
|
5
|
-
GraphQLScalarType,
|
|
6
|
-
GraphQLSchema,
|
|
7
|
-
} from 'graphql/type';
|
|
8
|
-
import { Config, NamedTsType, Operation, TsTypeField } from './_types';
|
|
9
|
-
import { print } from 'graphql/language';
|
|
10
|
-
import { FragmentDefinitionNode } from 'graphql/language/ast';
|
|
11
|
-
import { getGraphqlTypeWrappers, graphqlTypeToTypescript, selectionSetToTsType } from './graphql';
|
|
12
|
-
import * as fs from 'fs';
|
|
13
|
-
import * as path from 'path';
|
|
14
|
-
import { pluginDirectives } from './operation';
|
|
15
|
-
|
|
16
|
-
export const printOperationTypes = (operation: Operation, config: Config) => {
|
|
17
|
-
const content: string[] = [];
|
|
18
|
-
if (operation.variables) {
|
|
19
|
-
content.push(printTsType(operation.variables));
|
|
20
|
-
}
|
|
21
|
-
content.push(printTsType(operation.results));
|
|
22
|
-
|
|
23
|
-
const queryFragments = operation.fragments.map(fragment => `$\{${fragment}FragmentQuery}\n`);
|
|
24
|
-
let query = print(operation.definition);
|
|
25
|
-
for (let pluginDirective of pluginDirectives) {
|
|
26
|
-
query = query.replace(new RegExp('@' + pluginDirective, 'g'), '');
|
|
27
|
-
}
|
|
28
|
-
content.push(`const ${getOperationQueryName(operation.name)} = \`${queryFragments.join('')}${query}\`;`);
|
|
29
|
-
return content.join('\n');
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const printCreateSdkFunction = (operations: Operation[], config: Config) => {
|
|
33
|
-
const fields = operations
|
|
34
|
-
.map(operation => {
|
|
35
|
-
const hasVariables = !!operation.variables;
|
|
36
|
-
const functions: string[] = [];
|
|
37
|
-
|
|
38
|
-
for (let directive of operation.directives) {
|
|
39
|
-
switch (directive.name) {
|
|
40
|
-
case 'first':
|
|
41
|
-
case 'firstOrFail':
|
|
42
|
-
case 'required':
|
|
43
|
-
functions.push(`${directive.name}("${directive.path}")`);
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (operation.singleResultKey) {
|
|
49
|
-
functions.push(`singleResult("${operation.singleResultKey}")`);
|
|
50
|
-
}
|
|
51
|
-
const defArgs: string[] = [];
|
|
52
|
-
const execArgs: string[] = ['client'];
|
|
53
|
-
if (hasVariables) {
|
|
54
|
-
defArgs.push(`variables: ${operation.variables.name}`);
|
|
55
|
-
execArgs.push(`{variables, query: ${getOperationQueryName(operation.name)}}`);
|
|
56
|
-
} else {
|
|
57
|
-
execArgs.push(`{query: ${getOperationQueryName(operation.name)}}`);
|
|
58
|
-
}
|
|
59
|
-
execArgs.push(`[${functions.join(',')}]`);
|
|
60
|
-
execArgs.push('config');
|
|
61
|
-
defArgs.push(`config?: AxiosRequestConfig`);
|
|
62
|
-
return `${operation.name}: (${defArgs.join(',')}): Promise<${operation.results.name}> => execute(${execArgs.join(
|
|
63
|
-
',',
|
|
64
|
-
)})`;
|
|
65
|
-
})
|
|
66
|
-
.join(',');
|
|
67
|
-
return `export const createSdk = (client: AxiosInstance) => ({${fields}});`;
|
|
68
|
-
};
|
|
69
|
-
export const printEnum = (e: GraphQLEnumType, config: Config) => {
|
|
70
|
-
const suffix = config?.suffix?.enum || '';
|
|
71
|
-
const values = e.getValues().map(({ name, value }) => `${name} = "${value}"`);
|
|
72
|
-
return `export enum ${e.name + suffix} {${values.join(',')}};`;
|
|
73
|
-
};
|
|
74
|
-
export const printFragmentType = (fragment: FragmentDefinitionNode, schema: GraphQLSchema, config: Config) => {
|
|
75
|
-
const suffix = config?.suffix?.input || '';
|
|
76
|
-
const parent = assertObjectType(schema.getType(fragment.typeCondition.name.value));
|
|
77
|
-
return printTsType({
|
|
78
|
-
name: fragment.name.value + suffix,
|
|
79
|
-
...selectionSetToTsType(parent, fragment.selectionSet.selections, config),
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const printFragmentGql = (fragment: FragmentDefinitionNode) => {
|
|
84
|
-
return `const ${fragment.name.value}FragmentQuery = \`${print(fragment)}\``;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export const printInput = (input: GraphQLInputObjectType, config: Config) => {
|
|
88
|
-
const suffix = config?.suffix?.input || '';
|
|
89
|
-
return printTsType({
|
|
90
|
-
name: input.name + suffix,
|
|
91
|
-
fields: Object.values(input.getFields()).map(field => ({
|
|
92
|
-
name: field.name,
|
|
93
|
-
kind: 'inLine',
|
|
94
|
-
type: graphqlTypeToTypescript(field.type, config),
|
|
95
|
-
...getGraphqlTypeWrappers(field.type),
|
|
96
|
-
})),
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export const printHelpers = () => {
|
|
101
|
-
return fs.readFileSync(path.resolve(__dirname, '../templates/helpers.ts'));
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export const printScalars = (scalars: GraphQLScalarType[], config: Config) => {
|
|
105
|
-
const map: Record<string, string> = {
|
|
106
|
-
String: 'string',
|
|
107
|
-
Int: 'number',
|
|
108
|
-
Float: 'number',
|
|
109
|
-
Boolean: 'boolean',
|
|
110
|
-
...(config.scalars || {}),
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return `export type Scalar = {${Object.entries(map).map(([name, value]) => `${name}: ${value}`)}};`;
|
|
114
|
-
};
|
|
115
|
-
const printTsTypeFields = (fields: TsTypeField[]) => {
|
|
116
|
-
const ts: string[] = fields.map(field => {
|
|
117
|
-
let fieldType: string;
|
|
118
|
-
if (field.kind === 'inLine') {
|
|
119
|
-
fieldType = field.type;
|
|
120
|
-
} else {
|
|
121
|
-
const unions = field.unions?.length ? field.unions.join(' & ') + ' &' : '';
|
|
122
|
-
fieldType = unions + printTsTypeFields(field.fields);
|
|
123
|
-
}
|
|
124
|
-
if (field.isList) {
|
|
125
|
-
fieldType += '[]';
|
|
126
|
-
}
|
|
127
|
-
if (field.isNullable) {
|
|
128
|
-
fieldType = `Nullable<${fieldType}>`;
|
|
129
|
-
}
|
|
130
|
-
return `${field.name}${field.isNullable ? '?' : ''}: ${fieldType}`;
|
|
131
|
-
});
|
|
132
|
-
return `{${ts.join(',')}}`;
|
|
133
|
-
};
|
|
134
|
-
const printTsType = (type: NamedTsType) => {
|
|
135
|
-
const union = type.unions && type.unions.length ? type.unions.join(' & ') + ' &' : '';
|
|
136
|
-
return `export type ${type.name} = ${union} ${printTsTypeFields(type.fields)};`;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const getOperationQueryName = (name: string) => `${name}Query`;
|
package/src/scalar.ts
DELETED
package/src/utils.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
|