@graphql-codegen/typescript-rtk-query 3.0.0-alpha-20230925162647-c81ce80bd → 3.0.0-alpha-20230925162913-bf8fa688c

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 CHANGED
@@ -11,6 +11,7 @@ class RTKQueryVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
11
11
  documentMode: visitor_plugin_common_1.DocumentMode.string,
12
12
  importBaseApiFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.importBaseApiFrom, ''),
13
13
  importBaseApiAlternateName: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.importBaseApiAlternateName, 'api'),
14
+ addTransformResponse: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addTransformResponse, false),
14
15
  exportHooks: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.exportHooks, false),
15
16
  overrideExisting: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.overrideExisting, ''),
16
17
  });
@@ -59,6 +60,13 @@ export { injectedRtkApi as api };
59
60
  : '') +
60
61
  '\n\n');
61
62
  }
63
+ injectTransformResponse(Generics) {
64
+ if (this.config.addTransformResponse) {
65
+ const responseType = Generics.split(',')[0];
66
+ return `transformResponse: (response: ${responseType}) => response`;
67
+ }
68
+ return '';
69
+ }
62
70
  buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) {
63
71
  var _a, _b;
64
72
  operationResultType = this._externalImportPrefix + operationResultType;
@@ -66,30 +74,29 @@ export { injectedRtkApi as api };
66
74
  const operationName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value;
67
75
  if (!operationName)
68
76
  return '';
77
+ if (operationType === 'Subscription') {
78
+ // eslint-disable-next-line no-console
79
+ console.warn(`Plugin "typescript-rtk-query" does not support GraphQL Subscriptions at the moment! Skipping "${(_b = node.name) === null || _b === void 0 ? void 0 : _b.value}"...`);
80
+ return '';
81
+ }
69
82
  const Generics = `${operationResultType}, ${operationVariablesTypes}${hasRequiredVariables ? '' : ' | void'}`;
70
- if (operationType === 'Query') {
71
- this._endpoints.push(`
72
- ${operationName}: build.query<${Generics}>({
73
- query: (variables) => ({ document: ${documentVariableName}, variables })
74
- }),`);
75
- if (this.config.exportHooks) {
83
+ const operationTypeString = operationType.toLowerCase();
84
+ const functionsString = `query: (variables) => ({ document: ${documentVariableName}, variables })
85
+ ${this.injectTransformResponse(Generics)}`.trim();
86
+ const endpointString = `
87
+ ${operationName}: build.${operationTypeString}<${Generics}>({
88
+ ${functionsString}
89
+ }),`;
90
+ this._endpoints.push(endpointString);
91
+ if (this.config.exportHooks) {
92
+ if (operationType === 'Query') {
76
93
  this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Query`);
77
94
  this._hooks.push(`useLazy${(0, change_case_all_1.pascalCase)(operationName)}Query`);
78
95
  }
79
- }
80
- else if (operationType === 'Mutation') {
81
- this._endpoints.push(`
82
- ${operationName}: build.mutation<${Generics}>({
83
- query: (variables) => ({ document: ${documentVariableName}, variables })
84
- }),`);
85
- if (this.config.exportHooks) {
96
+ if (operationType === 'Mutation') {
86
97
  this._hooks.push(`use${(0, change_case_all_1.pascalCase)(operationName)}Mutation`);
87
98
  }
88
99
  }
89
- else if (operationType === 'Subscription') {
90
- // eslint-disable-next-line no-console
91
- console.warn(`Plugin "typescript-rtk-query" does not support GraphQL Subscriptions at the moment! Skipping "${(_b = node.name) === null || _b === void 0 ? void 0 : _b.value}"...`);
92
- }
93
100
  return '';
94
101
  }
95
102
  }
package/esm/visitor.js CHANGED
@@ -7,6 +7,7 @@ export class RTKQueryVisitor extends ClientSideBaseVisitor {
7
7
  documentMode: DocumentMode.string,
8
8
  importBaseApiFrom: getConfigValue(rawConfig.importBaseApiFrom, ''),
9
9
  importBaseApiAlternateName: getConfigValue(rawConfig.importBaseApiAlternateName, 'api'),
10
+ addTransformResponse: getConfigValue(rawConfig.addTransformResponse, false),
10
11
  exportHooks: getConfigValue(rawConfig.exportHooks, false),
11
12
  overrideExisting: getConfigValue(rawConfig.overrideExisting, ''),
12
13
  });
@@ -55,6 +56,13 @@ export { injectedRtkApi as api };
55
56
  : '') +
56
57
  '\n\n');
57
58
  }
59
+ injectTransformResponse(Generics) {
60
+ if (this.config.addTransformResponse) {
61
+ const responseType = Generics.split(',')[0];
62
+ return `transformResponse: (response: ${responseType}) => response`;
63
+ }
64
+ return '';
65
+ }
58
66
  buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes, hasRequiredVariables) {
59
67
  var _a, _b;
60
68
  operationResultType = this._externalImportPrefix + operationResultType;
@@ -62,30 +70,29 @@ export { injectedRtkApi as api };
62
70
  const operationName = (_a = node.name) === null || _a === void 0 ? void 0 : _a.value;
63
71
  if (!operationName)
64
72
  return '';
73
+ if (operationType === 'Subscription') {
74
+ // eslint-disable-next-line no-console
75
+ console.warn(`Plugin "typescript-rtk-query" does not support GraphQL Subscriptions at the moment! Skipping "${(_b = node.name) === null || _b === void 0 ? void 0 : _b.value}"...`);
76
+ return '';
77
+ }
65
78
  const Generics = `${operationResultType}, ${operationVariablesTypes}${hasRequiredVariables ? '' : ' | void'}`;
66
- if (operationType === 'Query') {
67
- this._endpoints.push(`
68
- ${operationName}: build.query<${Generics}>({
69
- query: (variables) => ({ document: ${documentVariableName}, variables })
70
- }),`);
71
- if (this.config.exportHooks) {
79
+ const operationTypeString = operationType.toLowerCase();
80
+ const functionsString = `query: (variables) => ({ document: ${documentVariableName}, variables })
81
+ ${this.injectTransformResponse(Generics)}`.trim();
82
+ const endpointString = `
83
+ ${operationName}: build.${operationTypeString}<${Generics}>({
84
+ ${functionsString}
85
+ }),`;
86
+ this._endpoints.push(endpointString);
87
+ if (this.config.exportHooks) {
88
+ if (operationType === 'Query') {
72
89
  this._hooks.push(`use${pascalCase(operationName)}Query`);
73
90
  this._hooks.push(`useLazy${pascalCase(operationName)}Query`);
74
91
  }
75
- }
76
- else if (operationType === 'Mutation') {
77
- this._endpoints.push(`
78
- ${operationName}: build.mutation<${Generics}>({
79
- query: (variables) => ({ document: ${documentVariableName}, variables })
80
- }),`);
81
- if (this.config.exportHooks) {
92
+ if (operationType === 'Mutation') {
82
93
  this._hooks.push(`use${pascalCase(operationName)}Mutation`);
83
94
  }
84
95
  }
85
- else if (operationType === 'Subscription') {
86
- // eslint-disable-next-line no-console
87
- console.warn(`Plugin "typescript-rtk-query" does not support GraphQL Subscriptions at the moment! Skipping "${(_b = node.name) === null || _b === void 0 ? void 0 : _b.value}"...`);
88
- }
89
96
  return '';
90
97
  }
91
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-rtk-query",
3
- "version": "3.0.0-alpha-20230925162647-c81ce80bd",
3
+ "version": "3.0.0-alpha-20230925162913-bf8fa688c",
4
4
  "description": "GraphQL Code Generator plugin for injecting graphql endpoints into a preconfigured RTK Query api",
5
5
  "peerDependenciesMeta": {
6
6
  "graphql-request": {
@@ -102,6 +102,32 @@ export interface RTKConfig {
102
102
  * ```
103
103
  */
104
104
  overrideExisting?: string;
105
+ /**
106
+ * @name addTransformResponse
107
+ * @description Sets the `addTransformResponse` option, which will automatically add a types transformResponse for query
108
+ * @default false
109
+ *
110
+
111
+ * @exampleMarkdown
112
+ * ```ts filename="codegen.ts"
113
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
114
+ *
115
+ * const config: CodegenConfig = {
116
+ * // ...
117
+ * generates: {
118
+ * 'path/to/file.ts': {
119
+ * plugins: ['typescript', 'typescript-resolvers', 'typescript-rtk-query'],
120
+ * config: {
121
+ * importBaseApiFrom: 'src/app/api/baseApi',
122
+ * addTransformResponse: true,
123
+ * },
124
+ * },
125
+ * },
126
+ * };
127
+ * export default config;
128
+ * ```
129
+ */
130
+ addTransformResponse?: boolean;
105
131
  }
106
132
  export interface RTKQueryRawPluginConfig extends RawClientSideBasePluginConfig, RTKConfig {
107
133
  }
@@ -102,6 +102,32 @@ export interface RTKConfig {
102
102
  * ```
103
103
  */
104
104
  overrideExisting?: string;
105
+ /**
106
+ * @name addTransformResponse
107
+ * @description Sets the `addTransformResponse` option, which will automatically add a types transformResponse for query
108
+ * @default false
109
+ *
110
+
111
+ * @exampleMarkdown
112
+ * ```ts filename="codegen.ts"
113
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
114
+ *
115
+ * const config: CodegenConfig = {
116
+ * // ...
117
+ * generates: {
118
+ * 'path/to/file.ts': {
119
+ * plugins: ['typescript', 'typescript-resolvers', 'typescript-rtk-query'],
120
+ * config: {
121
+ * importBaseApiFrom: 'src/app/api/baseApi',
122
+ * addTransformResponse: true,
123
+ * },
124
+ * },
125
+ * },
126
+ * };
127
+ * export default config;
128
+ * ```
129
+ */
130
+ addTransformResponse?: boolean;
105
131
  }
106
132
  export interface RTKQueryRawPluginConfig extends RawClientSideBasePluginConfig, RTKConfig {
107
133
  }
@@ -12,5 +12,6 @@ export declare class RTKQueryVisitor extends ClientSideBaseVisitor<RTKQueryRawPl
12
12
  get hasOperations(): boolean;
13
13
  getImports(): string[];
14
14
  getInjectCall(): string;
15
- protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
15
+ private injectTransformResponse;
16
+ protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: 'Query' | 'Mutation' | 'Subscription', operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
16
17
  }
@@ -12,5 +12,6 @@ export declare class RTKQueryVisitor extends ClientSideBaseVisitor<RTKQueryRawPl
12
12
  get hasOperations(): boolean;
13
13
  getImports(): string[];
14
14
  getInjectCall(): string;
15
- protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
15
+ private injectTransformResponse;
16
+ protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: 'Query' | 'Mutation' | 'Subscription', operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
16
17
  }