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 ? {[pageParamKey]: 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 | }
|