@graphql-codegen/typescript-react-query 3.5.14-alpha-7a65ba05d.0 → 3.6.0-alpha-29eb1293b.0
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/cjs/config.js +2 -0
- package/cjs/fetcher-custom-mapper.js +108 -0
- package/cjs/fetcher-fetch-hardcoded.js +108 -0
- package/cjs/fetcher-fetch.js +96 -0
- package/cjs/fetcher-graphql-request.js +92 -0
- package/cjs/fetcher.js +2 -0
- package/cjs/index.js +39 -0
- package/cjs/package.json +1 -0
- package/cjs/variables-generator.js +37 -0
- package/cjs/visitor.js +143 -0
- package/esm/config.js +1 -0
- package/esm/fetcher-custom-mapper.js +104 -0
- package/esm/fetcher-fetch-hardcoded.js +104 -0
- package/esm/fetcher-fetch.js +92 -0
- package/esm/fetcher-graphql-request.js +88 -0
- package/esm/fetcher.js +1 -0
- package/esm/index.js +34 -0
- package/esm/variables-generator.js +27 -0
- package/esm/visitor.js +138 -0
- package/package.json +22 -15
- package/{config.d.ts → typings/config.d.ts} +0 -0
- package/{fetcher-custom-mapper.d.ts → typings/fetcher-custom-mapper.d.ts} +3 -3
- package/{fetcher-fetch-hardcoded.d.ts → typings/fetcher-fetch-hardcoded.d.ts} +3 -3
- package/{fetcher-fetch.d.ts → typings/fetcher-fetch.d.ts} +2 -2
- package/{fetcher-graphql-request.d.ts → typings/fetcher-graphql-request.d.ts} +2 -2
- package/{fetcher.d.ts → typings/fetcher.d.ts} +0 -0
- package/{index.d.ts → typings/index.d.ts} +2 -2
- package/{variables-generator.d.ts → typings/variables-generator.d.ts} +0 -0
- package/{visitor.d.ts → typings/visitor.d.ts} +2 -2
- package/index.js +0 -592
- package/index.mjs +0 -584
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CustomFetch } from './config';
|
|
2
|
-
import { FetcherRenderer } from './fetcher';
|
|
1
|
+
import { CustomFetch } from './config.js';
|
|
2
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
3
3
|
import { OperationDefinitionNode } from 'graphql';
|
|
4
|
-
import { ReactQueryVisitor } from './visitor';
|
|
4
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
5
5
|
export declare class CustomMapperFetcher implements FetcherRenderer {
|
|
6
6
|
private visitor;
|
|
7
7
|
private _mapper;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { FetcherRenderer } from './fetcher';
|
|
2
|
-
import { HardcodedFetch } from './config';
|
|
1
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
2
|
+
import { HardcodedFetch } from './config.js';
|
|
3
3
|
import { OperationDefinitionNode } from 'graphql';
|
|
4
|
-
import { ReactQueryVisitor } from './visitor';
|
|
4
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
5
5
|
export declare class HardcodedFetchFetcher implements FetcherRenderer {
|
|
6
6
|
private visitor;
|
|
7
7
|
private config;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { FetcherRenderer } from './fetcher';
|
|
1
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
2
2
|
import { OperationDefinitionNode } from 'graphql';
|
|
3
|
-
import { ReactQueryVisitor } from './visitor';
|
|
3
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
4
4
|
export declare class FetchFetcher implements FetcherRenderer {
|
|
5
5
|
private visitor;
|
|
6
6
|
constructor(visitor: ReactQueryVisitor);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { FetcherRenderer } from './fetcher';
|
|
1
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
2
2
|
import { OperationDefinitionNode } from 'graphql';
|
|
3
|
-
import { ReactQueryVisitor } from './visitor';
|
|
3
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
4
4
|
export declare class GraphQLRequestClientFetcher implements FetcherRenderer {
|
|
5
5
|
private visitor;
|
|
6
6
|
constructor(visitor: ReactQueryVisitor);
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PluginFunction, PluginValidateFn, Types } from '@graphql-codegen/plugin-helpers';
|
|
2
|
-
import { ReactQueryRawPluginConfig } from './config';
|
|
3
|
-
import { ReactQueryVisitor } from './visitor';
|
|
2
|
+
import { ReactQueryRawPluginConfig } from './config.js';
|
|
3
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
4
4
|
export declare const plugin: PluginFunction<ReactQueryRawPluginConfig, Types.ComplexPluginOutput>;
|
|
5
5
|
export declare const validate: PluginValidateFn<any>;
|
|
6
6
|
export { ReactQueryVisitor };
|
|
File without changes
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ClientSideBasePluginConfig, ClientSideBaseVisitor, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
|
|
2
2
|
import { GraphQLSchema, OperationDefinitionNode } from 'graphql';
|
|
3
|
-
import { FetcherRenderer } from './fetcher';
|
|
4
|
-
import { ReactQueryRawPluginConfig } from './config';
|
|
3
|
+
import { FetcherRenderer } from './fetcher.js';
|
|
4
|
+
import { ReactQueryRawPluginConfig } from './config.js';
|
|
5
5
|
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
6
6
|
export interface ReactQueryPluginConfig extends ClientSideBasePluginConfig {
|
|
7
7
|
errorType: string;
|
package/index.js
DELETED
|
@@ -1,592 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
const graphql = require('graphql');
|
|
8
|
-
const pluginHelpers = require('@graphql-codegen/plugin-helpers');
|
|
9
|
-
const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
|
|
10
|
-
const autoBind = _interopDefault(require('auto-bind'));
|
|
11
|
-
const changeCaseAll = require('change-case-all');
|
|
12
|
-
const path = require('path');
|
|
13
|
-
|
|
14
|
-
function generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes) {
|
|
15
|
-
return `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
16
|
-
}
|
|
17
|
-
function generateInfiniteQueryKey(node, hasRequiredVariables) {
|
|
18
|
-
if (hasRequiredVariables)
|
|
19
|
-
return `['${node.name.value}.infinite', variables]`;
|
|
20
|
-
return `variables === undefined ? ['${node.name.value}.infinite'] : ['${node.name.value}.infinite', variables]`;
|
|
21
|
-
}
|
|
22
|
-
function generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables) {
|
|
23
|
-
const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
24
|
-
return `\nuseInfinite${operationName}.getKey = (${signature}) => ${generateInfiniteQueryKey(node, hasRequiredVariables)};\n`;
|
|
25
|
-
}
|
|
26
|
-
function generateQueryKey(node, hasRequiredVariables) {
|
|
27
|
-
if (hasRequiredVariables)
|
|
28
|
-
return `['${node.name.value}', variables]`;
|
|
29
|
-
return `variables === undefined ? ['${node.name.value}'] : ['${node.name.value}', variables]`;
|
|
30
|
-
}
|
|
31
|
-
function generateQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables) {
|
|
32
|
-
const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
33
|
-
return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
|
|
34
|
-
}
|
|
35
|
-
function generateMutationKey(node) {
|
|
36
|
-
return `['${node.name.value}']`;
|
|
37
|
-
}
|
|
38
|
-
function generateMutationKeyMaker(node, operationName) {
|
|
39
|
-
return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)};\n`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
class CustomMapperFetcher {
|
|
43
|
-
constructor(visitor, customFetcher) {
|
|
44
|
-
this.visitor = visitor;
|
|
45
|
-
if (typeof customFetcher === 'string') {
|
|
46
|
-
customFetcher = { func: customFetcher };
|
|
47
|
-
}
|
|
48
|
-
this._mapper = visitorPluginCommon.parseMapper(customFetcher.func);
|
|
49
|
-
this._isReactHook = customFetcher.isReactHook;
|
|
50
|
-
}
|
|
51
|
-
getFetcherFnName(operationResultType, operationVariablesTypes) {
|
|
52
|
-
return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
|
|
53
|
-
}
|
|
54
|
-
generateFetcherImplementaion() {
|
|
55
|
-
if (this._mapper.isExternal) {
|
|
56
|
-
return visitorPluginCommon.buildMapperImport(this._mapper.source, [
|
|
57
|
-
{
|
|
58
|
-
identifier: this._mapper.type,
|
|
59
|
-
asDefault: this._mapper.default,
|
|
60
|
-
},
|
|
61
|
-
], this.visitor.config.useTypeImports);
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
66
|
-
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
67
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
68
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
69
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
70
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
71
|
-
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
72
|
-
const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
|
|
73
|
-
const impl = this._isReactHook
|
|
74
|
-
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
75
|
-
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
76
|
-
return `export const useInfinite${operationName} = <
|
|
77
|
-
TData = ${operationResultType},
|
|
78
|
-
TError = ${this.visitor.config.errorType}
|
|
79
|
-
>(
|
|
80
|
-
${variables},
|
|
81
|
-
${options}
|
|
82
|
-
) =>{
|
|
83
|
-
${implHookOuter}
|
|
84
|
-
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
85
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
86
|
-
${impl},
|
|
87
|
-
options
|
|
88
|
-
)};`;
|
|
89
|
-
}
|
|
90
|
-
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
91
|
-
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
92
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
93
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
94
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
95
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
96
|
-
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
97
|
-
const impl = this._isReactHook
|
|
98
|
-
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
|
|
99
|
-
: `${typedFetcher}(${documentVariableName}, variables)`;
|
|
100
|
-
return `export const use${operationName} = <
|
|
101
|
-
TData = ${operationResultType},
|
|
102
|
-
TError = ${this.visitor.config.errorType}
|
|
103
|
-
>(
|
|
104
|
-
${variables},
|
|
105
|
-
${options}
|
|
106
|
-
) =>
|
|
107
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
108
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
109
|
-
${impl},
|
|
110
|
-
options
|
|
111
|
-
);`;
|
|
112
|
-
}
|
|
113
|
-
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
114
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
115
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
116
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
117
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
118
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
119
|
-
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
120
|
-
const impl = this._isReactHook
|
|
121
|
-
? `${typedFetcher}(${documentVariableName})`
|
|
122
|
-
: `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
|
|
123
|
-
return `export const use${operationName} = <
|
|
124
|
-
TError = ${this.visitor.config.errorType},
|
|
125
|
-
TContext = unknown
|
|
126
|
-
>(${options}) =>
|
|
127
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
128
|
-
${generateMutationKey(node)},
|
|
129
|
-
${impl},
|
|
130
|
-
options
|
|
131
|
-
);`;
|
|
132
|
-
}
|
|
133
|
-
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
134
|
-
// We can't generate a fetcher field since we can't call react hooks outside of a React Fucntion Component
|
|
135
|
-
// Related: https://reactjs.org/docs/hooks-rules.html
|
|
136
|
-
if (this._isReactHook)
|
|
137
|
-
return '';
|
|
138
|
-
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
139
|
-
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
140
|
-
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
141
|
-
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
class FetchFetcher {
|
|
146
|
-
constructor(visitor) {
|
|
147
|
-
this.visitor = visitor;
|
|
148
|
-
}
|
|
149
|
-
generateFetcherImplementaion() {
|
|
150
|
-
return `
|
|
151
|
-
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
|
|
152
|
-
return async (): Promise<TData> => {
|
|
153
|
-
const res = await fetch(endpoint, {
|
|
154
|
-
method: 'POST',
|
|
155
|
-
...requestInit,
|
|
156
|
-
body: JSON.stringify({ query, variables }),
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const json = await res.json();
|
|
160
|
-
|
|
161
|
-
if (json.errors) {
|
|
162
|
-
const { message } = json.errors[0];
|
|
163
|
-
|
|
164
|
-
throw new Error(message);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return json.data;
|
|
168
|
-
}
|
|
169
|
-
}`;
|
|
170
|
-
}
|
|
171
|
-
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
172
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
173
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
174
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
175
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
176
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
177
|
-
return `export const useInfinite${operationName} = <
|
|
178
|
-
TData = ${operationResultType},
|
|
179
|
-
TError = ${this.visitor.config.errorType}
|
|
180
|
-
>(
|
|
181
|
-
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
182
|
-
pageParamKey: keyof ${operationVariablesTypes},
|
|
183
|
-
${variables},
|
|
184
|
-
${options}
|
|
185
|
-
) =>
|
|
186
|
-
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
187
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
188
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
189
|
-
options
|
|
190
|
-
);`;
|
|
191
|
-
}
|
|
192
|
-
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
193
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
194
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
195
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
196
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
197
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
198
|
-
return `export const use${operationName} = <
|
|
199
|
-
TData = ${operationResultType},
|
|
200
|
-
TError = ${this.visitor.config.errorType}
|
|
201
|
-
>(
|
|
202
|
-
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
203
|
-
${variables},
|
|
204
|
-
${options}
|
|
205
|
-
) =>
|
|
206
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
207
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
208
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables),
|
|
209
|
-
options
|
|
210
|
-
);`;
|
|
211
|
-
}
|
|
212
|
-
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
213
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
214
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
215
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
216
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
217
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
218
|
-
return `export const use${operationName} = <
|
|
219
|
-
TError = ${this.visitor.config.errorType},
|
|
220
|
-
TContext = unknown
|
|
221
|
-
>(
|
|
222
|
-
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
223
|
-
${options}
|
|
224
|
-
) =>
|
|
225
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
226
|
-
${generateMutationKey(node)},
|
|
227
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
|
|
228
|
-
options
|
|
229
|
-
);`;
|
|
230
|
-
}
|
|
231
|
-
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
232
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
233
|
-
return `\nuse${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
class GraphQLRequestClientFetcher {
|
|
238
|
-
constructor(visitor) {
|
|
239
|
-
this.visitor = visitor;
|
|
240
|
-
}
|
|
241
|
-
generateFetcherImplementaion() {
|
|
242
|
-
return `
|
|
243
|
-
function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variables?: TVariables, headers?: RequestInit['headers']) {
|
|
244
|
-
return async (): Promise<TData> => client.request<TData, TVariables>(query, variables, headers);
|
|
245
|
-
}`;
|
|
246
|
-
}
|
|
247
|
-
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
248
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
249
|
-
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
250
|
-
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
251
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
252
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
253
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
254
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
255
|
-
return `export const useInfinite${operationName} = <
|
|
256
|
-
TData = ${operationResultType},
|
|
257
|
-
TError = ${this.visitor.config.errorType}
|
|
258
|
-
>(
|
|
259
|
-
pageParamKey: keyof ${operationVariablesTypes},
|
|
260
|
-
client: GraphQLClient,
|
|
261
|
-
${variables},
|
|
262
|
-
${options},
|
|
263
|
-
headers?: RequestInit['headers']
|
|
264
|
-
) =>
|
|
265
|
-
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
266
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
267
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)(),
|
|
268
|
-
options
|
|
269
|
-
);`;
|
|
270
|
-
}
|
|
271
|
-
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
272
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
273
|
-
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
274
|
-
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
275
|
-
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
276
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
277
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
278
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
279
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
280
|
-
return `export const use${operationName} = <
|
|
281
|
-
TData = ${operationResultType},
|
|
282
|
-
TError = ${this.visitor.config.errorType}
|
|
283
|
-
>(
|
|
284
|
-
client: GraphQLClient,
|
|
285
|
-
${variables},
|
|
286
|
-
${options},
|
|
287
|
-
headers?: RequestInit['headers']
|
|
288
|
-
) =>
|
|
289
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
290
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
291
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
|
|
292
|
-
options
|
|
293
|
-
);`;
|
|
294
|
-
}
|
|
295
|
-
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
296
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
297
|
-
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
298
|
-
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
299
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
300
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
301
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
302
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
303
|
-
return `export const use${operationName} = <
|
|
304
|
-
TError = ${this.visitor.config.errorType},
|
|
305
|
-
TContext = unknown
|
|
306
|
-
>(
|
|
307
|
-
client: GraphQLClient,
|
|
308
|
-
${options},
|
|
309
|
-
headers?: RequestInit['headers']
|
|
310
|
-
) =>
|
|
311
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
312
|
-
${generateMutationKey(node)},
|
|
313
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
|
|
314
|
-
options
|
|
315
|
-
);`;
|
|
316
|
-
}
|
|
317
|
-
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
318
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
319
|
-
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
320
|
-
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
321
|
-
return `\nuse${operationName}.fetcher = (client: GraphQLClient, ${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers);`;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
class HardcodedFetchFetcher {
|
|
326
|
-
constructor(visitor, config) {
|
|
327
|
-
this.visitor = visitor;
|
|
328
|
-
this.config = config;
|
|
329
|
-
}
|
|
330
|
-
getEndpoint() {
|
|
331
|
-
try {
|
|
332
|
-
new URL(this.config.endpoint);
|
|
333
|
-
return JSON.stringify(this.config.endpoint);
|
|
334
|
-
}
|
|
335
|
-
catch (e) {
|
|
336
|
-
return `${this.config.endpoint} as string`;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
getFetchParams() {
|
|
340
|
-
let fetchParamsPartial = '';
|
|
341
|
-
if (this.config.fetchParams) {
|
|
342
|
-
const fetchParamsString = typeof this.config.fetchParams === 'string' ? this.config.fetchParams : JSON.stringify(this.config.fetchParams);
|
|
343
|
-
fetchParamsPartial = `\n ...(${fetchParamsString}),`;
|
|
344
|
-
}
|
|
345
|
-
return ` method: "POST",${fetchParamsPartial}`;
|
|
346
|
-
}
|
|
347
|
-
generateFetcherImplementaion() {
|
|
348
|
-
return `
|
|
349
|
-
function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
|
|
350
|
-
return async (): Promise<TData> => {
|
|
351
|
-
const res = await fetch(${this.getEndpoint()}, {
|
|
352
|
-
${this.getFetchParams()}
|
|
353
|
-
body: JSON.stringify({ query, variables }),
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
const json = await res.json();
|
|
357
|
-
|
|
358
|
-
if (json.errors) {
|
|
359
|
-
const { message } = json.errors[0];
|
|
360
|
-
|
|
361
|
-
throw new Error(message);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
return json.data;
|
|
365
|
-
}
|
|
366
|
-
}`;
|
|
367
|
-
}
|
|
368
|
-
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
369
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
370
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
371
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
372
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
373
|
-
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
374
|
-
return `export const useInfinite${operationName} = <
|
|
375
|
-
TData = ${operationResultType},
|
|
376
|
-
TError = ${this.visitor.config.errorType}
|
|
377
|
-
>(
|
|
378
|
-
pageParamKey: keyof ${operationVariablesTypes},
|
|
379
|
-
${variables},
|
|
380
|
-
${options}
|
|
381
|
-
) =>
|
|
382
|
-
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
383
|
-
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
384
|
-
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
385
|
-
options
|
|
386
|
-
);`;
|
|
387
|
-
}
|
|
388
|
-
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
389
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
390
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
391
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
392
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
393
|
-
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
394
|
-
return `export const use${operationName} = <
|
|
395
|
-
TData = ${operationResultType},
|
|
396
|
-
TError = ${this.visitor.config.errorType}
|
|
397
|
-
>(
|
|
398
|
-
${variables},
|
|
399
|
-
${options}
|
|
400
|
-
) =>
|
|
401
|
-
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
402
|
-
${generateQueryKey(node, hasRequiredVariables)},
|
|
403
|
-
fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables),
|
|
404
|
-
options
|
|
405
|
-
);`;
|
|
406
|
-
}
|
|
407
|
-
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
408
|
-
const variables = `variables?: ${operationVariablesTypes}`;
|
|
409
|
-
const hookConfig = this.visitor.queryMethodMap;
|
|
410
|
-
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
411
|
-
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
412
|
-
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
413
|
-
return `export const use${operationName} = <
|
|
414
|
-
TError = ${this.visitor.config.errorType},
|
|
415
|
-
TContext = unknown
|
|
416
|
-
>(${options}) =>
|
|
417
|
-
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
418
|
-
${generateMutationKey(node)},
|
|
419
|
-
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
|
|
420
|
-
options
|
|
421
|
-
);`;
|
|
422
|
-
}
|
|
423
|
-
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
424
|
-
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
425
|
-
return `\nuse${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
class ReactQueryVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
430
|
-
constructor(schema, fragments, rawConfig, documents) {
|
|
431
|
-
super(schema, fragments, rawConfig, {
|
|
432
|
-
documentMode: visitorPluginCommon.DocumentMode.string,
|
|
433
|
-
errorType: visitorPluginCommon.getConfigValue(rawConfig.errorType, 'unknown'),
|
|
434
|
-
exposeDocument: visitorPluginCommon.getConfigValue(rawConfig.exposeDocument, false),
|
|
435
|
-
exposeQueryKeys: visitorPluginCommon.getConfigValue(rawConfig.exposeQueryKeys, false),
|
|
436
|
-
exposeMutationKeys: visitorPluginCommon.getConfigValue(rawConfig.exposeMutationKeys, false),
|
|
437
|
-
exposeFetcher: visitorPluginCommon.getConfigValue(rawConfig.exposeFetcher, false),
|
|
438
|
-
addInfiniteQuery: visitorPluginCommon.getConfigValue(rawConfig.addInfiniteQuery, false),
|
|
439
|
-
});
|
|
440
|
-
this.rawConfig = rawConfig;
|
|
441
|
-
this.reactQueryHookIdentifiersInUse = new Set();
|
|
442
|
-
this.reactQueryOptionsIdentifiersInUse = new Set();
|
|
443
|
-
this.queryMethodMap = {
|
|
444
|
-
infiniteQuery: {
|
|
445
|
-
hook: 'useInfiniteQuery',
|
|
446
|
-
options: 'UseInfiniteQueryOptions',
|
|
447
|
-
},
|
|
448
|
-
query: {
|
|
449
|
-
hook: 'useQuery',
|
|
450
|
-
options: 'UseQueryOptions',
|
|
451
|
-
},
|
|
452
|
-
mutation: {
|
|
453
|
-
hook: 'useMutation',
|
|
454
|
-
options: 'UseMutationOptions',
|
|
455
|
-
},
|
|
456
|
-
};
|
|
457
|
-
this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
|
|
458
|
-
this._documents = documents;
|
|
459
|
-
this.fetcher = this.createFetcher(rawConfig.fetcher || 'fetch');
|
|
460
|
-
autoBind(this);
|
|
461
|
-
}
|
|
462
|
-
get imports() {
|
|
463
|
-
return this._imports;
|
|
464
|
-
}
|
|
465
|
-
createFetcher(raw) {
|
|
466
|
-
if (raw === 'fetch') {
|
|
467
|
-
return new FetchFetcher(this);
|
|
468
|
-
}
|
|
469
|
-
if (typeof raw === 'object' && 'endpoint' in raw) {
|
|
470
|
-
return new HardcodedFetchFetcher(this, raw);
|
|
471
|
-
}
|
|
472
|
-
if (raw === 'graphql-request') {
|
|
473
|
-
return new GraphQLRequestClientFetcher(this);
|
|
474
|
-
}
|
|
475
|
-
return new CustomMapperFetcher(this, raw);
|
|
476
|
-
}
|
|
477
|
-
get hasOperations() {
|
|
478
|
-
return this._collectedOperations.length > 0;
|
|
479
|
-
}
|
|
480
|
-
getImports() {
|
|
481
|
-
const baseImports = super.getImports();
|
|
482
|
-
if (!this.hasOperations) {
|
|
483
|
-
return baseImports;
|
|
484
|
-
}
|
|
485
|
-
if (this.config.addInfiniteQuery) {
|
|
486
|
-
this.reactQueryOptionsIdentifiersInUse.add('QueryFunctionContext');
|
|
487
|
-
}
|
|
488
|
-
const hookAndTypeImports = [
|
|
489
|
-
...Array.from(this.reactQueryHookIdentifiersInUse),
|
|
490
|
-
...Array.from(this.reactQueryOptionsIdentifiersInUse).map(identifier => `${this.config.useTypeImports ? 'type ' : ''}${identifier}`),
|
|
491
|
-
];
|
|
492
|
-
return [...baseImports, `import { ${hookAndTypeImports.join(', ')} } from 'react-query';`];
|
|
493
|
-
}
|
|
494
|
-
getFetcherImplementation() {
|
|
495
|
-
return this.fetcher.generateFetcherImplementaion();
|
|
496
|
-
}
|
|
497
|
-
_getHookSuffix(name, operationType) {
|
|
498
|
-
if (this.config.omitOperationSuffix) {
|
|
499
|
-
return '';
|
|
500
|
-
}
|
|
501
|
-
if (!this.config.dedupeOperationSuffix) {
|
|
502
|
-
return changeCaseAll.pascalCase(operationType);
|
|
503
|
-
}
|
|
504
|
-
if (name.includes('Query') || name.includes('Mutation') || name.includes('Subscription')) {
|
|
505
|
-
return '';
|
|
506
|
-
}
|
|
507
|
-
return changeCaseAll.pascalCase(operationType);
|
|
508
|
-
}
|
|
509
|
-
buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
510
|
-
var _a, _b;
|
|
511
|
-
const nodeName = (_b = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '';
|
|
512
|
-
const suffix = this._getHookSuffix(nodeName, operationType);
|
|
513
|
-
const operationName = this.convertName(nodeName, {
|
|
514
|
-
suffix,
|
|
515
|
-
useTypesPrefix: false,
|
|
516
|
-
useTypesSuffix: false,
|
|
517
|
-
});
|
|
518
|
-
operationResultType = this._externalImportPrefix + operationResultType;
|
|
519
|
-
operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
|
|
520
|
-
if (operationType === 'Query') {
|
|
521
|
-
let query = this.fetcher.generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
522
|
-
if (this.config.exposeDocument) {
|
|
523
|
-
query += `\nuse${operationName}.document = ${documentVariableName};\n`;
|
|
524
|
-
}
|
|
525
|
-
if (this.config.exposeQueryKeys) {
|
|
526
|
-
query += `\n${generateQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
527
|
-
}
|
|
528
|
-
if (this.config.addInfiniteQuery) {
|
|
529
|
-
query += `\n${this.fetcher.generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables)}\n`;
|
|
530
|
-
if (this.config.exposeQueryKeys) {
|
|
531
|
-
query += `\n${generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
535
|
-
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
|
536
|
-
// a fetcher field anyways.
|
|
537
|
-
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
538
|
-
query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
539
|
-
}
|
|
540
|
-
return query;
|
|
541
|
-
}
|
|
542
|
-
if (operationType === 'Mutation') {
|
|
543
|
-
let query = this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
544
|
-
if (this.config.exposeMutationKeys) {
|
|
545
|
-
query += generateMutationKeyMaker(node, operationName);
|
|
546
|
-
}
|
|
547
|
-
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
548
|
-
query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
549
|
-
}
|
|
550
|
-
return query;
|
|
551
|
-
}
|
|
552
|
-
if (operationType === 'Subscription') {
|
|
553
|
-
// eslint-disable-next-line no-console
|
|
554
|
-
console.warn(`Plugin "typescript-react-query" does not support GraphQL Subscriptions at the moment! Ignoring "${node.name.value}"...`);
|
|
555
|
-
}
|
|
556
|
-
return null;
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
const plugin = (schema, documents, config) => {
|
|
561
|
-
const allAst = graphql.concatAST(documents.map(v => v.document));
|
|
562
|
-
const allFragments = [
|
|
563
|
-
...allAst.definitions.filter(d => d.kind === graphql.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
564
|
-
node: fragmentDef,
|
|
565
|
-
name: fragmentDef.name.value,
|
|
566
|
-
onType: fragmentDef.typeCondition.name.value,
|
|
567
|
-
isExternal: false,
|
|
568
|
-
})),
|
|
569
|
-
...(config.externalFragments || []),
|
|
570
|
-
];
|
|
571
|
-
const visitor = new ReactQueryVisitor(schema, allFragments, config, documents);
|
|
572
|
-
const visitorResult = pluginHelpers.oldVisit(allAst, { leave: visitor });
|
|
573
|
-
if (visitor.hasOperations) {
|
|
574
|
-
return {
|
|
575
|
-
prepend: [...visitor.getImports(), visitor.getFetcherImplementation()],
|
|
576
|
-
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
|
-
return {
|
|
580
|
-
prepend: [...visitor.getImports()],
|
|
581
|
-
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
|
582
|
-
};
|
|
583
|
-
};
|
|
584
|
-
const validate = async (schema, documents, config, outputFile) => {
|
|
585
|
-
if (path.extname(outputFile) !== '.ts' && path.extname(outputFile) !== '.tsx') {
|
|
586
|
-
throw new Error(`Plugin "typescript-react-query" requires extension to be ".ts" or ".tsx"!`);
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
exports.ReactQueryVisitor = ReactQueryVisitor;
|
|
591
|
-
exports.plugin = plugin;
|
|
592
|
-
exports.validate = validate;
|