@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/config.js ADDED
File without changes
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomMapperFetcher = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
7
+ const fetcher_js_1 = require("./fetcher.js");
8
+ class CustomMapperFetcher extends fetcher_js_1.FetcherRenderer {
9
+ constructor(visitor, customFetcher) {
10
+ super(visitor);
11
+ this.visitor = visitor;
12
+ if (typeof customFetcher === 'string') {
13
+ customFetcher = { func: customFetcher };
14
+ }
15
+ this._mapper = (0, visitor_plugin_common_1.parseMapper)(customFetcher.func);
16
+ this._isSolidHook = customFetcher.isSolidHook;
17
+ (0, auto_bind_1.default)(this);
18
+ }
19
+ getFetcherFnName(operationResultType, operationVariablesTypes) {
20
+ return `${this._mapper.type}<${operationResultType}, ${operationVariablesTypes}>`;
21
+ }
22
+ generateFetcherImplementation() {
23
+ if (this._mapper.isExternal) {
24
+ return (0, visitor_plugin_common_1.buildMapperImport)(this._mapper.source, [
25
+ {
26
+ identifier: this._mapper.type,
27
+ asDefault: this._mapper.default,
28
+ },
29
+ ], this.visitor.config.useTypeImports);
30
+ }
31
+ return null;
32
+ }
33
+ generateInfiniteQueryHook(config, isSuspense = false) {
34
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
35
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
36
+ const implHookOuter = this._isSolidHook
37
+ ? `const query = ${typedFetcher}(${documentVariableName})`
38
+ : '';
39
+ const implFetcher = this._isSolidHook
40
+ ? `(metaData) => query({...variables, ...(metaData.pageParam ?? {})})`
41
+ : `(metaData) => ${typedFetcher}(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`;
42
+ const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
43
+ return generateBaseInfiniteQueryHook({
44
+ implHookOuter,
45
+ implFetcher,
46
+ });
47
+ }
48
+ generateQueryHook(config, isSuspense = false) {
49
+ const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
50
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
51
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
52
+ const implFetcher = this._isSolidHook
53
+ ? `${typedFetcher}(${documentVariableName}).bind(null, variables)`
54
+ : `${typedFetcher}(${documentVariableName}, variables)`;
55
+ return generateBaseQueryHook({
56
+ implFetcher,
57
+ });
58
+ }
59
+ generateMutationHook(config) {
60
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
61
+ const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
62
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
63
+ const implFetcher = this._isSolidHook
64
+ ? `${typedFetcher}(${documentVariableName})`
65
+ : `(${variables}) => ${typedFetcher}(${documentVariableName}, variables)()`;
66
+ return generateBaseMutationHook({
67
+ implFetcher,
68
+ });
69
+ }
70
+ generateFetcherFetch(config) {
71
+ const { documentVariableName, operationResultType, operationVariablesTypes, hasRequiredVariables, operationName, } = config;
72
+ // We can't generate a fetcher field since we can't call solid hooks outside of a Solid Fucntion Component
73
+ // Related: https://solidjs.org/docs/hooks-rules.html
74
+ if (this._isSolidHook)
75
+ return '';
76
+ const variables = `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
77
+ const typedFetcher = this.getFetcherFnName(operationResultType, operationVariablesTypes);
78
+ const impl = `${typedFetcher}(${documentVariableName}, variables, options)`;
79
+ return `\ncreate${operationName}.fetcher = (${variables}, options?: RequestInit['headers']) => ${impl};`;
80
+ }
81
+ }
82
+ exports.CustomMapperFetcher = CustomMapperFetcher;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HardcodedFetchFetcher = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ const fetcher_js_1 = require("./fetcher.js");
7
+ class HardcodedFetchFetcher extends fetcher_js_1.FetcherRenderer {
8
+ constructor(visitor, config) {
9
+ super(visitor);
10
+ this.visitor = visitor;
11
+ this.config = config;
12
+ (0, auto_bind_1.default)(this);
13
+ }
14
+ getEndpoint() {
15
+ try {
16
+ new URL(this.config.endpoint);
17
+ return JSON.stringify(this.config.endpoint);
18
+ }
19
+ catch (e) {
20
+ return `${this.config.endpoint} as string`;
21
+ }
22
+ }
23
+ getFetchParams() {
24
+ let fetchParamsPartial = '';
25
+ if (this.config.fetchParams) {
26
+ const fetchParamsString = typeof this.config.fetchParams === 'string'
27
+ ? this.config.fetchParams
28
+ : JSON.stringify(this.config.fetchParams);
29
+ fetchParamsPartial = `\n ...(${fetchParamsString}),`;
30
+ }
31
+ return ` method: "POST",${fetchParamsPartial}`;
32
+ }
33
+ generateFetcherImplementation() {
34
+ return `
35
+ function fetcher<TData, TVariables>(query: string, variables?: TVariables) {
36
+ return async (): Promise<TData> => {
37
+ const res = await fetch(${this.getEndpoint()}, {
38
+ ${this.getFetchParams()}
39
+ body: JSON.stringify({ query, variables }),
40
+ });
41
+
42
+ const json = await res.json();
43
+
44
+ if (json.errors) {
45
+ const { message } = json.errors[0];
46
+
47
+ throw new Error(message);
48
+ }
49
+
50
+ return json.data;
51
+ }
52
+ }`;
53
+ }
54
+ generateInfiniteQueryHook(config, isSuspense = false) {
55
+ const { generateBaseInfiniteQueryHook } = this.generateInfiniteQueryHelper(config, isSuspense);
56
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
57
+ return generateBaseInfiniteQueryHook({
58
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
59
+ });
60
+ }
61
+ generateQueryHook(config, isSuspense = false) {
62
+ const { generateBaseQueryHook } = this.generateQueryHelper(config, isSuspense);
63
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
64
+ return generateBaseQueryHook({
65
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)`,
66
+ });
67
+ }
68
+ generateMutationHook(config) {
69
+ const { generateBaseMutationHook, variables } = this.generateMutationHelper(config);
70
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
71
+ return generateBaseMutationHook({
72
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables)()`,
73
+ });
74
+ }
75
+ generateFetcherFetch(config) {
76
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
77
+ const variables = this.generateQueryVariablesSignature(config);
78
+ return `\ncreate${operationName}.fetcher = (${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables);`;
79
+ }
80
+ }
81
+ exports.HardcodedFetchFetcher = HardcodedFetchFetcher;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FetchFetcher = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ const fetcher_js_1 = require("./fetcher.js");
7
+ class FetchFetcher extends fetcher_js_1.FetcherRenderer {
8
+ constructor(visitor) {
9
+ super(visitor);
10
+ this.visitor = visitor;
11
+ (0, auto_bind_1.default)(this);
12
+ }
13
+ generateFetcherImplementation() {
14
+ return `
15
+ function fetcher<TData, TVariables>(endpoint: string, requestInit: RequestInit, query: string, variables?: TVariables) {
16
+ return async (): Promise<TData> => {
17
+ const res = await fetch(endpoint, {
18
+ method: 'POST',
19
+ ...requestInit,
20
+ body: JSON.stringify({ query, variables }),
21
+ });
22
+
23
+ const json = await res.json();
24
+
25
+ if (json.errors) {
26
+ const { message } = json.errors[0];
27
+
28
+ throw new Error(message);
29
+ }
30
+
31
+ return json.data;
32
+ }
33
+ }`;
34
+ }
35
+ generateInfiniteQueryHook(config, isSuspense = false) {
36
+ const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
37
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
38
+ return generateBaseInfiniteQueryHook({
39
+ implArguments: `
40
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
41
+ ${variables},
42
+ ${options}
43
+ `,
44
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})})()`,
45
+ });
46
+ }
47
+ generateQueryHook(config, isSuspense = false) {
48
+ const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
49
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
50
+ return generateBaseQueryHook({
51
+ implArguments: `
52
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
53
+ ${variables},
54
+ ${options}
55
+ `,
56
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)`,
57
+ });
58
+ }
59
+ generateMutationHook(config) {
60
+ const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
61
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
62
+ return generateBaseMutationHook({
63
+ implArguments: `
64
+ dataSource: { endpoint: string, fetchParams?: RequestInit },
65
+ ${options}
66
+ `,
67
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables)()`,
68
+ });
69
+ }
70
+ generateFetcherFetch(config) {
71
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
72
+ const variables = this.generateQueryVariablesSignature(config);
73
+ return `\ncreate${operationName}.fetcher = (dataSource: { endpoint: string, fetchParams?: RequestInit }, ${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(dataSource.endpoint, dataSource.fetchParams || {}, ${documentVariableName}, variables);`;
74
+ }
75
+ }
76
+ exports.FetchFetcher = FetchFetcher;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphQLRequestClientFetcher = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ const fetcher_js_1 = require("./fetcher.js");
7
+ class GraphQLRequestClientFetcher extends fetcher_js_1.FetcherRenderer {
8
+ constructor(visitor, config) {
9
+ super(visitor);
10
+ this.visitor = visitor;
11
+ this.clientPath = typeof config === 'object' ? config.clientImportPath : null;
12
+ (0, auto_bind_1.default)(this);
13
+ }
14
+ generateFetcherImplementation() {
15
+ return this.clientPath
16
+ ? `
17
+ function fetcher<TData, TVariables extends { [key: string]: any }>(query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
18
+ return async (): Promise<TData> => graphqlClient.request({
19
+ document: query,
20
+ variables,
21
+ requestHeaders
22
+ });
23
+ }`
24
+ : `
25
+ function fetcher<TData, TVariables extends { [key: string]: any }>(client: GraphQLClient, query: string, variables?: TVariables, requestHeaders?: RequestInit['headers']) {
26
+ return async (): Promise<TData> => client.request({
27
+ document: query,
28
+ variables,
29
+ requestHeaders
30
+ });
31
+ }`;
32
+ }
33
+ generateInfiniteQueryHook(config, isSuspense = false) {
34
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
35
+ if (this.clientPath)
36
+ this.visitor.imports.add(this.clientPath);
37
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
38
+ const { generateBaseInfiniteQueryHook, variables, options } = this.generateInfiniteQueryHelper(config, isSuspense);
39
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
40
+ return this.clientPath
41
+ ? generateBaseInfiniteQueryHook({
42
+ implArguments: `
43
+ pageParamKey: keyof ${operationVariablesTypes},
44
+ ${variables},
45
+ ${options},
46
+ headers?: RequestInit['headers']
47
+ `,
48
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, {...variables, [pageParamKey]: metaData.pageParam}, headers)()`,
49
+ })
50
+ : generateBaseInfiniteQueryHook({
51
+ implArguments: `
52
+ client: GraphQLClient,
53
+ ${variables},
54
+ ${options},
55
+ headers?: RequestInit['headers']
56
+ `,
57
+ implFetcher: `(metaData) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, {...variables, ...(metaData.pageParam ?? {})}, headers)()`,
58
+ });
59
+ }
60
+ generateQueryHook(config, isSuspense = false) {
61
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
62
+ if (this.clientPath)
63
+ this.visitor.imports.add(this.clientPath);
64
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
65
+ this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
66
+ const { generateBaseQueryHook, variables, options } = this.generateQueryHelper(config, isSuspense);
67
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
68
+ return this.clientPath
69
+ ? generateBaseQueryHook({
70
+ implArguments: `
71
+ ${variables},
72
+ ${options},
73
+ headers?: RequestInit['headers']
74
+ `,
75
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)`,
76
+ })
77
+ : generateBaseQueryHook({
78
+ implArguments: `
79
+ client: GraphQLClient,
80
+ ${variables},
81
+ ${options},
82
+ headers?: RequestInit['headers']
83
+ `,
84
+ implFetcher: `fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)`,
85
+ });
86
+ }
87
+ generateMutationHook(config) {
88
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
89
+ if (this.clientPath)
90
+ this.visitor.imports.add(this.clientPath);
91
+ this.visitor.imports.add(`${typeImport} { GraphQLClient } from 'graphql-request';`);
92
+ const { generateBaseMutationHook, variables, options } = this.generateMutationHelper(config);
93
+ const { documentVariableName, operationResultType, operationVariablesTypes } = config;
94
+ return this.clientPath
95
+ ? generateBaseMutationHook({
96
+ implArguments: `
97
+ ${options},
98
+ headers?: RequestInit['headers']
99
+ `,
100
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers)()`,
101
+ })
102
+ : generateBaseMutationHook({
103
+ implArguments: `
104
+ client: GraphQLClient,
105
+ ${options},
106
+ headers?: RequestInit['headers']
107
+ `,
108
+ implFetcher: `(${variables}) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers)()`,
109
+ });
110
+ }
111
+ generateFetcherFetch(config) {
112
+ const { documentVariableName, operationResultType, operationVariablesTypes, operationName } = config;
113
+ const variables = this.generateQueryVariablesSignature(config);
114
+ const typeImport = this.visitor.config.useTypeImports ? 'import type' : 'import';
115
+ if (this.clientPath)
116
+ this.visitor.imports.add(this.clientPath);
117
+ this.visitor.imports.add(`${typeImport} { RequestInit } from 'graphql-request/dist/types.dom';`);
118
+ return this.clientPath
119
+ ? `\ncreate${operationName}.fetcher = (${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(${documentVariableName}, variables, headers);`
120
+ : `\ncreate${operationName}.fetcher = (client: GraphQLClient, ${variables}, headers?: RequestInit['headers']) => fetcher<${operationResultType}, ${operationVariablesTypes}>(client, ${documentVariableName}, variables, headers);`;
121
+ }
122
+ }
123
+ exports.GraphQLRequestClientFetcher = GraphQLRequestClientFetcher;
package/cjs/fetcher.js ADDED
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FetcherRenderer = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
6
+ class FetcherRenderer {
7
+ constructor(visitor) {
8
+ this.visitor = visitor;
9
+ (0, auto_bind_1.default)(this);
10
+ }
11
+ createQueryMethodMap(isSuspense = false) {
12
+ const suspenseText = isSuspense ? 'Suspense' : '';
13
+ const queryMethodMap = {
14
+ infiniteQuery: {
15
+ getHook: (operationName = 'Query') => `create${suspenseText}Infinite${operationName}`,
16
+ getOptions: () => `Create${suspenseText}InfiniteQueryOptions`,
17
+ getOtherTypes: () => ({ infiniteData: 'InfiniteData' }),
18
+ },
19
+ query: {
20
+ getHook: (operationName = 'Query') => `create${suspenseText}${operationName}`,
21
+ getOptions: () => `Create${suspenseText}QueryOptions`,
22
+ },
23
+ mutation: {
24
+ getHook: (operationName = 'Mutation') => `create${operationName}`,
25
+ getOptions: () => `CreateMutationOptions`,
26
+ },
27
+ };
28
+ return queryMethodMap;
29
+ }
30
+ generateInfiniteQueryHelper(config, isSuspense) {
31
+ const { operationResultType, operationName } = config;
32
+ const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
33
+ this.visitor.solidQueryHookIdentifiersInUse.add(infiniteQuery.getHook());
34
+ this.visitor.solidQueryOptionsIdentifiersInUse.add(infiniteQuery.getOptions());
35
+ this.visitor.solidQueryOptionsIdentifiersInUse.add(infiniteQuery.getOtherTypes().infiniteData);
36
+ const variables = this.generateInfiniteQueryVariablesSignature(config);
37
+ const options = this.generateInfiniteQueryOptionsSignature(config, isSuspense);
38
+ const generateBaseInfiniteQueryHook = (hookConfig) => {
39
+ const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
40
+ const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
41
+ ${variables},
42
+ ${options}
43
+ `;
44
+ return `export const ${infiniteQuery.getHook(operationName)} = <
45
+ TData = ${`${infiniteQuery.getOtherTypes().infiniteData}<${operationResultType}>`},
46
+ TError = ${this.visitor.config.errorType}
47
+ >(${argumentsResult}) => {
48
+ ${implHookOuter}
49
+ return ${infiniteQuery.getHook()}<${operationResultType}, TError, TData>(
50
+ ${this.generateInfiniteQueryFormattedParameters(this.generateInfiniteQueryKey(config, isSuspense), implFetcher)}
51
+ )};`;
52
+ };
53
+ return { generateBaseInfiniteQueryHook, variables, options };
54
+ }
55
+ generateQueryHelper(config, isSuspense) {
56
+ const { operationName, operationResultType } = config;
57
+ const { query } = this.createQueryMethodMap(isSuspense);
58
+ this.visitor.solidQueryHookIdentifiersInUse.add(query.getHook());
59
+ this.visitor.solidQueryOptionsIdentifiersInUse.add(query.getOptions());
60
+ const variables = this.generateQueryVariablesSignature(config);
61
+ const options = this.generateQueryOptionsSignature(config, isSuspense);
62
+ const generateBaseQueryHook = (hookConfig) => {
63
+ const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
64
+ const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `
65
+ ${variables},
66
+ ${options}
67
+ `;
68
+ return `export const ${query.getHook(operationName)} = <
69
+ TData = ${operationResultType},
70
+ TError = ${this.visitor.config.errorType}
71
+ >(${argumentsResult}) => {
72
+ ${implHookOuter}
73
+ return ${query.getHook()}<${operationResultType}, TError, TData>(
74
+ ${this.generateQueryFormattedParameters(this.generateQueryKey(config, isSuspense), implFetcher)}
75
+ )};`;
76
+ };
77
+ return {
78
+ generateBaseQueryHook,
79
+ variables,
80
+ options,
81
+ };
82
+ }
83
+ generateMutationHelper(config) {
84
+ const { operationResultType, operationVariablesTypes, operationName } = config;
85
+ const { mutation } = this.createQueryMethodMap();
86
+ this.visitor.solidQueryHookIdentifiersInUse.add(mutation.getHook());
87
+ this.visitor.solidQueryOptionsIdentifiersInUse.add(mutation.getOptions());
88
+ const variables = `variables?: ${operationVariablesTypes}`;
89
+ const options = `options?: ${mutation.getOptions()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>`;
90
+ const generateBaseMutationHook = (hookConfig) => {
91
+ const { implArguments, implHookOuter = '', implFetcher } = hookConfig;
92
+ const argumentsResult = implArguments !== null && implArguments !== void 0 ? implArguments : `${options}`;
93
+ return `export const ${mutation.getHook(operationName)} = <
94
+ TError = ${this.visitor.config.errorType},
95
+ TContext = unknown
96
+ >(${argumentsResult}) => {
97
+ ${implHookOuter}
98
+ return ${mutation.getHook()}<${operationResultType}, TError, ${operationVariablesTypes}, TContext>(
99
+ ${this.generateMutationFormattedParameters(this.generateMutationKey(config), implFetcher)}
100
+ )};`;
101
+ };
102
+ return {
103
+ generateBaseMutationHook,
104
+ variables,
105
+ options,
106
+ };
107
+ }
108
+ generateQueryVariablesSignature({ hasRequiredVariables, operationVariablesTypes, }) {
109
+ return `variables${hasRequiredVariables ? '' : '?'}: ${operationVariablesTypes}`;
110
+ }
111
+ generateQueryOptionsSignature({ operationResultType }, isSuspense) {
112
+ const { query } = this.createQueryMethodMap(isSuspense);
113
+ return `options?: Omit<${query.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${query.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
114
+ }
115
+ generateInfiniteQueryVariablesSignature(config) {
116
+ return `variables: ${config.operationVariablesTypes}`;
117
+ }
118
+ generateInfiniteQueryOptionsSignature({ operationResultType }, isSuspense) {
119
+ const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
120
+ return `options: Omit<${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>, 'queryKey'> & { queryKey?: ${infiniteQuery.getOptions()}<${operationResultType}, TError, TData>['queryKey'] }`;
121
+ }
122
+ generateInfiniteQueryKey(config, isSuspense) {
123
+ const identifier = isSuspense ? 'infiniteSuspense' : 'infinite';
124
+ if (config.hasRequiredVariables)
125
+ return `['${config.node.name.value}.${identifier}', variables]`;
126
+ return `variables === undefined ? ['${config.node.name.value}.${identifier}'] : ['${config.node.name.value}.${identifier}', variables]`;
127
+ }
128
+ generateInfiniteQueryOutput(config, isSuspense = false) {
129
+ const { infiniteQuery } = this.createQueryMethodMap(isSuspense);
130
+ const signature = this.generateQueryVariablesSignature(config);
131
+ const { operationName, node } = config;
132
+ return {
133
+ hook: this.generateInfiniteQueryHook(config, isSuspense),
134
+ getKey: `${infiniteQuery.getHook(operationName)}.getKey = (${signature}) => ${this.generateInfiniteQueryKey(config, isSuspense)};`,
135
+ rootKey: `${infiniteQuery.getHook(operationName)}.rootKey = '${node.name.value}.infinite';`,
136
+ };
137
+ }
138
+ generateQueryKey(config, isSuspense) {
139
+ const identifier = isSuspense ? `${config.node.name.value}Suspense` : config.node.name.value;
140
+ if (config.hasRequiredVariables)
141
+ return `['${identifier}', variables]`;
142
+ return `variables === undefined ? ['${identifier}'] : ['${identifier}', variables]`;
143
+ }
144
+ generateQueryOutput(config, isSuspense = false) {
145
+ const { query } = this.createQueryMethodMap(isSuspense);
146
+ const signature = this.generateQueryVariablesSignature(config);
147
+ const { operationName, node, documentVariableName } = config;
148
+ return {
149
+ hook: this.generateQueryHook(config, isSuspense),
150
+ document: `${query.getHook(operationName)}.document = ${documentVariableName};`,
151
+ getKey: `${query.getHook(operationName)}.getKey = (${signature}) => ${this.generateQueryKey(config, isSuspense)};`,
152
+ rootKey: `${query.getHook(operationName)}.rootKey = '${node.name.value}';`,
153
+ };
154
+ }
155
+ generateMutationKey({ node }) {
156
+ return `['${node.name.value}']`;
157
+ }
158
+ generateMutationOutput(config) {
159
+ const { mutation } = this.createQueryMethodMap();
160
+ const { operationName } = config;
161
+ return {
162
+ hook: this.generateMutationHook(config),
163
+ getKey: `${mutation.getHook(operationName)}.getKey = () => ${this.generateMutationKey(config)};`,
164
+ };
165
+ }
166
+ generateInfiniteQueryFormattedParameters(queryKey, queryFn) {
167
+ return `(() => {
168
+ const { queryKey: optionsQueryKey, ...restOptions } = options;
169
+ return () => ({
170
+ queryKey: optionsQueryKey ?? ${queryKey},
171
+ queryFn: ${queryFn},
172
+ ...restOptions
173
+ }
174
+ }))()`;
175
+ }
176
+ generateQueryFormattedParameters(queryKey, queryFn) {
177
+ return `() => ({
178
+ queryKey: ${queryKey},
179
+ queryFn: ${queryFn},
180
+ ...options
181
+ })`;
182
+ }
183
+ generateMutationFormattedParameters(mutationKey, mutationFn) {
184
+ return `() => ({
185
+ mutationKey: ${mutationKey},
186
+ mutationFn: ${mutationFn},
187
+ ...options
188
+ })`;
189
+ }
190
+ }
191
+ exports.FetcherRenderer = FetcherRenderer;
package/cjs/index.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolidQueryVisitor = exports.validate = exports.plugin = void 0;
4
+ const path_1 = require("path");
5
+ const graphql_1 = require("graphql");
6
+ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
7
+ const visitor_js_1 = require("./visitor.js");
8
+ Object.defineProperty(exports, "SolidQueryVisitor", { enumerable: true, get: function () { return visitor_js_1.SolidQueryVisitor; } });
9
+ const plugin = (schema, documents, config) => {
10
+ const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document));
11
+ const allFragments = [
12
+ ...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
13
+ node: fragmentDef,
14
+ name: fragmentDef.name.value,
15
+ onType: fragmentDef.typeCondition.name.value,
16
+ isExternal: false,
17
+ })),
18
+ ...(config.externalFragments || []),
19
+ ];
20
+ const visitor = new visitor_js_1.SolidQueryVisitor(schema, allFragments, config, documents);
21
+ const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
22
+ if (visitor.hasOperations) {
23
+ return {
24
+ prepend: [...visitor.getImports(), visitor.getFetcherImplementation()],
25
+ content: [
26
+ '',
27
+ visitor.fragments,
28
+ ...visitorResult.definitions.filter(t => typeof t === 'string'),
29
+ ].join('\n'),
30
+ };
31
+ }
32
+ return {
33
+ prepend: [...visitor.getImports()],
34
+ content: [
35
+ '',
36
+ visitor.fragments,
37
+ ...visitorResult.definitions.filter(t => typeof t === 'string'),
38
+ ].join('\n'),
39
+ };
40
+ };
41
+ exports.plugin = plugin;
42
+ const validate = async (schema, documents, config, outputFile) => {
43
+ if ((0, path_1.extname)(outputFile) !== '.ts' && (0, path_1.extname)(outputFile) !== '.tsx') {
44
+ throw new Error(`Plugin "typescript-solid-query" requires extension to be ".ts" or ".tsx"!`);
45
+ }
46
+ };
47
+ exports.validate = validate;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}