@graphql-codegen/typescript-react-query 3.3.1 → 3.5.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 +1 -1
- package/index.js +14 -3
- package/index.mjs +14 -3
- package/package.json +1 -1
- package/variables-generator.d.ts +1 -0
package/config.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export interface ReactQueryRawPluginConfig extends Omit<RawClientSideBasePluginC
|
|
|
40
40
|
exposeDocument?: boolean;
|
|
41
41
|
/**
|
|
42
42
|
* @default false
|
|
43
|
-
* @description For each generate query hook adds getKey(variables: QueryVariables) function. Useful for cache updates.
|
|
43
|
+
* @description For each generate query hook adds getKey(variables: QueryVariables) function. Useful for cache updates. If addInfiniteQuery is true, it will also add a getKey function to each infinite query.
|
|
44
44
|
* @exampleMarkdown
|
|
45
45
|
* ```ts
|
|
46
46
|
* const query = useUserDetailsQuery(...)
|
package/index.js
CHANGED
|
@@ -19,6 +19,10 @@ function generateInfiniteQueryKey(node, hasRequiredVariables) {
|
|
|
19
19
|
return `['${node.name.value}.infinite', variables]`;
|
|
20
20
|
return `variables === undefined ? ['${node.name.value}.infinite'] : ['${node.name.value}.infinite', variables]`;
|
|
21
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
|
+
}
|
|
22
26
|
function generateQueryKey(node, hasRequiredVariables) {
|
|
23
27
|
if (hasRequiredVariables)
|
|
24
28
|
return `['${node.name.value}', variables]`;
|
|
@@ -29,7 +33,7 @@ function generateQueryKeyMaker(node, operationName, operationVariablesTypes, has
|
|
|
29
33
|
return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
|
|
30
34
|
}
|
|
31
35
|
function generateMutationKey(node) {
|
|
32
|
-
return `'${node.name.value}'`;
|
|
36
|
+
return `['${node.name.value}']`;
|
|
33
37
|
}
|
|
34
38
|
function generateMutationKeyMaker(node, operationName) {
|
|
35
39
|
return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)};\n`;
|
|
@@ -60,6 +64,8 @@ class CustomMapperFetcher {
|
|
|
60
64
|
}
|
|
61
65
|
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
62
66
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
67
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
68
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
63
69
|
const hookConfig = this.visitor.queryMethodMap;
|
|
64
70
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
65
71
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
@@ -85,6 +91,8 @@ class CustomMapperFetcher {
|
|
|
85
91
|
}
|
|
86
92
|
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
87
93
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
94
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
95
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
88
96
|
const hookConfig = this.visitor.queryMethodMap;
|
|
89
97
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.hook);
|
|
90
98
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.options);
|
|
@@ -133,8 +141,8 @@ class CustomMapperFetcher {
|
|
|
133
141
|
return '';
|
|
134
142
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
135
143
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
136
|
-
const impl = `${typedFetcher}(${documentVariableName}, variables)`;
|
|
137
|
-
return `\nuse${operationName}.fetcher = (${variables}) => ${impl};`;
|
|
144
|
+
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
145
|
+
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
138
146
|
}
|
|
139
147
|
}
|
|
140
148
|
|
|
@@ -516,6 +524,9 @@ class ReactQueryVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
516
524
|
}
|
|
517
525
|
if (this.config.addInfiniteQuery) {
|
|
518
526
|
query += `\n${this.fetcher.generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables)}\n`;
|
|
527
|
+
if (this.config.exposeQueryKeys) {
|
|
528
|
+
query += `\n${generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
529
|
+
}
|
|
519
530
|
}
|
|
520
531
|
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
521
532
|
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
package/index.mjs
CHANGED
|
@@ -13,6 +13,10 @@ function generateInfiniteQueryKey(node, hasRequiredVariables) {
|
|
|
13
13
|
return `['${node.name.value}.infinite', variables]`;
|
|
14
14
|
return `variables === undefined ? ['${node.name.value}.infinite'] : ['${node.name.value}.infinite', variables]`;
|
|
15
15
|
}
|
|
16
|
+
function generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables) {
|
|
17
|
+
const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
18
|
+
return `\nuseInfinite${operationName}.getKey = (${signature}) => ${generateInfiniteQueryKey(node, hasRequiredVariables)};\n`;
|
|
19
|
+
}
|
|
16
20
|
function generateQueryKey(node, hasRequiredVariables) {
|
|
17
21
|
if (hasRequiredVariables)
|
|
18
22
|
return `['${node.name.value}', variables]`;
|
|
@@ -23,7 +27,7 @@ function generateQueryKeyMaker(node, operationName, operationVariablesTypes, has
|
|
|
23
27
|
return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
|
|
24
28
|
}
|
|
25
29
|
function generateMutationKey(node) {
|
|
26
|
-
return `'${node.name.value}'`;
|
|
30
|
+
return `['${node.name.value}']`;
|
|
27
31
|
}
|
|
28
32
|
function generateMutationKeyMaker(node, operationName) {
|
|
29
33
|
return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)};\n`;
|
|
@@ -54,6 +58,8 @@ class CustomMapperFetcher {
|
|
|
54
58
|
}
|
|
55
59
|
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
56
60
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
61
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
62
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
57
63
|
const hookConfig = this.visitor.queryMethodMap;
|
|
58
64
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
59
65
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
@@ -79,6 +85,8 @@ class CustomMapperFetcher {
|
|
|
79
85
|
}
|
|
80
86
|
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
81
87
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
88
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
89
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
82
90
|
const hookConfig = this.visitor.queryMethodMap;
|
|
83
91
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.hook);
|
|
84
92
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.options);
|
|
@@ -127,8 +135,8 @@ class CustomMapperFetcher {
|
|
|
127
135
|
return '';
|
|
128
136
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
129
137
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
130
|
-
const impl = `${typedFetcher}(${documentVariableName}, variables)`;
|
|
131
|
-
return `\nuse${operationName}.fetcher = (${variables}) => ${impl};`;
|
|
138
|
+
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
139
|
+
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
132
140
|
}
|
|
133
141
|
}
|
|
134
142
|
|
|
@@ -510,6 +518,9 @@ class ReactQueryVisitor extends ClientSideBaseVisitor {
|
|
|
510
518
|
}
|
|
511
519
|
if (this.config.addInfiniteQuery) {
|
|
512
520
|
query += `\n${this.fetcher.generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables)}\n`;
|
|
521
|
+
if (this.config.exposeQueryKeys) {
|
|
522
|
+
query += `\n${generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
523
|
+
}
|
|
513
524
|
}
|
|
514
525
|
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
515
526
|
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-react-query",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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"
|
package/variables-generator.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { OperationDefinitionNode } from 'graphql';
|
|
2
2
|
export declare function generateQueryVariablesSignature(hasRequiredVariables: boolean, operationVariablesTypes: string): string;
|
|
3
3
|
export declare function generateInfiniteQueryKey(node: OperationDefinitionNode, hasRequiredVariables: boolean): string;
|
|
4
|
+
export declare function generateInfiniteQueryKeyMaker(node: OperationDefinitionNode, operationName: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
|
|
4
5
|
export declare function generateQueryKey(node: OperationDefinitionNode, hasRequiredVariables: boolean): string;
|
|
5
6
|
export declare function generateQueryKeyMaker(node: OperationDefinitionNode, operationName: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
|
|
6
7
|
export declare function generateMutationKey(node: OperationDefinitionNode): string;
|