@graphql-codegen/typescript-solid-query 1.0.0-alpha-20240201110240-bdef5b40fe364d6a5b0276786062c582bd60d3cc

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/visitor.js ADDED
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolidQueryVisitor = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ const change_case_all_1 = require("change-case-all");
7
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
8
+ const fetcher_custom_mapper_js_1 = require("./fetcher-custom-mapper.js");
9
+ const fetcher_fetch_hardcoded_js_1 = require("./fetcher-fetch-hardcoded.js");
10
+ const fetcher_fetch_js_1 = require("./fetcher-fetch.js");
11
+ const fetcher_graphql_request_js_1 = require("./fetcher-graphql-request.js");
12
+ class SolidQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
13
+ constructor(schema, fragments, rawConfig, documents) {
14
+ super(schema, fragments, rawConfig, {
15
+ documentMode: visitor_plugin_common_1.DocumentMode.string,
16
+ errorType: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.errorType, 'unknown'),
17
+ exposeDocument: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeDocument, false),
18
+ exposeQueryKeys: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeQueryKeys, false),
19
+ exposeQueryRootKeys: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeQueryRootKeys, false),
20
+ exposeMutationKeys: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeMutationKeys, false),
21
+ exposeFetcher: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exposeFetcher, false),
22
+ addInfiniteQuery: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addInfiniteQuery, false),
23
+ addSuspenseQuery: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addSuspenseQuery, false),
24
+ solidQueryImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.solidQueryImportFrom, ''),
25
+ });
26
+ this.rawConfig = rawConfig;
27
+ this.solidQueryHookIdentifiersInUse = new Set();
28
+ this.solidQueryOptionsIdentifiersInUse = new Set();
29
+ this._externalImportPrefix = this.config.importOperationTypesFrom
30
+ ? `${this.config.importOperationTypesFrom}.`
31
+ : '';
32
+ this._documents = documents;
33
+ this.fetcher = this.createFetcher(rawConfig.fetcher || 'fetch');
34
+ (0, auto_bind_1.default)(this);
35
+ }
36
+ get imports() {
37
+ return this._imports;
38
+ }
39
+ createFetcher(raw) {
40
+ if (raw === 'fetch') {
41
+ return new fetcher_fetch_js_1.FetchFetcher(this);
42
+ }
43
+ if (typeof raw === 'object' && 'endpoint' in raw) {
44
+ return new fetcher_fetch_hardcoded_js_1.HardcodedFetchFetcher(this, raw);
45
+ }
46
+ if (raw === 'graphql-request' || (typeof raw === 'object' && 'clientImportPath' in raw)) {
47
+ return new fetcher_graphql_request_js_1.GraphQLRequestClientFetcher(this, raw);
48
+ }
49
+ return new fetcher_custom_mapper_js_1.CustomMapperFetcher(this, raw);
50
+ }
51
+ get hasOperations() {
52
+ return this._collectedOperations.length > 0;
53
+ }
54
+ getImports() {
55
+ const baseImports = super.getImports();
56
+ if (!this.hasOperations) {
57
+ return baseImports;
58
+ }
59
+ const hookAndTypeImports = [
60
+ ...Array.from(this.solidQueryHookIdentifiersInUse),
61
+ ...Array.from(this.solidQueryOptionsIdentifiersInUse).map(identifier => `${this.config.useTypeImports ? 'type ' : ''}${identifier}`),
62
+ ];
63
+ const moduleName = this.config.solidQueryImportFrom
64
+ ? this.config.solidQueryImportFrom
65
+ : '@tanstack/solid-query';
66
+ return [...baseImports, `import { ${hookAndTypeImports.join(', ')} } from '${moduleName}';`];
67
+ }
68
+ getFetcherImplementation() {
69
+ return this.fetcher.generateFetcherImplementation();
70
+ }
71
+ _getHookSuffix(name, operationType) {
72
+ if (this.config.omitOperationSuffix) {
73
+ return '';
74
+ }
75
+ if (!this.config.dedupeOperationSuffix) {
76
+ return (0, change_case_all_1.pascalCase)(operationType);
77
+ }
78
+ if (name.includes('Query') || name.includes('Mutation') || name.includes('Subscription')) {
79
+ return '';
80
+ }
81
+ return (0, change_case_all_1.pascalCase)(operationType);
82
+ }
83
+ buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) {
84
+ var _a, _b;
85
+ const nodeName = (_b = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '';
86
+ const suffix = this._getHookSuffix(nodeName, operationType);
87
+ const operationName = this.convertName(nodeName, {
88
+ suffix,
89
+ useTypesPrefix: false,
90
+ useTypesSuffix: false,
91
+ });
92
+ const generateConfig = {
93
+ node,
94
+ documentVariableName,
95
+ operationResultType,
96
+ operationVariablesTypes,
97
+ hasRequiredVariables,
98
+ operationName,
99
+ };
100
+ operationResultType = this._externalImportPrefix + operationResultType;
101
+ operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
102
+ const queries = [];
103
+ const getOutputFromQueries = () => `\n${queries.join('\n\n')}\n`;
104
+ if (operationType === 'Query') {
105
+ const addQuery = (generateConfig, isSuspense = false) => {
106
+ const { hook, getKey, rootKey, document } = this.fetcher.generateQueryOutput(generateConfig, isSuspense);
107
+ queries.push(hook);
108
+ if (this.config.exposeDocument)
109
+ queries.push(document);
110
+ if (this.config.exposeQueryKeys)
111
+ queries.push(getKey);
112
+ if (this.config.exposeQueryRootKeys)
113
+ queries.push(rootKey);
114
+ };
115
+ addQuery(generateConfig);
116
+ if (this.config.addSuspenseQuery)
117
+ addQuery(generateConfig, true);
118
+ if (this.config.addInfiniteQuery) {
119
+ const addInfiniteQuery = (generateConfig, isSuspense = false) => {
120
+ const { hook, getKey, rootKey } = this.fetcher.generateInfiniteQueryOutput(generateConfig, isSuspense);
121
+ queries.push(hook);
122
+ if (this.config.exposeQueryKeys)
123
+ queries.push(getKey);
124
+ if (this.config.exposeQueryRootKeys)
125
+ queries.push(rootKey);
126
+ };
127
+ addInfiniteQuery(generateConfig);
128
+ if (this.config.addSuspenseQuery) {
129
+ addInfiniteQuery(generateConfig, true);
130
+ }
131
+ }
132
+ // The reason we're looking at the private field of the CustomMapperFetcher to see if it's a solid hook
133
+ // is to prevent calling generateFetcherFetch for each query since all the queries won't be able to generate
134
+ // a fetcher field anyways.
135
+ if (this.config.exposeFetcher && !this.fetcher._isSolidHook) {
136
+ queries.push(this.fetcher.generateFetcherFetch(generateConfig));
137
+ }
138
+ return getOutputFromQueries();
139
+ }
140
+ if (operationType === 'Mutation') {
141
+ const { hook, getKey } = this.fetcher.generateMutationOutput(generateConfig);
142
+ queries.push(hook);
143
+ if (this.config.exposeMutationKeys)
144
+ queries.push(getKey);
145
+ if (this.config.exposeFetcher && !this.fetcher._isSolidHook) {
146
+ queries.push(this.fetcher.generateFetcherFetch(generateConfig));
147
+ }
148
+ return getOutputFromQueries();
149
+ }
150
+ if (operationType === 'Subscription') {
151
+ // eslint-disable-next-line no-console
152
+ console.warn(`Plugin "typescript-solid-query" does not support GraphQL Subscriptions at the moment! Ignoring "${node.name.value}"...`);
153
+ }
154
+ return null;
155
+ }
156
+ }
157
+ exports.SolidQueryVisitor = SolidQueryVisitor;
package/esm/config.js ADDED
File without changes
@@ -0,0 +1,77 @@
1
+ import autoBind from 'auto-bind';
2
+ import { buildMapperImport, parseMapper, } from '@graphql-codegen/visitor-plugin-common';
3
+ import { FetcherRenderer } from './fetcher.js';
4
+ export class CustomMapperFetcher extends FetcherRenderer {
5
+ constructor(visitor, customFetcher) {
6
+ super(visitor);
7
+ this.visitor = visitor;
8
+ if (typeof customFetcher === 'string') {
9
+ customFetcher = { func: customFetcher };
10
+ }
11
+ this._mapper = parseMapper(customFetcher.func);
12
+ this._isSolidHook = customFetcher.isSolidHook;
13
+ autoBind(this);
14
+ }
15
+ getFetcherFnName(operationResultType, operationVariablesTypes) {
16
+ return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
17
+ }
18
+ generateFetcherImplementation() {
19
+ if (this._mapper.isExternal) {
20
+ return buildMapperImport(this._mapper.source, [
21
+ {
22
+ identifier: this._mapper.type,
23
+ asDefault: this._mapper.default,
24
+ },
25
+ ], this.visitor.config.useTypeImports);
26
+ }
27
+ return null;
28
+ }
29
+ generateInfiniteQueryHook(config, isSuspense = false) {
30
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
31
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
32
+ const implHookOuter = this._isSolidHook
33
+ ? `const query = ${typedFetcher}(${documentVariableName})`
34
+ : '';
35
+ const implFetcher = this._isSolidHook
36
+ ? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
37
+ : `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
38
+ const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
39
+ return generateBaseInfiniteQueryHook({
40
+ implHookOuter,
41
+ implFetcher,
42
+ });
43
+ }
44
+ generateQueryHook(config, isSuspense = false) {
45
+ const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
46
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
47
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
48
+ const implFetcher = this._isSolidHook
49
+ ? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
50
+ : `${typedFetcher}(${documentVariableName}, variables)`;
51
+ return generateBaseQueryHook({
52
+ implFetcher,
53
+ });
54
+ }
55
+ generateMutationHook(config) {
56
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
57
+ const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
58
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
59
+ const implFetcher = this._isSolidHook
60
+ ? `${typedFetcher}(${documentVariableName})`
61
+ : `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
62
+ return generateBaseMutationHook({
63
+ implFetcher,
64
+ });
65
+ }
66
+ generateFetcherFetch(config) {
67
+ const { documentVariableName, operationResultType, operationVariablesTypes, hasRequiredVariables, operationName, } = config;
68
+ // We can't generate a fetcher field since we can't call solid hooks outside of a Solid Fucntion Component
69
+ // Related: https://solidjs.org/docs/hooks-rules.html
70
+ if (this._isSolidHook)
71
+ return '';
72
+ const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
73
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
74
+ const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
75
+ return `\ncreate${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
76
+ }
77
+ }
@@ -0,0 +1,76 @@
1
+ import autoBind from 'auto-bind';
2
+ import { FetcherRenderer } from './fetcher.js';
3
+ export class HardcodedFetchFetcher extends FetcherRenderer {
4
+ constructor(visitor, config) {
5
+ super(visitor);
6
+ this.visitor = visitor;
7
+ this.config = config;
8
+ autoBind(this);
9
+ }
10
+ getEndpoint() {
11
+ try {
12
+ new URL(this.config.endpoint);
13
+ return JSON.stringify(this.config.endpoint);
14
+ }
15
+ catch (e) {
16
+ return `${this.config.endpoint} as string`;
17
+ }
18
+ }
19
+ getFetchParams() {
20
+ let fetchParamsPartial = '';
21
+ if (this.config.fetchParams) {
22
+ const fetchParamsString = typeof this.config.fetchParams === 'string'
23
+ ? this.config.fetchParams
24
+ : JSON.stringify(this.config.fetchParams);
25
+ fetchParamsPartial = `\n ...(${fetchParamsString}),`;
26
+ }
27
+ return ` method: "POST",${fetchParamsPartial}`;
28
+ }
29
+ generateFetcherImplementation() {
30
+ return `
31
+ function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
32
+ return async (): Promise<TData> => {
33
+ const res = await fetch(${this.getEndpoint()}, {
34
+ ${this.getFetchParams()}
35
+ body: JSON.stringify({ query, variables }),
36
+ });
37
+
38
+ const json = await res.json();
39
+
40
+ if (json.errors) {
41
+ const { message } = json.errors[0];
42
+
43
+ throw new Error(message);
44
+ }
45
+
46
+ return json.data;
47
+ }
48
+ }`;
49
+ }
50
+ generateInfiniteQueryHook(config, isSuspense = false) {
51
+ const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
52
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
53
+ return generateBaseInfiniteQueryHook({
54
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
55
+ });
56
+ }
57
+ generateQueryHook(config, isSuspense = false) {
58
+ const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
59
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
60
+ return generateBaseQueryHook({
61
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`,
62
+ });
63
+ }
64
+ generateMutationHook(config) {
65
+ const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
66
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
67
+ return generateBaseMutationHook({
68
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)()`,
69
+ });
70
+ }
71
+ generateFetcherFetch(config) {
72
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
73
+ const variables = this.generateQueryVariablesSignature(config);
74
+ return `\ncreate${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
75
+ }
76
+ }
@@ -0,0 +1,71 @@
1
+ import autoBind from 'auto-bind';
2
+ import { FetcherRenderer } from './fetcher.js';
3
+ export class FetchFetcher extends FetcherRenderer {
4
+ constructor(visitor) {
5
+ super(visitor);
6
+ this.visitor = visitor;
7
+ autoBind(this);
8
+ }
9
+ generateFetcherImplementation() {
10
+ return `
11
+ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
12
+ return async (): Promise<TData> => {
13
+ const res = await fetch(endpoint, {
14
+ method: 'POST',
15
+ ...requestInit,
16
+ body: JSON.stringify({ query, variables }),
17
+ });
18
+
19
+ const json = await res.json();
20
+
21
+ if (json.errors) {
22
+ const { message } = json.errors[0];
23
+
24
+ throw new Error(message);
25
+ }
26
+
27
+ return json.data;
28
+ }
29
+ }`;
30
+ }
31
+ generateInfiniteQueryHook(config, isSuspense = false) {
32
+ const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
33
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
34
+ return generateBaseInfiniteQueryHook({
35
+ implArguments: `
36
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
37
+ ${variables},
38
+ ${options}
39
+ `,
40
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
41
+ });
42
+ }
43
+ generateQueryHook(config, isSuspense = false) {
44
+ const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
45
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
46
+ return generateBaseQueryHook({
47
+ implArguments: `
48
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
49
+ ${variables},
50
+ ${options}
51
+ `,
52
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)`,
53
+ });
54
+ }
55
+ generateMutationHook(config) {
56
+ const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
57
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
58
+ return generateBaseMutationHook({
59
+ implArguments: `
60
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
61
+ ${options}
62
+ `,
63
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)()`,
64
+ });
65
+ }
66
+ generateFetcherFetch(config) {
67
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
68
+ const variables = this.generateQueryVariablesSignature(config);
69
+ return `\ncreate${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
70
+ }
71
+ }
@@ -0,0 +1,118 @@
1
+ import autoBind from 'auto-bind';
2
+ import { FetcherRenderer } from './fetcher.js';
3
+ export class GraphQLRequestClientFetcher extends FetcherRenderer {
4
+ constructor(visitor, config) {
5
+ super(visitor);
6
+ this.visitor = visitor;
7
+ this.clientPath = typeof config === 'object' ? config.clientImportPath : null;
8
+ autoBind(this);
9
+ }
10
+ generateFetcherImplementation() {
11
+ return this.clientPath
12
+ ? `
13
+ function fetcher<TData, TVariables extends { [key: string]: any }>(query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
14
+ return async (): Promise<TData> => graphqlClient.request({
15
+ document: query,
16
+ variables,
17
+ requestHeaders
18
+ });
19
+ }`
20
+ : `
21
+ function fetcher<TData, TVariables extends { [key: string]: any }>(client: GraphQLClient, query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
22
+ return async (): Promise<TData> => client.request({
23
+ document: query,
24
+ variables,
25
+ requestHeaders
26
+ });
27
+ }`;
28
+ }
29
+ generateInfiniteQueryHook(config, isSuspense = false) {
30
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
31
+ if (this.clientPath)
32
+ this.visitor.imports.add(this.clientPath);
33
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
34
+ const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
35
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
36
+ return this.clientPath
37
+ ? generateBaseInfiniteQueryHook({
38
+ implArguments: `
39
+ pageParamKey: keyof ${operationVariablesTypes},
40
+ ${variables},
41
+ ${options},
42
+ headers?: RequestInit['headers']
43
+ `,
44
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam}, headers)()`,
45
+ })
46
+ : generateBaseInfiniteQueryHook({
47
+ implArguments: `
48
+ client: GraphQLClient,
49
+ ${variables},
50
+ ${options},
51
+ headers?: RequestInit['headers']
52
+ `,
53
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)()`,
54
+ });
55
+ }
56
+ generateQueryHook(config, isSuspense = false) {
57
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
58
+ if (this.clientPath)
59
+ this.visitor.imports.add(this.clientPath);
60
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
61
+ this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
62
+ const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
63
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
64
+ return this.clientPath
65
+ ? generateBaseQueryHook({
66
+ implArguments: `
67
+ ${variables},
68
+ ${options},
69
+ headers?: RequestInit['headers']
70
+ `,
71
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`,
72
+ })
73
+ : generateBaseQueryHook({
74
+ implArguments: `
75
+ client: GraphQLClient,
76
+ ${variables},
77
+ ${options},
78
+ headers?: RequestInit['headers']
79
+ `,
80
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`,
81
+ });
82
+ }
83
+ generateMutationHook(config) {
84
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
85
+ if (this.clientPath)
86
+ this.visitor.imports.add(this.clientPath);
87
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
88
+ const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
89
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
90
+ return this.clientPath
91
+ ? generateBaseMutationHook({
92
+ implArguments: `
93
+ ${options},
94
+ headers?: RequestInit['headers']
95
+ `,
96
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)()`,
97
+ })
98
+ : generateBaseMutationHook({
99
+ implArguments: `
100
+ client: GraphQLClient,
101
+ ${options},
102
+ headers?: RequestInit['headers']
103
+ `,
104
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)()`,
105
+ });
106
+ }
107
+ generateFetcherFetch(config) {
108
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
109
+ const variables = this.generateQueryVariablesSignature(config);
110
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
111
+ if (this.clientPath)
112
+ this.visitor.imports.add(this.clientPath);
113
+ this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
114
+ return this.clientPath
115
+ ? `\ncreate${operationName}.fetcher = (${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers);`
116
+ : `\ncreate${operationName}.fetcher = (client: GraphQLClient, ${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers);`;
117
+ }
118
+ }