@graphql-codegen/typescript-react-query 3.3.0 → 3.4.1-alpha-bc33c031d.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 +20 -7
- package/index.mjs +20 -7
- package/package.json +3 -3
- 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,13 +64,16 @@ 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);
|
|
66
72
|
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
67
73
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
74
|
+
const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
|
|
68
75
|
const impl = this._isReactHook
|
|
69
|
-
? `(metaData) =>
|
|
76
|
+
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
70
77
|
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
71
78
|
return `export const useInfinite${operationName} = <
|
|
72
79
|
TData = ${operationResultType},
|
|
@@ -74,15 +81,18 @@ class CustomMapperFetcher {
|
|
|
74
81
|
>(
|
|
75
82
|
${variables},
|
|
76
83
|
${options}
|
|
77
|
-
) =>
|
|
78
|
-
${
|
|
84
|
+
) =>{
|
|
85
|
+
${implHookOuter}
|
|
86
|
+
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
79
87
|
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
80
88
|
${impl},
|
|
81
89
|
options
|
|
82
|
-
);`;
|
|
90
|
+
)};`;
|
|
83
91
|
}
|
|
84
92
|
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
85
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';`);
|
|
86
96
|
const hookConfig = this.visitor.queryMethodMap;
|
|
87
97
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.hook);
|
|
88
98
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.options);
|
|
@@ -131,8 +141,8 @@ class CustomMapperFetcher {
|
|
|
131
141
|
return '';
|
|
132
142
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
133
143
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
134
|
-
const impl = `${typedFetcher}(${documentVariableName}, variables)`;
|
|
135
|
-
return `\nuse${operationName}.fetcher = (${variables}) => ${impl};`;
|
|
144
|
+
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
145
|
+
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
136
146
|
}
|
|
137
147
|
}
|
|
138
148
|
|
|
@@ -514,6 +524,9 @@ class ReactQueryVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
514
524
|
}
|
|
515
525
|
if (this.config.addInfiniteQuery) {
|
|
516
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
|
+
}
|
|
517
530
|
}
|
|
518
531
|
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
519
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,13 +58,16 @@ 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);
|
|
60
66
|
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
61
67
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
68
|
+
const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
|
|
62
69
|
const impl = this._isReactHook
|
|
63
|
-
? `(metaData) =>
|
|
70
|
+
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
64
71
|
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
65
72
|
return `export const useInfinite${operationName} = <
|
|
66
73
|
TData = ${operationResultType},
|
|
@@ -68,15 +75,18 @@ class CustomMapperFetcher {
|
|
|
68
75
|
>(
|
|
69
76
|
${variables},
|
|
70
77
|
${options}
|
|
71
|
-
) =>
|
|
72
|
-
${
|
|
78
|
+
) =>{
|
|
79
|
+
${implHookOuter}
|
|
80
|
+
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
73
81
|
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
74
82
|
${impl},
|
|
75
83
|
options
|
|
76
|
-
);`;
|
|
84
|
+
)};`;
|
|
77
85
|
}
|
|
78
86
|
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
79
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';`);
|
|
80
90
|
const hookConfig = this.visitor.queryMethodMap;
|
|
81
91
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.hook);
|
|
82
92
|
this.visitor.reactQueryIdentifiersInUse.add(hookConfig.query.options);
|
|
@@ -125,8 +135,8 @@ class CustomMapperFetcher {
|
|
|
125
135
|
return '';
|
|
126
136
|
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
127
137
|
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
128
|
-
const impl = `${typedFetcher}(${documentVariableName}, variables)`;
|
|
129
|
-
return `\nuse${operationName}.fetcher = (${variables}) => ${impl};`;
|
|
138
|
+
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
139
|
+
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
130
140
|
}
|
|
131
141
|
}
|
|
132
142
|
|
|
@@ -508,6 +518,9 @@ class ReactQueryVisitor extends ClientSideBaseVisitor {
|
|
|
508
518
|
}
|
|
509
519
|
if (this.config.addInfiniteQuery) {
|
|
510
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
|
+
}
|
|
511
524
|
}
|
|
512
525
|
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
513
526
|
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-react-query",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.1-alpha-bc33c031d.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"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "
|
|
10
|
-
"@graphql-codegen/visitor-plugin-common": "2.5.
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "2.3.3-alpha-bc33c031d.0",
|
|
10
|
+
"@graphql-codegen/visitor-plugin-common": "2.5.3-alpha-bc33c031d.0",
|
|
11
11
|
"auto-bind": "~4.0.0",
|
|
12
12
|
"change-case-all": "1.0.14",
|
|
13
13
|
"tslib": "~2.3.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;
|