@graphql-codegen/typescript-react-query 3.1.1-alpha-66fd7ef2f.0 → 3.1.2-alpha-2113b7d34.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/config.d.ts CHANGED
@@ -47,6 +47,16 @@ export interface ReactQueryRawPluginConfig extends Omit<RawClientSideBasePluginC
47
47
  * ```
48
48
  */
49
49
  exposeQueryKeys?: boolean;
50
+ /**
51
+ * @default false
52
+ * @description For each generate mutation hook adds getKey() function. Useful for call outside of functional component.
53
+ * @exampleMarkdown
54
+ * ```ts
55
+ * const mutation = useUserDetailsMutation(...);
56
+ * const key = useUserDetailsMutation.getKey();
57
+ * ```
58
+ */
59
+ exposeMutationKeys?: boolean;
50
60
  /**
51
61
  * @default false
52
62
  * @description For each generate query hook adds `fetcher` field with a corresponding GraphQL query using the fetcher.
@@ -10,6 +10,6 @@ export declare class CustomMapperFetcher implements FetcherRenderer {
10
10
  private getFetcherFnName;
11
11
  generateFetcherImplementaion(): string;
12
12
  generateQueryHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
13
- generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string): string;
13
+ generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
14
14
  generateFetcherFetch(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
15
15
  }
@@ -10,6 +10,6 @@ export declare class HardcodedFetchFetcher implements FetcherRenderer {
10
10
  private getFetchParams;
11
11
  generateFetcherImplementaion(): string;
12
12
  generateQueryHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
13
- generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string): string;
13
+ generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
14
14
  generateFetcherFetch(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
15
15
  }
@@ -6,6 +6,6 @@ export declare class FetchFetcher implements FetcherRenderer {
6
6
  constructor(visitor: ReactQueryVisitor);
7
7
  generateFetcherImplementaion(): string;
8
8
  generateQueryHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
9
- generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string): string;
9
+ generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
10
10
  generateFetcherFetch(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
11
11
  }
@@ -6,6 +6,6 @@ export declare class GraphQLRequestClientFetcher implements FetcherRenderer {
6
6
  constructor(visitor: ReactQueryVisitor);
7
7
  generateFetcherImplementaion(): string;
8
8
  generateQueryHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
9
- generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string): string;
9
+ generateMutationHook(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
10
10
  generateFetcherFetch(node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
11
11
  }
package/fetcher.d.ts CHANGED
@@ -2,6 +2,6 @@ import { OperationDefinitionNode } from 'graphql';
2
2
  export interface FetcherRenderer {
3
3
  generateFetcherImplementaion: () => string;
4
4
  generateQueryHook: (node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean) => string;
5
- generateMutationHook: (node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string) => string;
5
+ generateMutationHook: (node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean) => string;
6
6
  generateFetcherFetch: (node: OperationDefinitionNode, documentVariableName: string, operationName: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean) => string;
7
7
  }
package/index.js CHANGED
@@ -23,6 +23,12 @@ function generateQueryKeyMaker(node, operationName, operationVariablesTypes, has
23
23
  const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
24
24
  return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
25
25
  }
26
+ function generateMutationKey(node) {
27
+ return `'${node.name.value}'`;
28
+ }
29
+ function generateMutationKeyMaker(node, operationName) {
30
+ return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)}};\n`;
31
+ }
26
32
 
27
33
  class FetchFetcher {
28
34
  constructor(visitor) {
@@ -70,7 +76,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
70
76
  options
71
77
  );`;
72
78
  }
73
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
79
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
74
80
  const variables = `variables?: ${operationVariablesTypes}`;
75
81
  const hookConfig = this.visitor.queryMethodMap;
76
82
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -84,6 +90,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
84
90
  ${options}
85
91
  ) =>
86
92
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
93
+ ${generateMutationKey(node)},
87
94
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
88
95
  options
89
96
  );`;
@@ -155,7 +162,7 @@ ${this.getFetchParams()}
155
162
  options
156
163
  );`;
157
164
  }
158
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
165
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
159
166
  const variables = `variables?: ${operationVariablesTypes}`;
160
167
  const hookConfig = this.visitor.queryMethodMap;
161
168
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -166,6 +173,7 @@ ${this.getFetchParams()}
166
173
  TContext = unknown
167
174
  >(${options}) =>
168
175
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
176
+ ${generateMutationKey(node)},
169
177
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
170
178
  options
171
179
  );`;
@@ -199,18 +207,18 @@ function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variab
199
207
  TData = ${operationResultType},
200
208
  TError = ${this.visitor.config.errorType}
201
209
  >(
202
- client: GraphQLClient,
203
- ${variables},
210
+ client: GraphQLClient,
211
+ ${variables},
204
212
  ${options},
205
213
  headers?: RequestInit['headers']
206
- ) =>
214
+ ) =>
207
215
  ${hookConfig.query.hook}<${operationResultType}, TError, TData>(
208
216
  ${generateQueryKey(node, hasRequiredVariables)},
209
217
  fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
210
218
  options
211
219
  );`;
212
220
  }
213
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
221
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
214
222
  const variables = `variables?: ${operationVariablesTypes}`;
215
223
  this.visitor.imports.add(`import { GraphQLClient } from 'graphql-request';`);
216
224
  const hookConfig = this.visitor.queryMethodMap;
@@ -221,11 +229,12 @@ function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variab
221
229
  TError = ${this.visitor.config.errorType},
222
230
  TContext = unknown
223
231
  >(
224
- client: GraphQLClient,
232
+ client: GraphQLClient,
225
233
  ${options},
226
234
  headers?: RequestInit['headers']
227
- ) =>
235
+ ) =>
228
236
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
237
+ ${generateMutationKey(node)},
229
238
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
230
239
  options
231
240
  );`;
@@ -273,16 +282,16 @@ class CustomMapperFetcher {
273
282
  TData = ${operationResultType},
274
283
  TError = ${this.visitor.config.errorType}
275
284
  >(
276
- ${variables},
285
+ ${variables},
277
286
  ${options}
278
- ) =>
287
+ ) =>
279
288
  ${hookConfig.query.hook}<${operationResultType}, TError, TData>(
280
289
  ${generateQueryKey(node, hasRequiredVariables)},
281
290
  ${impl},
282
291
  options
283
292
  );`;
284
293
  }
285
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
294
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
286
295
  const variables = `variables?: ${operationVariablesTypes}`;
287
296
  const hookConfig = this.visitor.queryMethodMap;
288
297
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -295,8 +304,9 @@ class CustomMapperFetcher {
295
304
  return `export const use${operationName} = <
296
305
  TError = ${this.visitor.config.errorType},
297
306
  TContext = unknown
298
- >(${options}) =>
307
+ >(${options}) =>
299
308
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
309
+ ${generateMutationKey(node)},
300
310
  ${impl},
301
311
  options
302
312
  );`;
@@ -320,6 +330,7 @@ class ReactQueryVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
320
330
  errorType: visitorPluginCommon.getConfigValue(rawConfig.errorType, 'unknown'),
321
331
  exposeDocument: visitorPluginCommon.getConfigValue(rawConfig.exposeDocument, false),
322
332
  exposeQueryKeys: visitorPluginCommon.getConfigValue(rawConfig.exposeQueryKeys, false),
333
+ exposeMutationKeys: visitorPluginCommon.getConfigValue(rawConfig.exposeMutationKeys, false),
323
334
  exposeFetcher: visitorPluginCommon.getConfigValue(rawConfig.exposeFetcher, false),
324
335
  });
325
336
  this.rawConfig = rawConfig;
@@ -407,7 +418,14 @@ class ReactQueryVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
407
418
  return query;
408
419
  }
409
420
  else if (operationType === 'Mutation') {
410
- return this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes);
421
+ let query = this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
422
+ if (this.config.exposeMutationKeys) {
423
+ query += generateMutationKeyMaker(node, operationName);
424
+ }
425
+ if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
426
+ query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
427
+ }
428
+ return query;
411
429
  }
412
430
  else if (operationType === 'Subscription') {
413
431
  // eslint-disable-next-line no-console
package/index.mjs CHANGED
@@ -17,6 +17,12 @@ function generateQueryKeyMaker(node, operationName, operationVariablesTypes, has
17
17
  const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
18
18
  return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
19
19
  }
20
+ function generateMutationKey(node) {
21
+ return `'${node.name.value}'`;
22
+ }
23
+ function generateMutationKeyMaker(node, operationName) {
24
+ return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)}};\n`;
25
+ }
20
26
 
21
27
  class FetchFetcher {
22
28
  constructor(visitor) {
@@ -64,7 +70,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
64
70
  options
65
71
  );`;
66
72
  }
67
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
73
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
68
74
  const variables = `variables?: ${operationVariablesTypes}`;
69
75
  const hookConfig = this.visitor.queryMethodMap;
70
76
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -78,6 +84,7 @@ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit,
78
84
  ${options}
79
85
  ) =>
80
86
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
87
+ ${generateMutationKey(node)},
81
88
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
82
89
  options
83
90
  );`;
@@ -149,7 +156,7 @@ ${this.getFetchParams()}
149
156
  options
150
157
  );`;
151
158
  }
152
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
159
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
153
160
  const variables = `variables?: ${operationVariablesTypes}`;
154
161
  const hookConfig = this.visitor.queryMethodMap;
155
162
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -160,6 +167,7 @@ ${this.getFetchParams()}
160
167
  TContext = unknown
161
168
  >(${options}) =>
162
169
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
170
+ ${generateMutationKey(node)},
163
171
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
164
172
  options
165
173
  );`;
@@ -193,18 +201,18 @@ function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variab
193
201
  TData = ${operationResultType},
194
202
  TError = ${this.visitor.config.errorType}
195
203
  >(
196
- client: GraphQLClient,
197
- ${variables},
204
+ client: GraphQLClient,
205
+ ${variables},
198
206
  ${options},
199
207
  headers?: RequestInit['headers']
200
- ) =>
208
+ ) =>
201
209
  ${hookConfig.query.hook}<${operationResultType}, TError, TData>(
202
210
  ${generateQueryKey(node, hasRequiredVariables)},
203
211
  fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
204
212
  options
205
213
  );`;
206
214
  }
207
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
215
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
208
216
  const variables = `variables?: ${operationVariablesTypes}`;
209
217
  this.visitor.imports.add(`import { GraphQLClient } from 'graphql-request';`);
210
218
  const hookConfig = this.visitor.queryMethodMap;
@@ -215,11 +223,12 @@ function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variab
215
223
  TError = ${this.visitor.config.errorType},
216
224
  TContext = unknown
217
225
  >(
218
- client: GraphQLClient,
226
+ client: GraphQLClient,
219
227
  ${options},
220
228
  headers?: RequestInit['headers']
221
- ) =>
229
+ ) =>
222
230
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
231
+ ${generateMutationKey(node)},
223
232
  (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
224
233
  options
225
234
  );`;
@@ -267,16 +276,16 @@ class CustomMapperFetcher {
267
276
  TData = ${operationResultType},
268
277
  TError = ${this.visitor.config.errorType}
269
278
  >(
270
- ${variables},
279
+ ${variables},
271
280
  ${options}
272
- ) =>
281
+ ) =>
273
282
  ${hookConfig.query.hook}<${operationResultType}, TError, TData>(
274
283
  ${generateQueryKey(node, hasRequiredVariables)},
275
284
  ${impl},
276
285
  options
277
286
  );`;
278
287
  }
279
- generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes) {
288
+ generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
280
289
  const variables = `variables?: ${operationVariablesTypes}`;
281
290
  const hookConfig = this.visitor.queryMethodMap;
282
291
  this.visitor.reactQueryIdentifiersInUse.add(hookConfig.mutation.hook);
@@ -289,8 +298,9 @@ class CustomMapperFetcher {
289
298
  return `export const use${operationName} = <
290
299
  TError = ${this.visitor.config.errorType},
291
300
  TContext = unknown
292
- >(${options}) =>
301
+ >(${options}) =>
293
302
  ${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
303
+ ${generateMutationKey(node)},
294
304
  ${impl},
295
305
  options
296
306
  );`;
@@ -314,6 +324,7 @@ class ReactQueryVisitor extends ClientSideBaseVisitor {
314
324
  errorType: getConfigValue(rawConfig.errorType, 'unknown'),
315
325
  exposeDocument: getConfigValue(rawConfig.exposeDocument, false),
316
326
  exposeQueryKeys: getConfigValue(rawConfig.exposeQueryKeys, false),
327
+ exposeMutationKeys: getConfigValue(rawConfig.exposeMutationKeys, false),
317
328
  exposeFetcher: getConfigValue(rawConfig.exposeFetcher, false),
318
329
  });
319
330
  this.rawConfig = rawConfig;
@@ -401,7 +412,14 @@ class ReactQueryVisitor extends ClientSideBaseVisitor {
401
412
  return query;
402
413
  }
403
414
  else if (operationType === 'Mutation') {
404
- return this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes);
415
+ let query = this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
416
+ if (this.config.exposeMutationKeys) {
417
+ query += generateMutationKeyMaker(node, operationName);
418
+ }
419
+ if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
420
+ query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
421
+ }
422
+ return query;
405
423
  }
406
424
  else if (operationType === 'Subscription') {
407
425
  // eslint-disable-next-line no-console
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-react-query",
3
- "version": "3.1.1-alpha-66fd7ef2f.0",
3
+ "version": "3.1.2-alpha-2113b7d34.0",
4
4
  "description": "GraphQL Code Generator plugin for generating a ready-to-use React-Query Hooks based on GraphQL operations",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@graphql-codegen/plugin-helpers": "^2.3.0",
11
- "@graphql-codegen/visitor-plugin-common": "2.5.1-alpha-66fd7ef2f.0",
11
+ "@graphql-codegen/visitor-plugin-common": "2.5.1-alpha-2113b7d34.0",
12
12
  "auto-bind": "~4.0.0",
13
13
  "change-case-all": "1.0.14",
14
14
  "tslib": "~2.3.0"
@@ -2,3 +2,5 @@ import { OperationDefinitionNode } from 'graphql';
2
2
  export declare function generateQueryVariablesSignature(hasRequiredVariables: boolean, operationVariablesTypes: string): string;
3
3
  export declare function generateQueryKey(node: OperationDefinitionNode, hasRequiredVariables: boolean): string;
4
4
  export declare function generateQueryKeyMaker(node: OperationDefinitionNode, operationName: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
5
+ export declare function generateMutationKey(node: OperationDefinitionNode): string;
6
+ export declare function generateMutationKeyMaker(node: OperationDefinitionNode, operationName: string): string;
package/visitor.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface ReactQueryPluginConfig extends ClientSideBasePluginConfig {
7
7
  errorType: string;
8
8
  exposeDocument: boolean;
9
9
  exposeQueryKeys: boolean;
10
+ exposeMutationKeys: boolean;
10
11
  exposeFetcher: boolean;
11
12
  }
12
13
  export interface ReactQueryMethodMap {