@graphql-codegen/typescript-react-query 3.5.15 → 3.6.0-alpha-bd464a586.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
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { buildMapperImport, parseMapper } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
+
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey } from './variables-generator.js';
|
|
3
|
+
export class CustomMapperFetcher {
|
|
4
|
+
constructor(visitor, customFetcher) {
|
|
5
|
+
this.visitor = visitor;
|
|
6
|
+
if (typeof customFetcher === 'string') {
|
|
7
|
+
customFetcher = { func: customFetcher };
|
|
8
|
+
}
|
|
9
|
+
this._mapper = parseMapper(customFetcher.func);
|
|
10
|
+
this._isReactHook = customFetcher.isReactHook;
|
|
11
|
+
}
|
|
12
|
+
getFetcherFnName(operationResultType, operationVariablesTypes) {
|
|
13
|
+
return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
|
|
14
|
+
}
|
|
15
|
+
generateFetcherImplementaion() {
|
|
16
|
+
if (this._mapper.isExternal) {
|
|
17
|
+
return buildMapperImport(this._mapper.source, [
|
|
18
|
+
{
|
|
19
|
+
identifier: this._mapper.type,
|
|
20
|
+
asDefault: this._mapper.default,
|
|
21
|
+
},
|
|
22
|
+
], this.visitor.config.useTypeImports);
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
27
|
+
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
28
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
29
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
30
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
31
|
+
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
32
|
+
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
33
|
+
const implHookOuter = this._isReactHook ? `const query = ${typedFetcher}(${documentVariableName})` : '';
|
|
34
|
+
const impl = this._isReactHook
|
|
35
|
+
? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
|
|
36
|
+
: `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
|
|
37
|
+
return `export const useInfinite${operationName} = <
|
|
38
|
+
TData = ${operationResultType},
|
|
39
|
+
TError = ${this.visitor.config.errorType}
|
|
40
|
+
>(
|
|
41
|
+
${variables},
|
|
42
|
+
${options}
|
|
43
|
+
) =>{
|
|
44
|
+
${implHookOuter}
|
|
45
|
+
return ${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
46
|
+
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
47
|
+
${impl},
|
|
48
|
+
options
|
|
49
|
+
)};`;
|
|
50
|
+
}
|
|
51
|
+
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
52
|
+
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
53
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
54
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
55
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
56
|
+
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
57
|
+
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
58
|
+
const impl = this._isReactHook
|
|
59
|
+
? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
|
|
60
|
+
: `${typedFetcher}(${documentVariableName}, variables)`;
|
|
61
|
+
return `export const use${operationName} = <
|
|
62
|
+
TData = ${operationResultType},
|
|
63
|
+
TError = ${this.visitor.config.errorType}
|
|
64
|
+
>(
|
|
65
|
+
${variables},
|
|
66
|
+
${options}
|
|
67
|
+
) =>
|
|
68
|
+
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
69
|
+
${generateQueryKey(node, hasRequiredVariables)},
|
|
70
|
+
${impl},
|
|
71
|
+
options
|
|
72
|
+
);`;
|
|
73
|
+
}
|
|
74
|
+
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
75
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
76
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
77
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
78
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
79
|
+
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
80
|
+
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
81
|
+
const impl = this._isReactHook
|
|
82
|
+
? `${typedFetcher}(${documentVariableName})`
|
|
83
|
+
: `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
|
|
84
|
+
return `export const use${operationName} = <
|
|
85
|
+
TError = ${this.visitor.config.errorType},
|
|
86
|
+
TContext = unknown
|
|
87
|
+
>(${options}) =>
|
|
88
|
+
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
89
|
+
${generateMutationKey(node)},
|
|
90
|
+
${impl},
|
|
91
|
+
options
|
|
92
|
+
);`;
|
|
93
|
+
}
|
|
94
|
+
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
95
|
+
// We can't generate a fetcher field since we can't call react hooks outside of a React Fucntion Component
|
|
96
|
+
// Related: https://reactjs.org/docs/hooks-rules.html
|
|
97
|
+
if (this._isReactHook)
|
|
98
|
+
return '';
|
|
99
|
+
const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
100
|
+
const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
|
|
101
|
+
const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
|
|
102
|
+
return `\nuse${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey, generateQueryVariablesSignature, } from './variables-generator.js';
|
|
2
|
+
export class HardcodedFetchFetcher {
|
|
3
|
+
constructor(visitor, config) {
|
|
4
|
+
this.visitor = visitor;
|
|
5
|
+
this.config = config;
|
|
6
|
+
}
|
|
7
|
+
getEndpoint() {
|
|
8
|
+
try {
|
|
9
|
+
new URL(this.config.endpoint);
|
|
10
|
+
return JSON.stringify(this.config.endpoint);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
return `${this.config.endpoint} as string`;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
getFetchParams() {
|
|
17
|
+
let fetchParamsPartial = '';
|
|
18
|
+
if (this.config.fetchParams) {
|
|
19
|
+
const fetchParamsString = typeof this.config.fetchParams === 'string' ? this.config.fetchParams : JSON.stringify(this.config.fetchParams);
|
|
20
|
+
fetchParamsPartial = `\n ...(${fetchParamsString}),`;
|
|
21
|
+
}
|
|
22
|
+
return ` method: "POST",${fetchParamsPartial}`;
|
|
23
|
+
}
|
|
24
|
+
generateFetcherImplementaion() {
|
|
25
|
+
return `
|
|
26
|
+
function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
|
|
27
|
+
return async (): Promise<TData> => {
|
|
28
|
+
const res = await fetch(${this.getEndpoint()}, {
|
|
29
|
+
${this.getFetchParams()}
|
|
30
|
+
body: JSON.stringify({ query, variables }),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const json = await res.json();
|
|
34
|
+
|
|
35
|
+
if (json.errors) {
|
|
36
|
+
const { message } = json.errors[0];
|
|
37
|
+
|
|
38
|
+
throw new Error(message);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return json.data;
|
|
42
|
+
}
|
|
43
|
+
}`;
|
|
44
|
+
}
|
|
45
|
+
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
46
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
47
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
48
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
49
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
50
|
+
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
51
|
+
return `export const useInfinite${operationName} = <
|
|
52
|
+
TData = ${operationResultType},
|
|
53
|
+
TError = ${this.visitor.config.errorType}
|
|
54
|
+
>(
|
|
55
|
+
pageParamKey: keyof ${operationVariablesTypes},
|
|
56
|
+
${variables},
|
|
57
|
+
${options}
|
|
58
|
+
) =>
|
|
59
|
+
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
60
|
+
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
61
|
+
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
62
|
+
options
|
|
63
|
+
);`;
|
|
64
|
+
}
|
|
65
|
+
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
66
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
67
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
68
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
69
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
70
|
+
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
71
|
+
return `export const use${operationName} = <
|
|
72
|
+
TData = ${operationResultType},
|
|
73
|
+
TError = ${this.visitor.config.errorType}
|
|
74
|
+
>(
|
|
75
|
+
${variables},
|
|
76
|
+
${options}
|
|
77
|
+
) =>
|
|
78
|
+
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
79
|
+
${generateQueryKey(node, hasRequiredVariables)},
|
|
80
|
+
fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables),
|
|
81
|
+
options
|
|
82
|
+
);`;
|
|
83
|
+
}
|
|
84
|
+
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
85
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
86
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
87
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
88
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
89
|
+
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
90
|
+
return `export const use${operationName} = <
|
|
91
|
+
TError = ${this.visitor.config.errorType},
|
|
92
|
+
TContext = unknown
|
|
93
|
+
>(${options}) =>
|
|
94
|
+
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
95
|
+
${generateMutationKey(node)},
|
|
96
|
+
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)(),
|
|
97
|
+
options
|
|
98
|
+
);`;
|
|
99
|
+
}
|
|
100
|
+
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
101
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
102
|
+
return `\nuse${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey, generateQueryVariablesSignature, } from './variables-generator.js';
|
|
2
|
+
export class FetchFetcher {
|
|
3
|
+
constructor(visitor) {
|
|
4
|
+
this.visitor = visitor;
|
|
5
|
+
}
|
|
6
|
+
generateFetcherImplementaion() {
|
|
7
|
+
return `
|
|
8
|
+
function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
|
|
9
|
+
return async (): Promise<TData> => {
|
|
10
|
+
const res = await fetch(endpoint, {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
...requestInit,
|
|
13
|
+
body: JSON.stringify({ query, variables }),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const json = await res.json();
|
|
17
|
+
|
|
18
|
+
if (json.errors) {
|
|
19
|
+
const { message } = json.errors[0];
|
|
20
|
+
|
|
21
|
+
throw new Error(message);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return json.data;
|
|
25
|
+
}
|
|
26
|
+
}`;
|
|
27
|
+
}
|
|
28
|
+
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
29
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
30
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
31
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
32
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
33
|
+
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
34
|
+
return `export const useInfinite${operationName} = <
|
|
35
|
+
TData = ${operationResultType},
|
|
36
|
+
TError = ${this.visitor.config.errorType}
|
|
37
|
+
>(
|
|
38
|
+
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
39
|
+
pageParamKey: keyof ${operationVariablesTypes},
|
|
40
|
+
${variables},
|
|
41
|
+
${options}
|
|
42
|
+
) =>
|
|
43
|
+
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
44
|
+
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
45
|
+
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})(),
|
|
46
|
+
options
|
|
47
|
+
);`;
|
|
48
|
+
}
|
|
49
|
+
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
50
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
51
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
52
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
53
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
54
|
+
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
55
|
+
return `export const use${operationName} = <
|
|
56
|
+
TData = ${operationResultType},
|
|
57
|
+
TError = ${this.visitor.config.errorType}
|
|
58
|
+
>(
|
|
59
|
+
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
60
|
+
${variables},
|
|
61
|
+
${options}
|
|
62
|
+
) =>
|
|
63
|
+
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
64
|
+
${generateQueryKey(node, hasRequiredVariables)},
|
|
65
|
+
fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables),
|
|
66
|
+
options
|
|
67
|
+
);`;
|
|
68
|
+
}
|
|
69
|
+
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
70
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
71
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
72
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
73
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
74
|
+
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
75
|
+
return `export const use${operationName} = <
|
|
76
|
+
TError = ${this.visitor.config.errorType},
|
|
77
|
+
TContext = unknown
|
|
78
|
+
>(
|
|
79
|
+
dataSource: { endpoint: string, fetchParams?: RequestInit },
|
|
80
|
+
${options}
|
|
81
|
+
) =>
|
|
82
|
+
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
83
|
+
${generateMutationKey(node)},
|
|
84
|
+
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)(),
|
|
85
|
+
options
|
|
86
|
+
);`;
|
|
87
|
+
}
|
|
88
|
+
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
89
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
90
|
+
return `\nuse${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { generateInfiniteQueryKey, generateMutationKey, generateQueryKey, generateQueryVariablesSignature, } from './variables-generator.js';
|
|
2
|
+
export class GraphQLRequestClientFetcher {
|
|
3
|
+
constructor(visitor) {
|
|
4
|
+
this.visitor = visitor;
|
|
5
|
+
}
|
|
6
|
+
generateFetcherImplementaion() {
|
|
7
|
+
return `
|
|
8
|
+
function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variables?: TVariables, headers?: RequestInit['headers']) {
|
|
9
|
+
return async (): Promise<TData> => client.request<TData, TVariables>(query, variables, headers);
|
|
10
|
+
}`;
|
|
11
|
+
}
|
|
12
|
+
generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
13
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
14
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
15
|
+
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
16
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
17
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.infiniteQuery.hook);
|
|
18
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.infiniteQuery.options);
|
|
19
|
+
const options = `options?: ${hookConfig.infiniteQuery.options}<${operationResultType}, TError, TData>`;
|
|
20
|
+
return `export const useInfinite${operationName} = <
|
|
21
|
+
TData = ${operationResultType},
|
|
22
|
+
TError = ${this.visitor.config.errorType}
|
|
23
|
+
>(
|
|
24
|
+
pageParamKey: keyof ${operationVariablesTypes},
|
|
25
|
+
client: GraphQLClient,
|
|
26
|
+
${variables},
|
|
27
|
+
${options},
|
|
28
|
+
headers?: RequestInit['headers']
|
|
29
|
+
) =>
|
|
30
|
+
${hookConfig.infiniteQuery.hook}<${operationResultType}, TError, TData>(
|
|
31
|
+
${generateInfiniteQueryKey(node, hasRequiredVariables)},
|
|
32
|
+
(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)(),
|
|
33
|
+
options
|
|
34
|
+
);`;
|
|
35
|
+
}
|
|
36
|
+
generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
37
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
38
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
39
|
+
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
40
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
41
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
42
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.query.hook);
|
|
43
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.query.options);
|
|
44
|
+
const options = `options?: ${hookConfig.query.options}<${operationResultType}, TError, TData>`;
|
|
45
|
+
return `export const use${operationName} = <
|
|
46
|
+
TData = ${operationResultType},
|
|
47
|
+
TError = ${this.visitor.config.errorType}
|
|
48
|
+
>(
|
|
49
|
+
client: GraphQLClient,
|
|
50
|
+
${variables},
|
|
51
|
+
${options},
|
|
52
|
+
headers?: RequestInit['headers']
|
|
53
|
+
) =>
|
|
54
|
+
${hookConfig.query.hook}<${operationResultType}, TError, TData>(
|
|
55
|
+
${generateQueryKey(node, hasRequiredVariables)},
|
|
56
|
+
fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers),
|
|
57
|
+
options
|
|
58
|
+
);`;
|
|
59
|
+
}
|
|
60
|
+
generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
61
|
+
const variables = `variables?: ${operationVariablesTypes}`;
|
|
62
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
63
|
+
this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
|
|
64
|
+
const hookConfig = this.visitor.queryMethodMap;
|
|
65
|
+
this.visitor.reactQueryHookIdentifiersInUse.add(hookConfig.mutation.hook);
|
|
66
|
+
this.visitor.reactQueryOptionsIdentifiersInUse.add(hookConfig.mutation.options);
|
|
67
|
+
const options = `options?: ${hookConfig.mutation.options}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
|
|
68
|
+
return `export const use${operationName} = <
|
|
69
|
+
TError = ${this.visitor.config.errorType},
|
|
70
|
+
TContext = unknown
|
|
71
|
+
>(
|
|
72
|
+
client: GraphQLClient,
|
|
73
|
+
${options},
|
|
74
|
+
headers?: RequestInit['headers']
|
|
75
|
+
) =>
|
|
76
|
+
${hookConfig.mutation.hook}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
|
|
77
|
+
${generateMutationKey(node)},
|
|
78
|
+
(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)(),
|
|
79
|
+
options
|
|
80
|
+
);`;
|
|
81
|
+
}
|
|
82
|
+
generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
83
|
+
const variables = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
84
|
+
const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
|
|
85
|
+
this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
|
|
86
|
+
return `\nuse${operationName}.fetcher = (client: GraphQLClient, ${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers);`;
|
|
87
|
+
}
|
|
88
|
+
}
|
package/esm/fetcher.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Kind, concatAST } from 'graphql';
|
|
2
|
+
import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
3
|
+
import { ReactQueryVisitor } from './visitor.js';
|
|
4
|
+
import { extname } from 'path';
|
|
5
|
+
export const plugin = (schema, documents, config) => {
|
|
6
|
+
const allAst = concatAST(documents.map(v => v.document));
|
|
7
|
+
const allFragments = [
|
|
8
|
+
...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
9
|
+
node: fragmentDef,
|
|
10
|
+
name: fragmentDef.name.value,
|
|
11
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
12
|
+
isExternal: false,
|
|
13
|
+
})),
|
|
14
|
+
...(config.externalFragments || []),
|
|
15
|
+
];
|
|
16
|
+
const visitor = new ReactQueryVisitor(schema, allFragments, config, documents);
|
|
17
|
+
const visitorResult = oldVisit(allAst, { leave: visitor });
|
|
18
|
+
if (visitor.hasOperations) {
|
|
19
|
+
return {
|
|
20
|
+
prepend: [...visitor.getImports(), visitor.getFetcherImplementation()],
|
|
21
|
+
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
prepend: [...visitor.getImports()],
|
|
26
|
+
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export const validate = async (schema, documents, config, outputFile) => {
|
|
30
|
+
if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') {
|
|
31
|
+
throw new Error(`Plugin "typescript-react-query" requires extension to be ".ts" or ".tsx"!`);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export { ReactQueryVisitor };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes) {
|
|
2
|
+
return `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
|
|
3
|
+
}
|
|
4
|
+
export function generateInfiniteQueryKey(node, hasRequiredVariables) {
|
|
5
|
+
if (hasRequiredVariables)
|
|
6
|
+
return `['${node.name.value}.infinite', variables]`;
|
|
7
|
+
return `variables === undefined ? ['${node.name.value}.infinite'] : ['${node.name.value}.infinite', variables]`;
|
|
8
|
+
}
|
|
9
|
+
export function generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables) {
|
|
10
|
+
const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
11
|
+
return `\nuseInfinite${operationName}.getKey = (${signature}) => ${generateInfiniteQueryKey(node, hasRequiredVariables)};\n`;
|
|
12
|
+
}
|
|
13
|
+
export function generateQueryKey(node, hasRequiredVariables) {
|
|
14
|
+
if (hasRequiredVariables)
|
|
15
|
+
return `['${node.name.value}', variables]`;
|
|
16
|
+
return `variables === undefined ? ['${node.name.value}'] : ['${node.name.value}', variables]`;
|
|
17
|
+
}
|
|
18
|
+
export function generateQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables) {
|
|
19
|
+
const signature = generateQueryVariablesSignature(hasRequiredVariables, operationVariablesTypes);
|
|
20
|
+
return `\nuse${operationName}.getKey = (${signature}) => ${generateQueryKey(node, hasRequiredVariables)};\n`;
|
|
21
|
+
}
|
|
22
|
+
export function generateMutationKey(node) {
|
|
23
|
+
return `['${node.name.value}']`;
|
|
24
|
+
}
|
|
25
|
+
export function generateMutationKeyMaker(node, operationName) {
|
|
26
|
+
return `\nuse${operationName}.getKey = () => ${generateMutationKey(node)};\n`;
|
|
27
|
+
}
|
package/esm/visitor.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ClientSideBaseVisitor, DocumentMode, getConfigValue, } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
+
import { generateMutationKeyMaker, generateQueryKeyMaker, generateInfiniteQueryKeyMaker, } from './variables-generator.js';
|
|
3
|
+
import { CustomMapperFetcher } from './fetcher-custom-mapper.js';
|
|
4
|
+
import { FetchFetcher } from './fetcher-fetch.js';
|
|
5
|
+
import { GraphQLRequestClientFetcher } from './fetcher-graphql-request.js';
|
|
6
|
+
import { HardcodedFetchFetcher } from './fetcher-fetch-hardcoded.js';
|
|
7
|
+
import autoBind from 'auto-bind';
|
|
8
|
+
import { pascalCase } from 'change-case-all';
|
|
9
|
+
export class ReactQueryVisitor extends ClientSideBaseVisitor {
|
|
10
|
+
constructor(schema, fragments, rawConfig, documents) {
|
|
11
|
+
super(schema, fragments, rawConfig, {
|
|
12
|
+
documentMode: DocumentMode.string,
|
|
13
|
+
errorType: getConfigValue(rawConfig.errorType, 'unknown'),
|
|
14
|
+
exposeDocument: getConfigValue(rawConfig.exposeDocument, false),
|
|
15
|
+
exposeQueryKeys: getConfigValue(rawConfig.exposeQueryKeys, false),
|
|
16
|
+
exposeMutationKeys: getConfigValue(rawConfig.exposeMutationKeys, false),
|
|
17
|
+
exposeFetcher: getConfigValue(rawConfig.exposeFetcher, false),
|
|
18
|
+
addInfiniteQuery: getConfigValue(rawConfig.addInfiniteQuery, false),
|
|
19
|
+
});
|
|
20
|
+
this.rawConfig = rawConfig;
|
|
21
|
+
this.reactQueryHookIdentifiersInUse = new Set();
|
|
22
|
+
this.reactQueryOptionsIdentifiersInUse = new Set();
|
|
23
|
+
this.queryMethodMap = {
|
|
24
|
+
infiniteQuery: {
|
|
25
|
+
hook: 'useInfiniteQuery',
|
|
26
|
+
options: 'UseInfiniteQueryOptions',
|
|
27
|
+
},
|
|
28
|
+
query: {
|
|
29
|
+
hook: 'useQuery',
|
|
30
|
+
options: 'UseQueryOptions',
|
|
31
|
+
},
|
|
32
|
+
mutation: {
|
|
33
|
+
hook: 'useMutation',
|
|
34
|
+
options: 'UseMutationOptions',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
|
|
38
|
+
this._documents = documents;
|
|
39
|
+
this.fetcher = this.createFetcher(rawConfig.fetcher || 'fetch');
|
|
40
|
+
autoBind(this);
|
|
41
|
+
}
|
|
42
|
+
get imports() {
|
|
43
|
+
return this._imports;
|
|
44
|
+
}
|
|
45
|
+
createFetcher(raw) {
|
|
46
|
+
if (raw === 'fetch') {
|
|
47
|
+
return new FetchFetcher(this);
|
|
48
|
+
}
|
|
49
|
+
if (typeof raw === 'object' && 'endpoint' in raw) {
|
|
50
|
+
return new HardcodedFetchFetcher(this, raw);
|
|
51
|
+
}
|
|
52
|
+
if (raw === 'graphql-request') {
|
|
53
|
+
return new GraphQLRequestClientFetcher(this);
|
|
54
|
+
}
|
|
55
|
+
return new CustomMapperFetcher(this, raw);
|
|
56
|
+
}
|
|
57
|
+
get hasOperations() {
|
|
58
|
+
return this._collectedOperations.length > 0;
|
|
59
|
+
}
|
|
60
|
+
getImports() {
|
|
61
|
+
const baseImports = super.getImports();
|
|
62
|
+
if (!this.hasOperations) {
|
|
63
|
+
return baseImports;
|
|
64
|
+
}
|
|
65
|
+
if (this.config.addInfiniteQuery) {
|
|
66
|
+
this.reactQueryOptionsIdentifiersInUse.add('QueryFunctionContext');
|
|
67
|
+
}
|
|
68
|
+
const hookAndTypeImports = [
|
|
69
|
+
...Array.from(this.reactQueryHookIdentifiersInUse),
|
|
70
|
+
...Array.from(this.reactQueryOptionsIdentifiersInUse).map(identifier => `${this.config.useTypeImports ? 'type ' : ''}${identifier}`),
|
|
71
|
+
];
|
|
72
|
+
return [...baseImports, `import { ${hookAndTypeImports.join(', ')} } from 'react-query';`];
|
|
73
|
+
}
|
|
74
|
+
getFetcherImplementation() {
|
|
75
|
+
return this.fetcher.generateFetcherImplementaion();
|
|
76
|
+
}
|
|
77
|
+
_getHookSuffix(name, operationType) {
|
|
78
|
+
if (this.config.omitOperationSuffix) {
|
|
79
|
+
return '';
|
|
80
|
+
}
|
|
81
|
+
if (!this.config.dedupeOperationSuffix) {
|
|
82
|
+
return pascalCase(operationType);
|
|
83
|
+
}
|
|
84
|
+
if (name.includes('Query') || name.includes('Mutation') || name.includes('Subscription')) {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
return pascalCase(operationType);
|
|
88
|
+
}
|
|
89
|
+
buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) {
|
|
90
|
+
var _a, _b;
|
|
91
|
+
const nodeName = (_b = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '';
|
|
92
|
+
const suffix = this._getHookSuffix(nodeName, operationType);
|
|
93
|
+
const operationName = this.convertName(nodeName, {
|
|
94
|
+
suffix,
|
|
95
|
+
useTypesPrefix: false,
|
|
96
|
+
useTypesSuffix: false,
|
|
97
|
+
});
|
|
98
|
+
operationResultType = this._externalImportPrefix + operationResultType;
|
|
99
|
+
operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
|
|
100
|
+
if (operationType === 'Query') {
|
|
101
|
+
let query = this.fetcher.generateQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
102
|
+
if (this.config.exposeDocument) {
|
|
103
|
+
query += `\nuse${operationName}.document = ${documentVariableName};\n`;
|
|
104
|
+
}
|
|
105
|
+
if (this.config.exposeQueryKeys) {
|
|
106
|
+
query += `\n${generateQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
107
|
+
}
|
|
108
|
+
if (this.config.addInfiniteQuery) {
|
|
109
|
+
query += `\n${this.fetcher.generateInfiniteQueryHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables)}\n`;
|
|
110
|
+
if (this.config.exposeQueryKeys) {
|
|
111
|
+
query += `\n${generateInfiniteQueryKeyMaker(node, operationName, operationVariablesTypes, hasRequiredVariables)};\n`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// The reason we're looking at the private field of the CustomMapperFetcher to see if it's a react hook
|
|
115
|
+
// is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
|
|
116
|
+
// a fetcher field anyways.
|
|
117
|
+
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
118
|
+
query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
119
|
+
}
|
|
120
|
+
return query;
|
|
121
|
+
}
|
|
122
|
+
if (operationType === 'Mutation') {
|
|
123
|
+
let query = this.fetcher.generateMutationHook(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
124
|
+
if (this.config.exposeMutationKeys) {
|
|
125
|
+
query += generateMutationKeyMaker(node, operationName);
|
|
126
|
+
}
|
|
127
|
+
if (this.config.exposeFetcher && !this.fetcher._isReactHook) {
|
|
128
|
+
query += this.fetcher.generateFetcherFetch(node, documentVariableName, operationName, operationResultType, operationVariablesTypes, hasRequiredVariables);
|
|
129
|
+
}
|
|
130
|
+
return query;
|
|
131
|
+
}
|
|
132
|
+
if (operationType === 'Subscription') {
|
|
133
|
+
// eslint-disable-next-line no-console
|
|
134
|
+
console.warn(`Plugin "typescript-react-query" does not support GraphQL Subscriptions at the moment! Ignoring "${node.name.value}"...`);
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-react-query",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-alpha-bd464a586.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": "^2.
|
|
10
|
-
"@graphql-codegen/visitor-plugin-common": "2.
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "^2.5.0-alpha-bd464a586.0",
|
|
10
|
+
"@graphql-codegen/visitor-plugin-common": "2.11.0-alpha-bd464a586.0",
|
|
11
11
|
"auto-bind": "~4.0.0",
|
|
12
12
|
"change-case-all": "1.0.14",
|
|
13
13
|
"tslib": "~2.4.0"
|
|
@@ -18,21 +18,28 @@
|
|
|
18
18
|
"directory": "packages/plugins/typescript/react-query"
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"main": "index.js",
|
|
22
|
-
"module": "index.
|
|
23
|
-
"typings": "index.d.ts",
|
|
21
|
+
"main": "cjs/index.js",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"typings": "typings/index.d.ts",
|
|
24
24
|
"typescript": {
|
|
25
|
-
"definition": "index.d.ts"
|
|
25
|
+
"definition": "typings/index.d.ts"
|
|
26
26
|
},
|
|
27
|
+
"type": "module",
|
|
27
28
|
"exports": {
|
|
28
|
-
"./package.json": "./package.json",
|
|
29
29
|
".": {
|
|
30
|
-
"require":
|
|
31
|
-
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./typings/index.d.ts",
|
|
32
|
+
"default": "./cjs/index.js"
|
|
33
|
+
},
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./typings/index.d.ts",
|
|
36
|
+
"default": "./esm/index.js"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./typings/index.d.ts",
|
|
40
|
+
"default": "./esm/index.js"
|
|
41
|
+
}
|
|
32
42
|
},
|
|
33
|
-
"
|
|
34
|
-
"require": "./*.js",
|
|
35
|
-
"import": "./*.mjs"
|
|
36
|
-
}
|
|
43
|
+
"./package.json": "./package.json"
|
|
37
44
|
}
|
|
38
|
-
}
|
|
45
|
+
}
|
|
File without changes
|