@devticon-os/graphql-codegen-axios 0.2.18 → 0.3.1-test
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 +105 -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 +141 -0
- package/lib/input.js.map +1 -0
- package/lib/operation.js +322 -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 +188 -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 +13 -5
- package/templates/helpers.ts +95 -0
- package/src/enums.js +0 -35
- package/src/fragments.js +0 -35
- package/src/functions.js +0 -34
- package/src/helpers.ts +0 -59
- package/src/index.js +0 -94
- package/src/input.js +0 -77
- package/src/query.js +0 -14
- package/src/render.js +0 -118
- package/src/results.js +0 -43
- package/src/scalar.js +0 -6
- package/src/types.js +0 -49
- package/src/utils.js +0 -3
- package/src/variables.js +0 -34
package/lib/variables.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.getVariablesFields = void 0;
|
|
15
|
+
var input_1 = require("./input");
|
|
16
|
+
var enums_1 = require("./enums");
|
|
17
|
+
var language_1 = require("graphql/language");
|
|
18
|
+
var getVariablesFields = function (definition, schema) {
|
|
19
|
+
return definition.variableDefinitions.map(function (variable) { return (__assign(__assign({ name: variable.variable.name.value, alias: variable.variable.name.value, union: [] }, getVariableType(variable.type, schema)), { fields: [] })); });
|
|
20
|
+
};
|
|
21
|
+
exports.getVariablesFields = getVariablesFields;
|
|
22
|
+
var getVariableType = function (type, schema, isList, isNullable) {
|
|
23
|
+
if (isList === void 0) { isList = false; }
|
|
24
|
+
if (isNullable === void 0) { isNullable = true; }
|
|
25
|
+
if (type.kind === language_1.Kind.LIST_TYPE) {
|
|
26
|
+
isList = true;
|
|
27
|
+
return getVariableType(type.type, schema, isList, isNullable);
|
|
28
|
+
}
|
|
29
|
+
if (type.kind === language_1.Kind.NON_NULL_TYPE) {
|
|
30
|
+
isNullable = false;
|
|
31
|
+
return getVariableType(type.type, schema, isList, isNullable);
|
|
32
|
+
}
|
|
33
|
+
var typeName = type.name.value;
|
|
34
|
+
var isScalar = !(0, input_1.findInputInSchema)(typeName, schema) && !(0, enums_1.findEnumInSchema)(typeName, schema);
|
|
35
|
+
return {
|
|
36
|
+
isList: isList,
|
|
37
|
+
isNullable: isNullable,
|
|
38
|
+
typeName: typeName,
|
|
39
|
+
isScalar: isScalar,
|
|
40
|
+
inLine: !isScalar,
|
|
41
|
+
gqlType: isScalar ? 'scalar' : 'input',
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=variables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variables.js","sourceRoot":"","sources":["../src/variables.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAA4C;AAC5C,iCAA2C;AAG3C,6CAAwC;AAGjC,IAAM,kBAAkB,GAAG,UAAC,UAAmC,EAAE,MAAqB;IAC3F,OAAO,UAAU,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAA,QAAQ,IAAI,OAAA,qBACpD,IAAI,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAClC,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EACnC,KAAK,EAAE,EAAE,IACN,eAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,KACzC,MAAM,EAAE,EAAE,IACV,EANoD,CAMpD,CAAC,CAAC;AACN,CAAC,CAAC;AARW,QAAA,kBAAkB,sBAQ7B;AAEF,IAAM,eAAe,GAAG,UACtB,IAAc,EACd,MAAqB,EACrB,MAAc,EACd,UAAiB;IADjB,uBAAA,EAAA,cAAc;IACd,2BAAA,EAAA,iBAAiB;IAEjB,IAAI,IAAI,CAAC,IAAI,KAAK,eAAI,CAAC,SAAS,EAAE;QAChC,MAAM,GAAG,IAAI,CAAC;QACd,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC/D;IACD,IAAI,IAAI,CAAC,IAAI,KAAK,eAAI,CAAC,aAAa,EAAE;QACpC,UAAU,GAAG,KAAK,CAAC;QACnB,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;KAC/D;IACD,IAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;IACjC,IAAM,QAAQ,GAAG,CAAC,IAAA,yBAAiB,EAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,IAAA,wBAAgB,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC7F,OAAO;QACL,MAAM,QAAA;QACN,UAAU,YAAA;QACV,QAAQ,UAAA;QACR,QAAQ,UAAA;QACR,MAAM,EAAE,CAAC,QAAQ;QACjB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO;KACvC,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devticon-os/graphql-codegen-axios",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1-test",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"repository": "https://github.com/devticon/graphql-codegen-axios",
|
|
7
7
|
"author": {
|
|
8
8
|
"name": "krs",
|
|
9
9
|
"email": "k.kamasa@gmail.com"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
|
-
"
|
|
12
|
+
"lib",
|
|
13
|
+
"templates"
|
|
13
14
|
],
|
|
14
15
|
"scripts": {
|
|
15
|
-
"start": "node src/index.
|
|
16
|
+
"start": "node src/index.ts",
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"build:watch": "tsc --watch"
|
|
16
19
|
},
|
|
17
20
|
"peerDependencies": {
|
|
18
21
|
"axios": "^1.3.3",
|
|
19
22
|
"graphql": "^16.6.0"
|
|
20
23
|
},
|
|
21
24
|
"devDependencies": {
|
|
22
|
-
"
|
|
25
|
+
"@types/node": "^18.15.0",
|
|
26
|
+
"axios": "^1.3.4",
|
|
27
|
+
"graphql": "^16.6.0",
|
|
28
|
+
"prettier": "^2.8.4",
|
|
29
|
+
"ts-node": "^10.9.1",
|
|
30
|
+
"typescript": "^4.9.5"
|
|
23
31
|
}
|
|
24
32
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { AxiosResponse, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { GraphQLError } from 'graphql';
|
|
3
|
+
|
|
4
|
+
type Nullable<T> = T | undefined;
|
|
5
|
+
|
|
6
|
+
type GraphqlResponse<T> = {
|
|
7
|
+
data: T;
|
|
8
|
+
errors?: GraphQLError[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type GraphqlRequestParams = {
|
|
12
|
+
query: string;
|
|
13
|
+
variables?: any;
|
|
14
|
+
};
|
|
15
|
+
const first = (key: string) => (data: any) => {
|
|
16
|
+
data[key] = data[key][0];
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const firstOrFail = (key: string) => (data: any) => {
|
|
21
|
+
data[key] = (data as any)[key][0];
|
|
22
|
+
if (!data[key]) {
|
|
23
|
+
throw {
|
|
24
|
+
message: `Empty list for ${key} `,
|
|
25
|
+
code: 'EMPTY_LIST',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return data;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const required = (path: string) => (data: any) => {
|
|
32
|
+
const p = path.split('.');
|
|
33
|
+
let d = data;
|
|
34
|
+
while (p.length) {
|
|
35
|
+
const k = p.shift();
|
|
36
|
+
const f = d[k];
|
|
37
|
+
if (Array.isArray(f)) {
|
|
38
|
+
for (let i of f) {
|
|
39
|
+
required(p.join('.'))(f);
|
|
40
|
+
}
|
|
41
|
+
} else if (p.length > 0) {
|
|
42
|
+
d = f;
|
|
43
|
+
} else if (!f) {
|
|
44
|
+
throw {
|
|
45
|
+
message: `missing required field: ${path}`,
|
|
46
|
+
code: 'MISSING_FIELD',
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return data;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const execute = (
|
|
54
|
+
client: AxiosInstance,
|
|
55
|
+
body: GraphqlRequestParams,
|
|
56
|
+
functions: ((data: any) => any)[],
|
|
57
|
+
config?: AxiosRequestConfig,
|
|
58
|
+
) =>
|
|
59
|
+
client.post('', body, config).then(({ data }: AxiosResponse<GraphqlResponse<any>>) => {
|
|
60
|
+
const errors = data.errors;
|
|
61
|
+
if (errors && errors.length > 0) {
|
|
62
|
+
throw new GraphqlError('Request failed', errors);
|
|
63
|
+
}
|
|
64
|
+
let d = data.data;
|
|
65
|
+
for (let func of functions) {
|
|
66
|
+
try {
|
|
67
|
+
d = func(d);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
if (e.code) {
|
|
70
|
+
throw new QueryError(e.message, e.code, body);
|
|
71
|
+
} else {
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return d;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export const singleResult = (key: string) => (data: any) => data[key];
|
|
80
|
+
|
|
81
|
+
export class GraphqlError extends Error {
|
|
82
|
+
constructor(message: string, public gqlErrors: GraphQLError[]) {
|
|
83
|
+
super(`${message} ${gqlErrors.map(e => e.message).join('\n')}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class QueryError extends Error {
|
|
88
|
+
query: string;
|
|
89
|
+
variables: any;
|
|
90
|
+
constructor(message: string, code: string, params: GraphqlRequestParams) {
|
|
91
|
+
super(message);
|
|
92
|
+
this.query = params.query;
|
|
93
|
+
this.variables = params.variables;
|
|
94
|
+
}
|
|
95
|
+
}
|
package/src/enums.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const { GraphQLEnumType } = require('graphql/type');
|
|
2
|
-
const findUsageEnums = (types, schema) => {
|
|
3
|
-
const enums = [];
|
|
4
|
-
for (let type of types) {
|
|
5
|
-
for (let e of findEnumInType(type, schema, enums)) {
|
|
6
|
-
if (!enums.includes(e)) {
|
|
7
|
-
enums.push(e);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return enums.map(e => ({
|
|
13
|
-
name: e.name,
|
|
14
|
-
values: e._values.map(({ name, value }) => ({ name, value })),
|
|
15
|
-
}));
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const findEnumInType = (type, schema, ignore) => {
|
|
19
|
-
const enums = [];
|
|
20
|
-
for (let field of type.fields) {
|
|
21
|
-
const type = schema._typeMap[field.typeName];
|
|
22
|
-
if (type instanceof GraphQLEnumType && !ignore.includes(type)) {
|
|
23
|
-
enums.push(type);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return enums;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const findEnumInSchema = (name, schema) => {
|
|
30
|
-
const type = schema._typeMap[name];
|
|
31
|
-
if (type instanceof GraphQLEnumType) {
|
|
32
|
-
return type;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
module.exports = { findUsageEnums, findEnumInSchema };
|
package/src/fragments.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const { GraphQLObjectType } = require('graphql/type');
|
|
2
|
-
const { getGraphqlTypeInfo } = require('./types');
|
|
3
|
-
const { getField } = require('./results');
|
|
4
|
-
const findUsageFragments = (documents, schema) => {
|
|
5
|
-
const fragments = [];
|
|
6
|
-
for (let { document } of documents) {
|
|
7
|
-
for (const definition of document.definitions) {
|
|
8
|
-
if (definition.kind === 'FragmentDefinition') {
|
|
9
|
-
const name = definition.name.value;
|
|
10
|
-
const parentName = definition.typeCondition.name.value;
|
|
11
|
-
const parent = findObjectTypeInSchema(schema, parentName);
|
|
12
|
-
const nestedFragments = definition.selectionSet.selections.filter(s => s.kind === 'FragmentSpread');
|
|
13
|
-
const fields = definition.selectionSet.selections.filter(s => s.kind !== 'FragmentSpread');
|
|
14
|
-
const union = nestedFragments.map(f => f.name.value);
|
|
15
|
-
|
|
16
|
-
fragments.push({
|
|
17
|
-
name,
|
|
18
|
-
type: definition,
|
|
19
|
-
union,
|
|
20
|
-
fields: fields.map(f => getField(parent, f, schema)),
|
|
21
|
-
gqlType: 'fragment',
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return fragments;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const findObjectTypeInSchema = (schema, name) => {
|
|
30
|
-
const type = schema._typeMap[name];
|
|
31
|
-
if (type instanceof GraphQLObjectType) {
|
|
32
|
-
return type;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
module.exports = { findUsageFragments };
|
package/src/functions.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const getFunctionChain = (operation, useSingleResults) => {
|
|
2
|
-
const chain = [];
|
|
3
|
-
for (let selection of operation.selectionSet.selections) {
|
|
4
|
-
const directives = selection.directives.map(d => d.name.value);
|
|
5
|
-
const propertyName = selection.name.value;
|
|
6
|
-
for (let directive of directives) {
|
|
7
|
-
if (!['first', 'firstOrFail', 'nonNullable', 'singleResult'].includes(directive)) {
|
|
8
|
-
continue;
|
|
9
|
-
}
|
|
10
|
-
if (directive === 'nonNullable' || directive === 'firstOrFail') {
|
|
11
|
-
chain.push(`${directive}("${propertyName}", body)`);
|
|
12
|
-
} else if (directive === 'first') {
|
|
13
|
-
chain.push(`${directive}("${propertyName}")`);
|
|
14
|
-
} else {
|
|
15
|
-
chain.push(directive);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (useSingleResults) {
|
|
21
|
-
const propertyName = operation.selectionSet.selections[0].name.value;
|
|
22
|
-
chain.push(`unpackSingleResults("${propertyName}")`);
|
|
23
|
-
}
|
|
24
|
-
return chain;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const isSingleResultOperation = (operation, config) => {
|
|
28
|
-
if (config.autoSingleResult === undefined || config.autoSingleResult === true) {
|
|
29
|
-
return operation.selectionSet.selections.length === 1;
|
|
30
|
-
}
|
|
31
|
-
return operation.directives.some(d => d.name.value === 'singleResult');
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
module.exports = { getFunctionChain, isSingleResultOperation };
|
package/src/helpers.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { AxiosResponse, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
-
import { GraphQLError } from 'graphql';
|
|
3
|
-
|
|
4
|
-
type Nullable<T> = T | undefined;
|
|
5
|
-
|
|
6
|
-
type GraphqlResponse<T> = {
|
|
7
|
-
data: T;
|
|
8
|
-
errors?: GraphQLError[];
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type GraphqlRequestParams = {
|
|
12
|
-
query: string;
|
|
13
|
-
variables: any;
|
|
14
|
-
};
|
|
15
|
-
const first = (key: string) => (data: any) => {
|
|
16
|
-
data[key] = data[key][0];
|
|
17
|
-
return data;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const firstOrFail = (key: string, reqParams: GraphqlRequestParams) => (data: any) => {
|
|
21
|
-
data[key] = (data as any)[key][0];
|
|
22
|
-
if (!data[key]) {
|
|
23
|
-
throw new QueryNoResultsError(reqParams);
|
|
24
|
-
}
|
|
25
|
-
return data;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const nonNullable = (key: string, reqParams: GraphqlRequestParams) => (data: any) => {
|
|
29
|
-
const row = data[key];
|
|
30
|
-
if (!row) {
|
|
31
|
-
throw new QueryNoResultsError(reqParams);
|
|
32
|
-
}
|
|
33
|
-
return data;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export const handleResponse = ({ data }: AxiosResponse<GraphqlResponse<any>>): any => {
|
|
37
|
-
const errors = data.errors;
|
|
38
|
-
if (errors && errors.length > 0) {
|
|
39
|
-
throw new GraphqlError('Request failed', errors);
|
|
40
|
-
}
|
|
41
|
-
return data.data;
|
|
42
|
-
};
|
|
43
|
-
export const unpackSingleResults = (key: string) => (data: any) => data[key];
|
|
44
|
-
|
|
45
|
-
export class GraphqlError extends Error {
|
|
46
|
-
constructor(message: string, public gqlErrors: GraphQLError[]) {
|
|
47
|
-
super(`${message} ${gqlErrors.map(e => e.message).join('\n')}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export class QueryNoResultsError extends Error {
|
|
52
|
-
query: string;
|
|
53
|
-
variables: any;
|
|
54
|
-
constructor(params: GraphqlRequestParams) {
|
|
55
|
-
super(`Query has no results`);
|
|
56
|
-
this.query = params.query;
|
|
57
|
-
this.variables = params.variables;
|
|
58
|
-
}
|
|
59
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const { findUsageInputs } = require('./input');
|
|
4
|
-
const { getVariablesFields } = require('./variables');
|
|
5
|
-
const { getResultType } = require('./results');
|
|
6
|
-
const { findScalars } = require('./scalar');
|
|
7
|
-
const {
|
|
8
|
-
renderType,
|
|
9
|
-
renderQuery,
|
|
10
|
-
renderSdk,
|
|
11
|
-
renderScalars,
|
|
12
|
-
renderEnum,
|
|
13
|
-
renderHeader,
|
|
14
|
-
renderFragment,
|
|
15
|
-
} = require('./render');
|
|
16
|
-
const { findUsageEnums } = require('./enums');
|
|
17
|
-
const { findUsageFragments } = require('./fragments');
|
|
18
|
-
const { getFunctionChain, isSingleResultOperation } = require('./functions');
|
|
19
|
-
const { capitalize } = require('./utils');
|
|
20
|
-
|
|
21
|
-
const helpers = fs.readFileSync(path.join(__dirname, 'helpers.ts'), 'utf-8');
|
|
22
|
-
const directives = `directive @first on FIELD
|
|
23
|
-
directive @firstOrFail on FIELD
|
|
24
|
-
directive @singleResult on QUERY | MUTATION
|
|
25
|
-
directive @nonNullable on FIELD`;
|
|
26
|
-
module.exports = {
|
|
27
|
-
plugin(schema, documents, config) {
|
|
28
|
-
try {
|
|
29
|
-
fs.writeFileSync(path.join(config.directivesFilePath || '', 'directives.graphql'), directives);
|
|
30
|
-
const functions = [];
|
|
31
|
-
const queries = [];
|
|
32
|
-
const inputs = findUsageInputs(documents, schema);
|
|
33
|
-
const fragments = findUsageFragments(documents, schema);
|
|
34
|
-
const types = [];
|
|
35
|
-
const scalars = findScalars(schema);
|
|
36
|
-
|
|
37
|
-
for (let { document } of documents) {
|
|
38
|
-
for (const definition of document.definitions) {
|
|
39
|
-
if (definition.kind !== 'OperationDefinition') {
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
const name = definition.name.value;
|
|
43
|
-
const useSingleResults = isSingleResultOperation(definition, config);
|
|
44
|
-
|
|
45
|
-
const results = getResultType(definition, schema, document, useSingleResults);
|
|
46
|
-
types.push(results);
|
|
47
|
-
|
|
48
|
-
const variables = {
|
|
49
|
-
name: capitalize(`${name}Variables`),
|
|
50
|
-
fields: getVariablesFields(definition, schema),
|
|
51
|
-
};
|
|
52
|
-
types.push(variables);
|
|
53
|
-
|
|
54
|
-
queries.push({
|
|
55
|
-
name,
|
|
56
|
-
ast: definition,
|
|
57
|
-
allFragments: fragments,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
functions.push({
|
|
61
|
-
name,
|
|
62
|
-
results,
|
|
63
|
-
variables,
|
|
64
|
-
chain: getFunctionChain(definition, useSingleResults),
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const enums = findUsageEnums([...types, ...inputs, ...fragments], schema);
|
|
70
|
-
return [
|
|
71
|
-
renderHeader('HELPERS'),
|
|
72
|
-
helpers,
|
|
73
|
-
renderHeader('Scalars'),
|
|
74
|
-
renderScalars(scalars, config),
|
|
75
|
-
renderHeader('Enum'),
|
|
76
|
-
...enums.map(e => renderEnum(e, config)),
|
|
77
|
-
renderHeader('FRAGMENTS'),
|
|
78
|
-
...fragments.map(t => renderType(t, config)),
|
|
79
|
-
...fragments.map(f => renderFragment(f)),
|
|
80
|
-
renderHeader('INPUTS'),
|
|
81
|
-
...inputs.map(t => renderType(t, config)),
|
|
82
|
-
renderHeader('TYPES'),
|
|
83
|
-
...types.map(t => renderType(t, config)),
|
|
84
|
-
renderHeader('QUERIES'),
|
|
85
|
-
...queries.map(q => renderQuery(q)),
|
|
86
|
-
renderSdk(functions),
|
|
87
|
-
].join('\n');
|
|
88
|
-
} catch (e) {
|
|
89
|
-
console.log(e);
|
|
90
|
-
process.exit(1);
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
addToSchema: directives,
|
|
94
|
-
};
|
package/src/input.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
const { GraphQLInputObjectType, GraphQLNonNull, GraphQLList } = require('graphql/type');
|
|
2
|
-
const { getGraphqlTypeInfo } = require('./types');
|
|
3
|
-
const findUsageInputs = (documents, schema) => {
|
|
4
|
-
const inputs = [];
|
|
5
|
-
for (let { document } of documents) {
|
|
6
|
-
for (let definition of document.definitions) {
|
|
7
|
-
if (definition.kind !== 'OperationDefinition') {
|
|
8
|
-
continue;
|
|
9
|
-
}
|
|
10
|
-
for (const variableDefinition of definition.variableDefinitions) {
|
|
11
|
-
const type = unpackVariableType(variableDefinition.type);
|
|
12
|
-
const name = type.name.value;
|
|
13
|
-
const input = findInputInSchema(name, schema);
|
|
14
|
-
if (input && !inputs.includes(input)) {
|
|
15
|
-
inputs.push(input);
|
|
16
|
-
inputs.push(...findInputDependencies(input, schema, inputs));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return [...new Set(inputs)].map(input => ({
|
|
22
|
-
name: input.name,
|
|
23
|
-
fields: getInputFields(input),
|
|
24
|
-
gqlType: 'input',
|
|
25
|
-
}));
|
|
26
|
-
};
|
|
27
|
-
const findInputInSchema = (name, schema) => {
|
|
28
|
-
const type = schema._typeMap[name];
|
|
29
|
-
if (type instanceof GraphQLInputObjectType) {
|
|
30
|
-
return type;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
const findInputDependencies = (input, schema, ignore) => {
|
|
34
|
-
const dependencies = [];
|
|
35
|
-
for (let field of Object.values(input._fields)) {
|
|
36
|
-
const type = unpackInputType(field.type);
|
|
37
|
-
if (type instanceof GraphQLInputObjectType && !ignore.includes(type)) {
|
|
38
|
-
dependencies.push(type);
|
|
39
|
-
dependencies.push(...findInputDependencies(type, schema, [...ignore, ...dependencies]));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return dependencies;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const getInputFields = input => {
|
|
46
|
-
return Object.values(input._fields).map(field => {
|
|
47
|
-
const typeInfo = getGraphqlTypeInfo(field.type);
|
|
48
|
-
return {
|
|
49
|
-
name: field.name,
|
|
50
|
-
fields: [],
|
|
51
|
-
...typeInfo,
|
|
52
|
-
inLine: !typeInfo.isScalar,
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const unpackInputType = type => {
|
|
58
|
-
if (type instanceof GraphQLNonNull) {
|
|
59
|
-
return unpackInputType(type.ofType);
|
|
60
|
-
}
|
|
61
|
-
if (type instanceof GraphQLList) {
|
|
62
|
-
return unpackInputType(type.ofType);
|
|
63
|
-
}
|
|
64
|
-
return type;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const unpackVariableType = type => {
|
|
68
|
-
if (type.kind === 'ListType') {
|
|
69
|
-
return unpackVariableType(type.type);
|
|
70
|
-
}
|
|
71
|
-
if (type.kind === 'NonNullType') {
|
|
72
|
-
return unpackVariableType(type.type);
|
|
73
|
-
}
|
|
74
|
-
return type;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
module.exports = { findUsageInputs, findInputInSchema };
|
package/src/query.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const { print } = require('graphql/index');
|
|
2
|
-
const getUsedFragments = (raw, allFragments, results = []) => {
|
|
3
|
-
const fragments = [...raw.matchAll(/\.\.\.(\w*)$/gm)]
|
|
4
|
-
.map(([_, name]) => name)
|
|
5
|
-
.map(name => allFragments.find(d => d.name === name));
|
|
6
|
-
|
|
7
|
-
for (let fragment of fragments) {
|
|
8
|
-
getUsedFragments(print(fragment.type), allFragments, results);
|
|
9
|
-
}
|
|
10
|
-
results.push(...fragments);
|
|
11
|
-
return results;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
module.exports = { getUsedFragments };
|
package/src/render.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
const { print } = require('graphql/index');
|
|
2
|
-
const { getUsedFragments } = require('./query');
|
|
3
|
-
const { GraphQLInputObjectType, GraphQLEnumType } = require('graphql/type');
|
|
4
|
-
const { get } = require('axios');
|
|
5
|
-
const { capitalize } = require('./utils');
|
|
6
|
-
|
|
7
|
-
const getName = (name, type, config) => {
|
|
8
|
-
const suffix = config.suffix ? config.suffix[type] || '' : '';
|
|
9
|
-
return `${name}${suffix}`;
|
|
10
|
-
};
|
|
11
|
-
const renderType = ({ name, fields, union, isList, isNullable, gqlType }, config) => {
|
|
12
|
-
let tsType = '';
|
|
13
|
-
if (union && union.length) {
|
|
14
|
-
tsType += [...union.map(u => getName(u, 'fragment', config)), ''].join(' & ');
|
|
15
|
-
}
|
|
16
|
-
tsType += `{${renderTypeField(fields, config)}}`;
|
|
17
|
-
tsType = `(${tsType})`;
|
|
18
|
-
if (isList) {
|
|
19
|
-
tsType += '[]';
|
|
20
|
-
}
|
|
21
|
-
if (isNullable) {
|
|
22
|
-
tsType = `Nullable<${tsType}>`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return `export type ${gqlType ? getName(name, gqlType, config) : name} = ${tsType}`;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const renderHeader = text => `/** \n ${text} \n **/`;
|
|
29
|
-
const renderTypeField = (fields, config) => {
|
|
30
|
-
return fields
|
|
31
|
-
.map(({ isList, isNullable, typeName, name, fields, isScalar, union, type, inLine, gqlType, alias }) => {
|
|
32
|
-
let tsType = '';
|
|
33
|
-
if (union && union.length) {
|
|
34
|
-
tsType += [...union.map(u => getName(u, 'fragment', config)), ''].join(' & ');
|
|
35
|
-
}
|
|
36
|
-
if (isScalar) {
|
|
37
|
-
tsType += getScalarTsType(typeName);
|
|
38
|
-
} else if (inLine) {
|
|
39
|
-
tsType += gqlType ? getName(typeName, gqlType, config) : typeName;
|
|
40
|
-
} else {
|
|
41
|
-
if (type instanceof GraphQLInputObjectType || type instanceof GraphQLEnumType) {
|
|
42
|
-
tsType += gqlType ? getName(typeName, gqlType, config) : typeName;
|
|
43
|
-
} else {
|
|
44
|
-
tsType += `{${renderTypeField(fields, config)}}`;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
tsType = `(${tsType})`;
|
|
48
|
-
if (isList) {
|
|
49
|
-
tsType += '[]';
|
|
50
|
-
}
|
|
51
|
-
if (isNullable) {
|
|
52
|
-
tsType = `Nullable<${tsType}>`;
|
|
53
|
-
}
|
|
54
|
-
return `${alias || name}${isNullable ? '?' : ''}: ${tsType}`;
|
|
55
|
-
})
|
|
56
|
-
.join(',\n');
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const renderSdk = functions => {
|
|
60
|
-
let str = '{';
|
|
61
|
-
for (let func of functions) {
|
|
62
|
-
str += `${func.name}: ${renderFunction(func)},\n`;
|
|
63
|
-
}
|
|
64
|
-
str += '}';
|
|
65
|
-
return `export const getSdk = (client: AxiosInstance) => (${str})`;
|
|
66
|
-
};
|
|
67
|
-
const renderFunction = ({ name, variables, results, chain }) => {
|
|
68
|
-
const chainStr = chain.map(f => `.then(${f})`).join('');
|
|
69
|
-
return `(variables: ${variables.name}, config?: AxiosRequestConfig): Promise<${results.name}> => {
|
|
70
|
-
const body = {variables, query: ${name}RawQuery};
|
|
71
|
-
return client.post("", body, config).then(handleResponse)${chainStr}
|
|
72
|
-
}`;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const renderQuery = ({ name, ast, allFragments }) => {
|
|
76
|
-
const raw = print(ast)
|
|
77
|
-
.replace(/@firstOrFail/g, '')
|
|
78
|
-
.replace(/@first/g, '')
|
|
79
|
-
.replace(/@nonNullable/g, '')
|
|
80
|
-
.replace(/@singleResult/g, '');
|
|
81
|
-
|
|
82
|
-
let fragments = getUsedFragments(raw, allFragments);
|
|
83
|
-
fragments = [...new Set(fragments)].map(f => `\${${f.name}FragmentQuery}`);
|
|
84
|
-
|
|
85
|
-
const gql = fragments.join('\n') + '\n' + raw;
|
|
86
|
-
return `const ${name}RawQuery = \`${gql}\`;`;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const getScalarTsType = name => `Scalar["${name}"]`;
|
|
90
|
-
|
|
91
|
-
const renderEnum = (e, config) => {
|
|
92
|
-
let str = `export enum ${getName(e.name, 'enum', config)} {`;
|
|
93
|
-
for (let { name, value } of e.values) {
|
|
94
|
-
str += `${capitalize(name.toLowerCase())} = "${value}",`;
|
|
95
|
-
}
|
|
96
|
-
str += `};`;
|
|
97
|
-
return str;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const renderFragment = fragment => {
|
|
101
|
-
return `export const ${fragment.name}FragmentQuery = \`${print(fragment.type)}\`;`;
|
|
102
|
-
};
|
|
103
|
-
const renderScalars = (scalars, config = {}) => {
|
|
104
|
-
const map = {
|
|
105
|
-
String: 'string',
|
|
106
|
-
Boolean: 'boolean',
|
|
107
|
-
Int: 'number',
|
|
108
|
-
Float: 'number',
|
|
109
|
-
...(config.scalars || {}),
|
|
110
|
-
};
|
|
111
|
-
return `export type Scalar = {${scalars
|
|
112
|
-
.map(({ name }) => {
|
|
113
|
-
const type = map[name] || 'string';
|
|
114
|
-
return `${name}: ${type}`;
|
|
115
|
-
})
|
|
116
|
-
.join(',')}};`;
|
|
117
|
-
};
|
|
118
|
-
module.exports = { renderType, renderQuery, renderSdk, renderScalars, renderEnum, renderHeader, renderFragment };
|