@avantstay/graphql-ts-client 10.5.1 → 10.6.0-rc.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/dist/endpoint.d.ts +9 -9
- package/dist/index.d.ts +21 -19
- package/dist/index.js +44 -26
- package/package.json +5 -3
- package/src/__snapshots__/generateTypescriptClient.test.ts.snap +133 -0
- package/src/__testClient.d.ts +1 -1
- package/src/__testClient.js +16 -24
- package/src/generateTypescriptClient.test.ts +10 -1
- package/src/generateTypescriptClient.ts +30 -9
- package/src/index.ts +1 -1
- package/dist/types-85e628f6.d.ts +0 -69
package/dist/endpoint.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { C as ClientConfig, I as IResponseListener, E as Endpoint } from './types-
|
|
1
|
+
import { C as ClientConfig, I as IResponseListener, E as Endpoint } from './types-4ecbabdf.js';
|
|
2
2
|
|
|
3
|
-
declare const getApiEndpointCreator: (apiConfig: {
|
|
4
|
-
getClient: () => ClientConfig;
|
|
5
|
-
responseListeners: IResponseListener[];
|
|
6
|
-
typesTree: any;
|
|
7
|
-
maxAge: number;
|
|
8
|
-
verbose: boolean;
|
|
9
|
-
formatGraphQL: any;
|
|
10
|
-
errorsParser?: ((errors: any[]) => any) | undefined;
|
|
3
|
+
declare const getApiEndpointCreator: (apiConfig: {
|
|
4
|
+
getClient: () => ClientConfig;
|
|
5
|
+
responseListeners: IResponseListener[];
|
|
6
|
+
typesTree: any;
|
|
7
|
+
maxAge: number;
|
|
8
|
+
verbose: boolean;
|
|
9
|
+
formatGraphQL: any;
|
|
10
|
+
errorsParser?: ((errors: any[]) => any) | undefined;
|
|
11
11
|
}) => <I = any, O = any, E = any>(kind: 'mutation' | 'query', queryName: string) => Endpoint<I, O, E>;
|
|
12
12
|
|
|
13
13
|
export { getApiEndpointCreator };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { PathLike } from 'fs';
|
|
2
|
-
export { C as ClientConfig, c as DeepReplace, D as Defined, E as Endpoint, G as GraphQLClientError, I as IResponseListener, J as JsonOutput, L as LogInfo, M as Maybe, P as Projection, d as RawEndpoint, b as Replacement, R as ResponseData, a as ResponseListenerInfo, U as Unpacked } from './types-
|
|
2
|
+
export { C as ClientConfig, c as DeepReplace, D as Defined, E as Endpoint, G as GraphQLClientError, I as IResponseListener, J as JsonOutput, L as LogInfo, M as Maybe, P as Projection, d as RawEndpoint, b as Replacement, R as ResponseData, a as ResponseListenerInfo, U as Unpacked } from './types-4ecbabdf.js';
|
|
3
3
|
|
|
4
|
-
type IClientOptions = {
|
|
5
|
-
output?: PathLike;
|
|
6
|
-
clientName?: string;
|
|
7
|
-
headers?: {
|
|
8
|
-
[key: string]: string;
|
|
9
|
-
};
|
|
10
|
-
introspectionEndpoint?: string;
|
|
11
|
-
endpoint: string;
|
|
12
|
-
verbose?: boolean;
|
|
13
|
-
formatGraphQL?: boolean;
|
|
14
|
-
skipCache?: boolean;
|
|
15
|
-
errorsParser?: (errors: any[]) => any;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
typings: string;
|
|
19
|
-
js: string;
|
|
20
|
-
}
|
|
4
|
+
type IClientOptions = {
|
|
5
|
+
output?: PathLike;
|
|
6
|
+
clientName?: string;
|
|
7
|
+
headers?: {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
};
|
|
10
|
+
introspectionEndpoint?: string;
|
|
11
|
+
endpoint: string;
|
|
12
|
+
verbose?: boolean;
|
|
13
|
+
formatGraphQL?: boolean;
|
|
14
|
+
skipCache?: boolean;
|
|
15
|
+
errorsParser?: (errors: any[]) => any;
|
|
16
|
+
};
|
|
17
|
+
type Client = {
|
|
18
|
+
typings: string;
|
|
19
|
+
js: string;
|
|
20
|
+
};
|
|
21
|
+
declare function generateTypescriptClient({ introspectionEndpoint, ...options }: IClientOptions): Promise<Client>;
|
|
22
|
+
declare function generateTypescriptClientFromSDL(SDL: string, options: IClientOptions): Client;
|
|
21
23
|
|
|
22
|
-
export { generateTypescriptClient };
|
|
24
|
+
export { generateTypescriptClient, generateTypescriptClientFromSDL };
|
package/dist/index.js
CHANGED
|
@@ -464,6 +464,9 @@ __export(src_exports, {
|
|
|
464
464
|
},
|
|
465
465
|
generateTypescriptClient: function() {
|
|
466
466
|
return generateTypescriptClient;
|
|
467
|
+
},
|
|
468
|
+
generateTypescriptClientFromSDL: function() {
|
|
469
|
+
return generateTypescriptClientFromSDL;
|
|
467
470
|
}
|
|
468
471
|
});
|
|
469
472
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -484,7 +487,7 @@ var prettier = __toESM(require("prettier"));
|
|
|
484
487
|
// package.json
|
|
485
488
|
var package_default = {
|
|
486
489
|
name: "@avantstay/graphql-ts-client",
|
|
487
|
-
version: "10.
|
|
490
|
+
version: "10.6.0-rc.1",
|
|
488
491
|
description: "GraphQL Typescript Client Generator",
|
|
489
492
|
author: "Wellington Guimaraes",
|
|
490
493
|
license: "MIT",
|
|
@@ -906,19 +909,43 @@ function _fetchIntrospection() {
|
|
|
906
909
|
});
|
|
907
910
|
return _fetchIntrospection.apply(this, arguments);
|
|
908
911
|
}
|
|
912
|
+
function generateClient(introspectionTypes, _param) {
|
|
913
|
+
var output = _param.output, restOptions = _objectWithoutProperties(_param, [
|
|
914
|
+
"output"
|
|
915
|
+
]);
|
|
916
|
+
var _generateClientCode = generateClientCode(introspectionTypes, restOptions), js = _generateClientCode.js, typings = _generateClientCode.typings;
|
|
917
|
+
if (output && typeof output === "string") {
|
|
918
|
+
var outputDir = import_path.default.dirname(output);
|
|
919
|
+
if (!fs.existsSync(outputDir)) {
|
|
920
|
+
fs.mkdirSync(outputDir, {
|
|
921
|
+
recursive: true
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".d.ts"), typings, {
|
|
925
|
+
encoding: "utf8"
|
|
926
|
+
});
|
|
927
|
+
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".js"), js, {
|
|
928
|
+
encoding: "utf8"
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
return {
|
|
932
|
+
js: js,
|
|
933
|
+
typings: typings
|
|
934
|
+
};
|
|
935
|
+
}
|
|
909
936
|
function generateTypescriptClient(_param) {
|
|
910
937
|
return _generateTypescriptClient.apply(this, arguments);
|
|
911
938
|
}
|
|
912
939
|
function _generateTypescriptClient() {
|
|
913
940
|
_generateTypescriptClient = _asyncToGenerator(function(_param) {
|
|
914
|
-
var introspectionEndpoint,
|
|
941
|
+
var introspectionEndpoint, options, _options_clientName, introspectionTypes;
|
|
915
942
|
return __generator(this, function(_state) {
|
|
916
943
|
switch(_state.label){
|
|
917
944
|
case 0:
|
|
918
|
-
introspectionEndpoint = _param.introspectionEndpoint,
|
|
919
|
-
"introspectionEndpoint"
|
|
920
|
-
"output"
|
|
945
|
+
introspectionEndpoint = _param.introspectionEndpoint, options = _objectWithoutProperties(_param, [
|
|
946
|
+
"introspectionEndpoint"
|
|
921
947
|
]);
|
|
948
|
+
console.log("Generating TypeScript client (name: ".concat((_options_clientName = options.clientName) !== null && _options_clientName !== void 0 ? _options_clientName : "n/a", ")"));
|
|
922
949
|
(0, import_axios_retry.default)(import_axios.default, {
|
|
923
950
|
retries: 5,
|
|
924
951
|
retryDelay: function(retryCount) {
|
|
@@ -932,34 +959,24 @@ function _generateTypescriptClient() {
|
|
|
932
959
|
}))
|
|
933
960
|
];
|
|
934
961
|
case 1:
|
|
935
|
-
|
|
936
|
-
_generateClientCode = generateClientCode(types, options), js = _generateClientCode.js, typings = _generateClientCode.typings;
|
|
937
|
-
if (output && typeof output === "string") {
|
|
938
|
-
outputDir = import_path.default.dirname(output);
|
|
939
|
-
if (!fs.existsSync(outputDir)) {
|
|
940
|
-
fs.mkdirSync(outputDir, {
|
|
941
|
-
recursive: true
|
|
942
|
-
});
|
|
943
|
-
}
|
|
944
|
-
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".d.ts"), typings, {
|
|
945
|
-
encoding: "utf8"
|
|
946
|
-
});
|
|
947
|
-
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".js"), js, {
|
|
948
|
-
encoding: "utf8"
|
|
949
|
-
});
|
|
950
|
-
}
|
|
962
|
+
introspectionTypes = _state.sent();
|
|
951
963
|
return [
|
|
952
964
|
2,
|
|
953
|
-
|
|
954
|
-
js: js,
|
|
955
|
-
typings: typings
|
|
956
|
-
}
|
|
965
|
+
generateClient(introspectionTypes, options)
|
|
957
966
|
];
|
|
958
967
|
}
|
|
959
968
|
});
|
|
960
969
|
});
|
|
961
970
|
return _generateTypescriptClient.apply(this, arguments);
|
|
962
971
|
}
|
|
972
|
+
function generateTypescriptClientFromSDL(SDL, options) {
|
|
973
|
+
var _data;
|
|
974
|
+
var _options_clientName;
|
|
975
|
+
console.log("Generating TypeScript client from SDL (name: ".concat((_options_clientName = options.clientName) !== null && _options_clientName !== void 0 ? _options_clientName : "n/a", ")"));
|
|
976
|
+
var graphqlSchemaObj = (0, import_graphql.buildSchema)(SDL);
|
|
977
|
+
var introspectionTypes = (_data = (0, import_graphql.graphqlSync)(graphqlSchemaObj, new import_graphql.Source((0, import_graphql.getIntrospectionQuery)())).data) === null || _data === void 0 ? void 0 : _data.__schema.types;
|
|
978
|
+
return generateClient(introspectionTypes, options);
|
|
979
|
+
}
|
|
963
980
|
// src/types.ts
|
|
964
981
|
var GraphQLClientError = /*#__PURE__*/ function(Error1) {
|
|
965
982
|
_inherits(GraphQLClientError1, Error1);
|
|
@@ -993,5 +1010,6 @@ var GraphQLClientError = /*#__PURE__*/ function(Error1) {
|
|
|
993
1010
|
// Annotate the CommonJS export names for ESM import in node:
|
|
994
1011
|
0 && (module.exports = {
|
|
995
1012
|
GraphQLClientError: GraphQLClientError,
|
|
996
|
-
generateTypescriptClient: generateTypescriptClient
|
|
1013
|
+
generateTypescriptClient: generateTypescriptClient,
|
|
1014
|
+
generateTypescriptClientFromSDL: generateTypescriptClientFromSDL
|
|
997
1015
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avantstay/graphql-ts-client",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.6.0-rc.1",
|
|
4
4
|
"description": "GraphQL Typescript Client Generator",
|
|
5
5
|
"author": "Wellington Guimaraes",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,8 +8,7 @@
|
|
|
8
8
|
"typings": "dist/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsup src/index.ts src/endpoint.ts --dts",
|
|
11
|
-
"test": "cross-env GQL_CLIENT_DIST_PATH='.' jest --watch"
|
|
12
|
-
"prepublishOnly": "yarn build"
|
|
11
|
+
"test": "cross-env GQL_CLIENT_DIST_PATH='.' jest --watch"
|
|
13
12
|
},
|
|
14
13
|
"files": [
|
|
15
14
|
"dist",
|
|
@@ -86,5 +85,8 @@
|
|
|
86
85
|
"tslib": "^2.3.1",
|
|
87
86
|
"tsup": "^6.5.0",
|
|
88
87
|
"typescript": "^4.9.4"
|
|
88
|
+
},
|
|
89
|
+
"publishConfig": {
|
|
90
|
+
"@avantstay:registry": "https://registry.npmjs.org/"
|
|
89
91
|
}
|
|
90
92
|
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`Generated Client should generate proper code from SDL 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"js": "var __create = Object.create;
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
8
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
__markAsModule(target);
|
|
14
|
+
for (var name in all)
|
|
15
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, module2, desc) => {
|
|
18
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(module2))
|
|
20
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
21
|
+
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return target;
|
|
24
|
+
};
|
|
25
|
+
var __toModule = (module2) => {
|
|
26
|
+
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
27
|
+
};
|
|
28
|
+
__export(exports, {
|
|
29
|
+
client: () => client,
|
|
30
|
+
default: () => stdin_default
|
|
31
|
+
});
|
|
32
|
+
var import_endpoint = __toModule(require("../dist/endpoint"));
|
|
33
|
+
const formatGraphQL = (query) => query;
|
|
34
|
+
const typesTree = {};
|
|
35
|
+
let verbose = false;
|
|
36
|
+
let headers = {};
|
|
37
|
+
let url = "https://sample.endpoint.com/graphl";
|
|
38
|
+
let retryConfig = {
|
|
39
|
+
max: 0,
|
|
40
|
+
before: void 0,
|
|
41
|
+
waitBeforeRetry: 0
|
|
42
|
+
};
|
|
43
|
+
let responseListeners = [];
|
|
44
|
+
let errorsParser = void 0;
|
|
45
|
+
let apiEndpoint = (0, import_endpoint.getApiEndpointCreator)({
|
|
46
|
+
getClient: () => ({ url, headers, retryConfig }),
|
|
47
|
+
responseListeners,
|
|
48
|
+
maxAge: 3e4,
|
|
49
|
+
verbose,
|
|
50
|
+
typesTree,
|
|
51
|
+
formatGraphQL,
|
|
52
|
+
errorsParser
|
|
53
|
+
});
|
|
54
|
+
const client = {
|
|
55
|
+
addResponseListener: (listener) => responseListeners.push(listener),
|
|
56
|
+
setHeader: (key, value) => {
|
|
57
|
+
headers[key] = value;
|
|
58
|
+
},
|
|
59
|
+
setHeaders: (newHeaders) => {
|
|
60
|
+
headers = newHeaders;
|
|
61
|
+
},
|
|
62
|
+
setRetryConfig: (options) => {
|
|
63
|
+
if (!Number.isInteger(options.max) || options.max < 0) {
|
|
64
|
+
throw new Error("retryOptions.max should be a non-negative integer");
|
|
65
|
+
}
|
|
66
|
+
retryConfig = {
|
|
67
|
+
max: options.max,
|
|
68
|
+
waitBeforeRetry: options.waitBeforeRetry,
|
|
69
|
+
before: options.before
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
setUrl: (_url) => url = _url,
|
|
73
|
+
queries: {
|
|
74
|
+
hello: apiEndpoint("query", "hello")
|
|
75
|
+
},
|
|
76
|
+
mutations: {}
|
|
77
|
+
};
|
|
78
|
+
var stdin_default = client;
|
|
79
|
+
",
|
|
80
|
+
"typings": "// noinspection TypeScriptUnresolvedVariable, ES6UnusedImports, JSUnusedLocalSymbols, TypeScriptCheckImport
|
|
81
|
+
import { DeepRequired } from "ts-essentials"
|
|
82
|
+
import { Maybe, IResponseListener, Endpoint } from "../dist"
|
|
83
|
+
|
|
84
|
+
// Scalars
|
|
85
|
+
export type IDate = string | Date
|
|
86
|
+
|
|
87
|
+
// Enums
|
|
88
|
+
|
|
89
|
+
type AllEnums = never
|
|
90
|
+
|
|
91
|
+
// Args
|
|
92
|
+
export interface HelloArgs {}
|
|
93
|
+
|
|
94
|
+
// Input/Output Types
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated Avoid directly using this interface. Instead, create a type alias based on the query/mutation return type.
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
export interface Query {
|
|
101
|
+
hello?: string
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Selection Types
|
|
105
|
+
|
|
106
|
+
export declare const client: {
|
|
107
|
+
addResponseListener: (listener: IResponseListener) => void
|
|
108
|
+
setHeader: (key: string, value: string) => void
|
|
109
|
+
setHeaders: (newHeaders: { [k: string]: string }) => void
|
|
110
|
+
setUrl: (url: string) => void
|
|
111
|
+
setRetryConfig: (options: {
|
|
112
|
+
max: number
|
|
113
|
+
waitBeforeRetry?: number
|
|
114
|
+
before?: IResponseListener
|
|
115
|
+
}) => void
|
|
116
|
+
queries: {
|
|
117
|
+
hello: Endpoint<
|
|
118
|
+
{
|
|
119
|
+
__headers?: { [key: string]: string }
|
|
120
|
+
__retry?: boolean
|
|
121
|
+
__alias?: string
|
|
122
|
+
},
|
|
123
|
+
string,
|
|
124
|
+
AllEnums
|
|
125
|
+
>
|
|
126
|
+
}
|
|
127
|
+
mutations: {}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export default client
|
|
131
|
+
",
|
|
132
|
+
}
|
|
133
|
+
`;
|
package/src/__testClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// noinspection TypeScriptUnresolvedVariable, ES6UnusedImports, JSUnusedLocalSymbols, TypeScriptCheckImport
|
|
2
2
|
import { DeepRequired } from "ts-essentials"
|
|
3
|
-
import { Maybe, IResponseListener, Endpoint } from "
|
|
3
|
+
import { Maybe, IResponseListener, Endpoint } from "../dist"
|
|
4
4
|
|
|
5
5
|
// Scalars
|
|
6
6
|
export type IDate = string | Date
|
package/src/__testClient.js
CHANGED
|
@@ -4,37 +4,31 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
7
8
|
var __export = (target, all) => {
|
|
9
|
+
__markAsModule(target);
|
|
8
10
|
for (var name in all)
|
|
9
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
12
|
};
|
|
11
|
-
var
|
|
12
|
-
if (
|
|
13
|
-
for (let key of __getOwnPropNames(
|
|
14
|
-
if (!__hasOwnProp.call(
|
|
15
|
-
__defProp(
|
|
13
|
+
var __reExport = (target, module2, desc) => {
|
|
14
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(module2))
|
|
16
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
17
|
+
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
16
18
|
}
|
|
17
|
-
return
|
|
19
|
+
return target;
|
|
18
20
|
};
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var stdin_exports = {};
|
|
29
|
-
__export(stdin_exports, {
|
|
21
|
+
var __toModule = (module2) => {
|
|
22
|
+
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
23
|
+
};
|
|
24
|
+
__export(exports, {
|
|
30
25
|
BookType: () => BookType,
|
|
31
26
|
default: () => stdin_default,
|
|
32
27
|
myApiClient: () => myApiClient
|
|
33
28
|
});
|
|
34
|
-
|
|
35
|
-
var
|
|
36
|
-
var
|
|
37
|
-
var import_parser_graphql = __toESM(require("prettier/parser-graphql"));
|
|
29
|
+
var import_endpoint = __toModule(require("../dist/endpoint"));
|
|
30
|
+
var import_standalone = __toModule(require("prettier/standalone"));
|
|
31
|
+
var import_parser_graphql = __toModule(require("prettier/parser-graphql"));
|
|
38
32
|
const formatGraphQL = (query) => (0, import_standalone.format)(query, { parser: "graphql", plugins: [import_parser_graphql.default] });
|
|
39
33
|
const BookType = {
|
|
40
34
|
dolor: "DOLOR",
|
|
@@ -87,9 +81,7 @@ let apiEndpoint = (0, import_endpoint.getApiEndpointCreator)({
|
|
|
87
81
|
errorsParser
|
|
88
82
|
});
|
|
89
83
|
const myApiClient = {
|
|
90
|
-
addResponseListener: (listener) => responseListeners.push(
|
|
91
|
-
listener
|
|
92
|
-
),
|
|
84
|
+
addResponseListener: (listener) => responseListeners.push(listener),
|
|
93
85
|
setHeader: (key, value) => {
|
|
94
86
|
headers[key] = value;
|
|
95
87
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ApolloServer } from 'apollo-server'
|
|
2
2
|
import * as path from 'path'
|
|
3
|
-
import { generateTypescriptClient } from './generateTypescriptClient'
|
|
3
|
+
import { generateTypescriptClient, generateTypescriptClientFromSDL } from './generateTypescriptClient'
|
|
4
4
|
import { startServer } from './testServer'
|
|
5
5
|
import { GraphQLClientError, ResponseListenerInfo } from './types'
|
|
6
6
|
|
|
@@ -124,4 +124,13 @@ describe('Generated Client', () => {
|
|
|
124
124
|
expect(responseData?.queryName).toBe('failingQuery')
|
|
125
125
|
expect(responseData?.response.errors.length).toBeGreaterThan(0)
|
|
126
126
|
})
|
|
127
|
+
|
|
128
|
+
it('should generate proper code from SDL', ()=>{
|
|
129
|
+
const sdlString = `
|
|
130
|
+
type Query {
|
|
131
|
+
hello: String
|
|
132
|
+
}
|
|
133
|
+
`;
|
|
134
|
+
expect(generateTypescriptClientFromSDL(sdlString, {endpoint: 'https://sample.endpoint.com/graphl'})).toMatchSnapshot()
|
|
135
|
+
})
|
|
127
136
|
})
|
|
@@ -5,6 +5,8 @@ import * as esbuild from 'esbuild'
|
|
|
5
5
|
import * as fs from 'fs'
|
|
6
6
|
import { PathLike } from 'fs'
|
|
7
7
|
import {
|
|
8
|
+
buildSchema,
|
|
9
|
+
graphqlSync,
|
|
8
10
|
getIntrospectionQuery,
|
|
9
11
|
IntrospectionEnumType,
|
|
10
12
|
IntrospectionField,
|
|
@@ -13,6 +15,7 @@ import {
|
|
|
13
15
|
IntrospectionObjectType,
|
|
14
16
|
IntrospectionOutputTypeRef,
|
|
15
17
|
IntrospectionType,
|
|
18
|
+
Source,
|
|
16
19
|
} from 'graphql'
|
|
17
20
|
import { kebabCase } from 'lodash'
|
|
18
21
|
import orderBy from 'lodash/orderBy'
|
|
@@ -479,7 +482,7 @@ function generateClientCode(types: ReadonlyArray<IntrospectionType>, options: Om
|
|
|
479
482
|
return output
|
|
480
483
|
}
|
|
481
484
|
|
|
482
|
-
async function fetchIntrospection({ endpoint, headers }: FetchIntrospectionOptions) {
|
|
485
|
+
async function fetchIntrospection({ endpoint, headers }: FetchIntrospectionOptions): Promise<ReadonlyArray<IntrospectionType>> {
|
|
483
486
|
const introspectionCacheFileName = `gql-ts-client__introspection__${kebabCase(endpoint)}.json`
|
|
484
487
|
const introspectionCacheFilePath = path.resolve(tempDir, introspectionCacheFileName)
|
|
485
488
|
|
|
@@ -522,15 +525,10 @@ async function fetchIntrospection({ endpoint, headers }: FetchIntrospectionOptio
|
|
|
522
525
|
return types
|
|
523
526
|
}
|
|
524
527
|
|
|
525
|
-
|
|
526
|
-
axiosRetry(axios, { retries: 5, retryDelay: retryCount => 1000 * 2 ** retryCount })
|
|
527
|
-
|
|
528
|
-
const types = await fetchIntrospection({
|
|
529
|
-
...options,
|
|
530
|
-
endpoint: introspectionEndpoint || options.endpoint,
|
|
531
|
-
})
|
|
528
|
+
type Client = { typings: string; js: string }
|
|
532
529
|
|
|
533
|
-
|
|
530
|
+
function generateClient(introspectionTypes: ReadonlyArray<IntrospectionType>, {output,...restOptions}: IClientOptions): Client {
|
|
531
|
+
const { js, typings } = generateClientCode(introspectionTypes, restOptions)
|
|
534
532
|
|
|
535
533
|
if (output && typeof output === 'string') {
|
|
536
534
|
const outputDir = path.dirname(output)
|
|
@@ -545,3 +543,26 @@ export async function generateTypescriptClient({ introspectionEndpoint, output,
|
|
|
545
543
|
|
|
546
544
|
return { js, typings }
|
|
547
545
|
}
|
|
546
|
+
|
|
547
|
+
export async function generateTypescriptClient({ introspectionEndpoint, ...options }: IClientOptions): Promise<Client> {
|
|
548
|
+
console.log(`Generating TypeScript client (name: ${options.clientName ?? 'n/a'})`)
|
|
549
|
+
|
|
550
|
+
axiosRetry(axios, { retries: 5, retryDelay: retryCount => 1000 * 2 ** retryCount })
|
|
551
|
+
|
|
552
|
+
const introspectionTypes = await fetchIntrospection({
|
|
553
|
+
...options,
|
|
554
|
+
endpoint: introspectionEndpoint || options.endpoint,
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
return generateClient(introspectionTypes, options)
|
|
558
|
+
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export function generateTypescriptClientFromSDL(SDL: string, options: IClientOptions): Client {
|
|
562
|
+
console.log(`Generating TypeScript client from SDL (name: ${options.clientName ?? 'n/a'})`);
|
|
563
|
+
|
|
564
|
+
const graphqlSchemaObj = buildSchema(SDL);
|
|
565
|
+
const introspectionTypes = graphqlSync(graphqlSchemaObj, new Source(getIntrospectionQuery())).data?.__schema.types as IntrospectionType[]
|
|
566
|
+
|
|
567
|
+
return generateClient(introspectionTypes, options)
|
|
568
|
+
}
|
package/src/index.ts
CHANGED
package/dist/types-85e628f6.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
type Maybe<T> = null | undefined | T;
|
|
2
|
-
type Defined<T> = Exclude<T, undefined>;
|
|
3
|
-
type ResponseData = {
|
|
4
|
-
data: any;
|
|
5
|
-
warnings: any;
|
|
6
|
-
headers: any;
|
|
7
|
-
status?: number;
|
|
8
|
-
errors: {
|
|
9
|
-
message: string;
|
|
10
|
-
}[];
|
|
11
|
-
};
|
|
12
|
-
type ResponseListenerInfo = {
|
|
13
|
-
queryName: string;
|
|
14
|
-
query: string;
|
|
15
|
-
variables: any;
|
|
16
|
-
response: ResponseData;
|
|
17
|
-
};
|
|
18
|
-
type IResponseListener = (info: ResponseListenerInfo) => void | Promise<void>;
|
|
19
|
-
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
20
|
-
type Projection<Selection, Base, E = never> = Base extends Array<any> ? ArrayElement<Base> extends Date | string | number | boolean | null | undefined | E ? ArrayElement<Base>[] : Projection<Defined<Selection>, ArrayElement<Base>, E>[] : Base extends Date | string | number | boolean | null | E ? Selection extends undefined ? Base | undefined : Base : {
|
|
21
|
-
[k in keyof Selection & keyof Base]: Selection[k] extends boolean ? Base[k] : Base[k] extends Array<infer A> ? Projection<Defined<Selection[k]>, A, E>[] : Projection<Defined<Selection[k]>, Base[k], E>;
|
|
22
|
-
};
|
|
23
|
-
type Unpacked<T> = T extends (infer U)[] ? U : T extends (...args: any[]) => infer U ? U : T extends Promise<infer U> ? U : T;
|
|
24
|
-
type Replacement<M extends [any, any], T> = M extends any ? ([T] extends [M[0]] ? M[1] : never) : never;
|
|
25
|
-
type DeepReplace<T, Ignore, M extends [any, any]> = {
|
|
26
|
-
[P in keyof T]: T[P] extends M[0] ? T[P] extends Ignore ? T[P] extends object ? DeepReplace<T[P], Ignore, M> : T[P] : Replacement<M, T[P]> : T[P] extends object ? DeepReplace<T[P], Ignore, M> : T[P];
|
|
27
|
-
};
|
|
28
|
-
type RawEndpoint<I, O, E> = <S extends I>(jsonQuery?: S) => Promise<{
|
|
29
|
-
data: Projection<S, O, E>;
|
|
30
|
-
errors: any[];
|
|
31
|
-
warnings: any[];
|
|
32
|
-
headers: any;
|
|
33
|
-
status: any;
|
|
34
|
-
}>;
|
|
35
|
-
type JsonOutput<O, ToBeIgnored> = DeepReplace<O, ToBeIgnored, [string | Date, string]>;
|
|
36
|
-
type Endpoint<I, O, E> = (<S extends I>(jsonQuery?: S) => Promise<Projection<S, JsonOutput<O, E>, E>>) & {
|
|
37
|
-
memo: <S extends I>(jsonQuery?: S) => Promise<Projection<S, JsonOutput<O, E>, E>>;
|
|
38
|
-
memoRaw: RawEndpoint<I, JsonOutput<O, E>, E>;
|
|
39
|
-
raw: RawEndpoint<I, JsonOutput<O, E>, E>;
|
|
40
|
-
};
|
|
41
|
-
type ClientConfig = {
|
|
42
|
-
url: string;
|
|
43
|
-
headers: {
|
|
44
|
-
[key: string]: string;
|
|
45
|
-
};
|
|
46
|
-
retryConfig: {
|
|
47
|
-
max: number;
|
|
48
|
-
waitBeforeRetry?: number;
|
|
49
|
-
before: IResponseListener;
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
type LogInfo = {
|
|
53
|
-
query: string;
|
|
54
|
-
variables: any;
|
|
55
|
-
formatGraphQL: any;
|
|
56
|
-
kind: string;
|
|
57
|
-
queryName: string;
|
|
58
|
-
response?: any;
|
|
59
|
-
error?: Error;
|
|
60
|
-
duration: number;
|
|
61
|
-
};
|
|
62
|
-
declare class GraphQLClientError extends Error {
|
|
63
|
-
responseData: ResponseData;
|
|
64
|
-
constructor(responseData: ResponseData);
|
|
65
|
-
get message(): string;
|
|
66
|
-
get response(): ResponseData;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export { ClientConfig as C, Defined as D, Endpoint as E, GraphQLClientError as G, IResponseListener as I, JsonOutput as J, LogInfo as L, Maybe as M, Projection as P, ResponseData as R, Unpacked as U, ResponseListenerInfo as a, Replacement as b, DeepReplace as c, RawEndpoint as d };
|