@devticon-os/graphql-codegen-axios 0.0.4
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/LICENSE +21 -0
- package/package.json +19 -0
- package/src/helpers.ts +45 -0
- package/src/index.js +100 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 devticon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@devticon-os/graphql-codegen-axios",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"axios": "^1.3.3",
|
|
14
|
+
"graphql": "^16.6.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"prettier": "^2.8.4"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AxiosResponse, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { GraphQLError } from 'graphql';
|
|
3
|
+
|
|
4
|
+
type GraphqlResponse<T> = {
|
|
5
|
+
data: T;
|
|
6
|
+
errors?: GraphQLError[];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const first = <T>(data: T[]) => data[0];
|
|
10
|
+
|
|
11
|
+
const firstOrFail = <T>(data: T[]) => {
|
|
12
|
+
const row = data[0];
|
|
13
|
+
if (!row) {
|
|
14
|
+
throw new Error('Not found');
|
|
15
|
+
}
|
|
16
|
+
return row;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const nonNullable = <T>(data: T) => {
|
|
20
|
+
const row = data;
|
|
21
|
+
if (!row) {
|
|
22
|
+
throw new Error('Not found');
|
|
23
|
+
}
|
|
24
|
+
return row;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const handleResponse = <T>({ data }: AxiosResponse<GraphqlResponse<T>>) => {
|
|
28
|
+
const errors = data.errors;
|
|
29
|
+
if (errors && errors.length > 0) {
|
|
30
|
+
throw new GraphqlError('Request failed', errors);
|
|
31
|
+
}
|
|
32
|
+
return data.data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const unpackSingleResults =
|
|
36
|
+
<T extends Object, K extends keyof T>(key: K) =>
|
|
37
|
+
(data: T) =>
|
|
38
|
+
data[key];
|
|
39
|
+
|
|
40
|
+
export class GraphqlError extends Error {
|
|
41
|
+
constructor(message: string, public gqlErrors: GraphQLError[]) {
|
|
42
|
+
const msg = `${message} ${gqlErrors.map(e => e.message).join('\n')}`;
|
|
43
|
+
super(msg);
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { print } = require('graphql');
|
|
4
|
+
|
|
5
|
+
const getType = (name, operationType, type) => {
|
|
6
|
+
name = name.charAt(0).toUpperCase() + name.slice(1);
|
|
7
|
+
operationType = operationType.charAt(0).toUpperCase() + operationType.slice(1);
|
|
8
|
+
switch (type) {
|
|
9
|
+
case 'variables':
|
|
10
|
+
return `${name}${operationType}Variables`;
|
|
11
|
+
case 'results':
|
|
12
|
+
return `${name}${operationType}`;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const functionsToString = functions => {
|
|
17
|
+
let str = '{';
|
|
18
|
+
for (let [name, body] of Object.entries(functions)) {
|
|
19
|
+
str += `${name}: ${body},\n`;
|
|
20
|
+
}
|
|
21
|
+
str += '}';
|
|
22
|
+
return str;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const getUsedFragments = (raw, allFragments, results = []) => {
|
|
26
|
+
const fragments = [...raw.matchAll(/\.\.\.(\w*)$/gm)]
|
|
27
|
+
.map(([_, name]) => name)
|
|
28
|
+
.map(name => allFragments.find(d => d.name.value === name));
|
|
29
|
+
|
|
30
|
+
for (let fragment of fragments) {
|
|
31
|
+
getUsedFragments(print(fragment), allFragments, results);
|
|
32
|
+
}
|
|
33
|
+
results.push(...fragments);
|
|
34
|
+
return results;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const printOperation = (ast, allFragments) => {
|
|
38
|
+
ast.directives = ast.directives.filter(d => !['first', 'firstOrFail'].includes(d.name.value));
|
|
39
|
+
const raw = print(ast).replace('@firstOrFail', '').replace('@first', '').replace('@nonNullable', '');
|
|
40
|
+
|
|
41
|
+
let fragments = getUsedFragments(raw, allFragments);
|
|
42
|
+
fragments = [...new Set(fragments)].map(f => print(f));
|
|
43
|
+
|
|
44
|
+
return fragments.join('\n') + '\n' + raw;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const helpers = fs.readFileSync(path.join(__dirname, 'helpers.ts'), 'utf-8');
|
|
48
|
+
|
|
49
|
+
module.exports = {
|
|
50
|
+
plugin(schema, documents, config) {
|
|
51
|
+
const functions = {};
|
|
52
|
+
const queries = [];
|
|
53
|
+
const fragments = [];
|
|
54
|
+
|
|
55
|
+
for (let { document } of documents) {
|
|
56
|
+
for (const definition of document.definitions) {
|
|
57
|
+
if (definition.kind === 'FragmentDefinition') {
|
|
58
|
+
fragments.push(definition);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (let { document } of documents) {
|
|
64
|
+
for (const definition of document.definitions) {
|
|
65
|
+
const name = definition.name.value;
|
|
66
|
+
|
|
67
|
+
if (definition.kind !== 'OperationDefinition') {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const variablesType = getType(name, definition.operation, 'variables');
|
|
71
|
+
const resultsType = getType(name, definition.operation, 'results');
|
|
72
|
+
|
|
73
|
+
queries.push(`const ${name}RawQuery = \`${printOperation(definition, fragments)}\`;`);
|
|
74
|
+
let func = `(variables: ${variablesType}, config?: AxiosRequestConfig) => client.post<GraphqlResponse<${resultsType}>>("", {variables, query: ${name}RawQuery}, config).then(handleResponse)`;
|
|
75
|
+
|
|
76
|
+
if (definition.selectionSet.selections.length === 1) {
|
|
77
|
+
if (!['query', 'mutation'].includes(definition.operation)) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const selection = definition.selectionSet.selections[0];
|
|
81
|
+
const directives = selection.directives.map(d => d.name.value);
|
|
82
|
+
const propertyName = selection.name.value;
|
|
83
|
+
func += `.then(unpackSingleResults("${propertyName}"))\n`;
|
|
84
|
+
for (let directive of directives) {
|
|
85
|
+
func += `.then(${directive})\n`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
functions[name] = func;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const sdk = `export const getSdk = (client: AxiosInstance) => (${functionsToString(functions)})`;
|
|
93
|
+
return [...queries, helpers, sdk].join('\n');
|
|
94
|
+
},
|
|
95
|
+
addToSchema: /* GraphQL */ `
|
|
96
|
+
directive @first on OBJECT
|
|
97
|
+
directive @firstOrFail on OBJECT
|
|
98
|
+
directive @nonNullable on OBJECT
|
|
99
|
+
`,
|
|
100
|
+
};
|