@devticon-os/graphql-codegen-axios 0.2.14 → 0.2.15
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/package.json +1 -1
- package/src/functions.js +6 -1
- package/src/helpers.ts +1 -1
- package/src/index.js +8 -7
- package/src/render.js +8 -4
- package/src/results.js +1 -0
package/package.json
CHANGED
package/src/functions.js
CHANGED
|
@@ -21,6 +21,11 @@ const getFunctionChain = (operation, useSingleResults) => {
|
|
|
21
21
|
return chain;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
const isSingleResultOperation = operation =>
|
|
24
|
+
const isSingleResultOperation = (operation, config) => {
|
|
25
|
+
if (config.autoSingleResult === undefined || config.autoSingleResult === true) {
|
|
26
|
+
return operation.selectionSet.selections.length === 1;
|
|
27
|
+
}
|
|
28
|
+
return operation.directives.some(d => d.name.value === 'singleResult');
|
|
29
|
+
};
|
|
25
30
|
|
|
26
31
|
module.exports = { getFunctionChain, isSingleResultOperation };
|
package/src/helpers.ts
CHANGED
|
@@ -33,7 +33,7 @@ const nonNullable = (key: string, reqParams: GraphqlRequestParams) => (data: any
|
|
|
33
33
|
return data;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
export const handleResponse =
|
|
36
|
+
export const handleResponse = ({ data }: AxiosResponse<GraphqlResponse<any>>): any => {
|
|
37
37
|
const errors = data.errors;
|
|
38
38
|
if (errors && errors.length > 0) {
|
|
39
39
|
throw new GraphqlError('Request failed', errors);
|
package/src/index.js
CHANGED
|
@@ -2,7 +2,7 @@ const fs = require('fs');
|
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const { findUsageInputs } = require('./input');
|
|
4
4
|
const { getVariablesFields } = require('./variables');
|
|
5
|
-
const {
|
|
5
|
+
const { getResultType } = require('./results');
|
|
6
6
|
const { findScalars } = require('./scalar');
|
|
7
7
|
const {
|
|
8
8
|
renderType,
|
|
@@ -19,9 +19,14 @@ const { getFunctionChain, isSingleResultOperation } = require('./functions');
|
|
|
19
19
|
const { capitalize } = require('./utils');
|
|
20
20
|
|
|
21
21
|
const helpers = fs.readFileSync(path.join(__dirname, 'helpers.ts'), 'utf-8');
|
|
22
|
+
const directives = ` directive @first on OBJECT
|
|
23
|
+
directive @firstOrFail on OBJECT
|
|
24
|
+
directive @singleResult on OBJECT
|
|
25
|
+
directive @nonNullable on OBJECT`;
|
|
22
26
|
module.exports = {
|
|
23
27
|
plugin(schema, documents, config) {
|
|
24
28
|
try {
|
|
29
|
+
fs.writeFileSync('directives.graphql', directives);
|
|
25
30
|
const functions = [];
|
|
26
31
|
const queries = [];
|
|
27
32
|
const inputs = findUsageInputs(documents, schema);
|
|
@@ -35,7 +40,7 @@ module.exports = {
|
|
|
35
40
|
continue;
|
|
36
41
|
}
|
|
37
42
|
const name = definition.name.value;
|
|
38
|
-
const useSingleResults = isSingleResultOperation(definition);
|
|
43
|
+
const useSingleResults = isSingleResultOperation(definition, config);
|
|
39
44
|
|
|
40
45
|
const results = getResultType(definition, schema, document, useSingleResults);
|
|
41
46
|
types.push(results);
|
|
@@ -85,9 +90,5 @@ module.exports = {
|
|
|
85
90
|
process.exit(1);
|
|
86
91
|
}
|
|
87
92
|
},
|
|
88
|
-
addToSchema:
|
|
89
|
-
directive @first on OBJECT
|
|
90
|
-
directive @firstOrFail on OBJECT
|
|
91
|
-
directive @nonNullable on OBJECT
|
|
92
|
-
`,
|
|
93
|
+
addToSchema: directives,
|
|
93
94
|
};
|
package/src/render.js
CHANGED
|
@@ -27,7 +27,7 @@ const renderType = ({ name, fields, union, isList, isNullable, gqlType }, config
|
|
|
27
27
|
const renderHeader = text => `/** \n ${text} \n **/`;
|
|
28
28
|
const renderTypeField = (fields, config) => {
|
|
29
29
|
return fields
|
|
30
|
-
.map(({ isList, isNullable, typeName, name, fields, isScalar, union, type, inLine, gqlType }) => {
|
|
30
|
+
.map(({ isList, isNullable, typeName, name, fields, isScalar, union, type, inLine, gqlType, alias }) => {
|
|
31
31
|
let tsType = '';
|
|
32
32
|
if (union && union.length) {
|
|
33
33
|
tsType += [...union.map(u => getName(u, 'fragment', config)), ''].join(' & ');
|
|
@@ -40,7 +40,7 @@ const renderTypeField = (fields, config) => {
|
|
|
40
40
|
if (type instanceof GraphQLInputObjectType || type instanceof GraphQLEnumType) {
|
|
41
41
|
tsType += gqlType ? getName(typeName, gqlType, config) : typeName;
|
|
42
42
|
} else {
|
|
43
|
-
tsType += `{${renderTypeField(fields)}}`;
|
|
43
|
+
tsType += `{${renderTypeField(fields, config)}}`;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
tsType = `(${tsType})`;
|
|
@@ -50,7 +50,7 @@ const renderTypeField = (fields, config) => {
|
|
|
50
50
|
if (isNullable) {
|
|
51
51
|
tsType = `Nullable<${tsType}>`;
|
|
52
52
|
}
|
|
53
|
-
return `${name}${isNullable ? '?' : ''}: ${tsType}`;
|
|
53
|
+
return `${alias || name}${isNullable ? '?' : ''}: ${tsType}`;
|
|
54
54
|
})
|
|
55
55
|
.join(',\n');
|
|
56
56
|
};
|
|
@@ -72,7 +72,11 @@ const renderFunction = ({ name, variables, results, chain }) => {
|
|
|
72
72
|
};
|
|
73
73
|
|
|
74
74
|
const renderQuery = ({ name, ast, allFragments }) => {
|
|
75
|
-
const raw = print(ast)
|
|
75
|
+
const raw = print(ast)
|
|
76
|
+
.replace(/@firstOrFail/g, '')
|
|
77
|
+
.replace(/@first/g, '')
|
|
78
|
+
.replace(/@nonNullable/g, '')
|
|
79
|
+
.replace(/@singleResult/g, '');
|
|
76
80
|
|
|
77
81
|
let fragments = getUsedFragments(raw, allFragments);
|
|
78
82
|
fragments = [...new Set(fragments)].map(f => `\${${f.name}FragmentQuery}`);
|