@graphql-codegen/typescript-react-apollo 4.1.1-alpha-20231126041150-01a328d1e → 4.2.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/visitor.js CHANGED
@@ -21,6 +21,7 @@ class ReactApolloVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
21
21
  withHooks: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.withHooks, true),
22
22
  withMutationFn: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.withMutationFn, true),
23
23
  withRefetchFn: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.withRefetchFn, false),
24
+ withFragmentHooks: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.withFragmentHooks, false),
24
25
  apolloReactCommonImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.apolloReactCommonImportFrom, rawConfig.reactApolloVersion === 2
25
26
  ? '@apollo/react-common'
26
27
  : APOLLO_CLIENT_3_UNIFIED_PACKAGE),
@@ -105,9 +106,12 @@ class ReactApolloVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
105
106
  getImports() {
106
107
  const baseImports = super.getImports();
107
108
  const hasOperations = this._collectedOperations.length > 0;
108
- if (!hasOperations) {
109
+ if (!hasOperations && !this.config.withFragmentHooks) {
109
110
  return baseImports;
110
111
  }
112
+ if (this.config.withFragmentHooks) {
113
+ return [...baseImports, this.getApolloReactHooksImport(false), ...Array.from(this.imports)];
114
+ }
111
115
  return [...baseImports, ...Array.from(this.imports)];
112
116
  }
113
117
  _buildHocProps(operationName, operationType) {
@@ -352,5 +356,46 @@ class ReactApolloVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
352
356
  .filter(a => a)
353
357
  .join('\n');
354
358
  }
359
+ get fragments() {
360
+ var _a, _b;
361
+ const fragments = super.fragments;
362
+ if (this._fragments.length === 0 || !this.config.withFragmentHooks) {
363
+ return fragments;
364
+ }
365
+ const operationType = 'Fragment';
366
+ const hookFns = [fragments];
367
+ for (const fragment of this._fragments.values()) {
368
+ if (fragment.isExternal) {
369
+ continue;
370
+ }
371
+ const nodeName = (_a = fragment.name) !== null && _a !== void 0 ? _a : '';
372
+ const suffix = this._getHookSuffix(nodeName, operationType);
373
+ const fragmentName = this.convertName(nodeName, {
374
+ suffix,
375
+ useTypesPrefix: false,
376
+ useTypesSuffix: false,
377
+ }) + this.config.hooksSuffix;
378
+ const operationTypeSuffix = this.getOperationSuffix(fragmentName, operationType);
379
+ const operationResultType = this.convertName(nodeName, {
380
+ suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,
381
+ });
382
+ const IDType = (_b = this.scalars.ID) !== null && _b !== void 0 ? _b : 'string';
383
+ const hook = `export function use${fragmentName}<F = { id: ${IDType} }>(identifiers: F) {
384
+ return ${this.getApolloReactHooksIdentifier()}.use${operationType}<${operationResultType}>({
385
+ fragment: ${nodeName}${this.config.fragmentVariableSuffix},
386
+ fragmentName: "${nodeName}",
387
+ from: {
388
+ __typename: "${fragment.onType}",
389
+ ...identifiers,
390
+ },
391
+ });
392
+ }`;
393
+ const hookResults = [
394
+ `export type ${fragmentName}HookResult = ReturnType<typeof use${fragmentName}>;`,
395
+ ];
396
+ hookFns.push([hook, hookResults].join('\n'));
397
+ }
398
+ return hookFns.join('\n');
399
+ }
355
400
  }
356
401
  exports.ReactApolloVisitor = ReactApolloVisitor;
package/esm/visitor.js CHANGED
@@ -17,6 +17,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor {
17
17
  withHooks: getConfigValue(rawConfig.withHooks, true),
18
18
  withMutationFn: getConfigValue(rawConfig.withMutationFn, true),
19
19
  withRefetchFn: getConfigValue(rawConfig.withRefetchFn, false),
20
+ withFragmentHooks: getConfigValue(rawConfig.withFragmentHooks, false),
20
21
  apolloReactCommonImportFrom: getConfigValue(rawConfig.apolloReactCommonImportFrom, rawConfig.reactApolloVersion === 2
21
22
  ? '@apollo/react-common'
22
23
  : APOLLO_CLIENT_3_UNIFIED_PACKAGE),
@@ -101,9 +102,12 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor {
101
102
  getImports() {
102
103
  const baseImports = super.getImports();
103
104
  const hasOperations = this._collectedOperations.length > 0;
104
- if (!hasOperations) {
105
+ if (!hasOperations && !this.config.withFragmentHooks) {
105
106
  return baseImports;
106
107
  }
108
+ if (this.config.withFragmentHooks) {
109
+ return [...baseImports, this.getApolloReactHooksImport(false), ...Array.from(this.imports)];
110
+ }
107
111
  return [...baseImports, ...Array.from(this.imports)];
108
112
  }
109
113
  _buildHocProps(operationName, operationType) {
@@ -348,4 +352,45 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor {
348
352
  .filter(a => a)
349
353
  .join('\n');
350
354
  }
355
+ get fragments() {
356
+ var _a, _b;
357
+ const fragments = super.fragments;
358
+ if (this._fragments.length === 0 || !this.config.withFragmentHooks) {
359
+ return fragments;
360
+ }
361
+ const operationType = 'Fragment';
362
+ const hookFns = [fragments];
363
+ for (const fragment of this._fragments.values()) {
364
+ if (fragment.isExternal) {
365
+ continue;
366
+ }
367
+ const nodeName = (_a = fragment.name) !== null && _a !== void 0 ? _a : '';
368
+ const suffix = this._getHookSuffix(nodeName, operationType);
369
+ const fragmentName = this.convertName(nodeName, {
370
+ suffix,
371
+ useTypesPrefix: false,
372
+ useTypesSuffix: false,
373
+ }) + this.config.hooksSuffix;
374
+ const operationTypeSuffix = this.getOperationSuffix(fragmentName, operationType);
375
+ const operationResultType = this.convertName(nodeName, {
376
+ suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,
377
+ });
378
+ const IDType = (_b = this.scalars.ID) !== null && _b !== void 0 ? _b : 'string';
379
+ const hook = `export function use${fragmentName}<F = { id: ${IDType} }>(identifiers: F) {
380
+ return ${this.getApolloReactHooksIdentifier()}.use${operationType}<${operationResultType}>({
381
+ fragment: ${nodeName}${this.config.fragmentVariableSuffix},
382
+ fragmentName: "${nodeName}",
383
+ from: {
384
+ __typename: "${fragment.onType}",
385
+ ...identifiers,
386
+ },
387
+ });
388
+ }`;
389
+ const hookResults = [
390
+ `export type ${fragmentName}HookResult = ReturnType<typeof use${fragmentName}>;`,
391
+ ];
392
+ hookFns.push([hook, hookResults].join('\n'));
393
+ }
394
+ return hookFns.join('\n');
395
+ }
351
396
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-react-apollo",
3
- "version": "4.1.1-alpha-20231126041150-01a328d1e",
3
+ "version": "4.2.0",
4
4
  "description": "GraphQL Code Generator plugin for generating a ready-to-use React Components/HOC/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",
@@ -10,7 +10,7 @@
10
10
  "@graphql-codegen/plugin-helpers": "^3.0.0",
11
11
  "@graphql-codegen/visitor-plugin-common": "2.13.1",
12
12
  "auto-bind": "~4.0.0",
13
- "change-case-all": "2.0.1",
13
+ "change-case-all": "1.0.15",
14
14
  "tslib": "~2.6.0"
15
15
  },
16
16
  "repository": {
@@ -216,6 +216,29 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi
216
216
  * ```
217
217
  */
218
218
  withMutationOptionsType?: boolean;
219
+ /**
220
+ * @description Whether or not to include wrappers for Apollo's useFragment hook.
221
+ * @default false
222
+ *
223
+ * @exampleMarkdown
224
+ * ```ts filename="codegen.ts"
225
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
226
+ *
227
+ * const config: CodegenConfig = {
228
+ * // ...
229
+ * generates: {
230
+ * 'path/to/file.ts': {
231
+ * plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
232
+ * config: {
233
+ * withFragmentHooks: true
234
+ * },
235
+ * },
236
+ * },
237
+ * };
238
+ * export default config;
239
+ * ```
240
+ */
241
+ withFragmentHooks?: boolean;
219
242
  /**
220
243
  * @description Allows you to enable/disable the generation of docblocks in generated code.
221
244
  * Some IDE's (like VSCode) add extra inline information with docblocks, you can disable this feature if your preferred IDE does not.
@@ -240,8 +263,19 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi
240
263
  * ```
241
264
  */
242
265
  addDocBlocks?: boolean;
243
- defaultBaseOptions?: {
244
- [key: string]: string;
245
- };
266
+ /**
267
+ * @description Configure default mutation and query hook options.
268
+ */
269
+ defaultBaseOptions?: ReactApolloPluginConfigDefaultBaseOptions;
246
270
  hooksSuffix?: string;
247
271
  }
272
+ export interface ReactApolloPluginConfigDefaultBaseOptions {
273
+ awaitRefetchQueries?: boolean;
274
+ errorPolicy?: string;
275
+ fetchPolicy?: string;
276
+ ignoreResults?: boolean;
277
+ notifyOnNetworkStatusChange?: boolean;
278
+ returnPartialData?: boolean;
279
+ ssr?: boolean;
280
+ [key: string]: any;
281
+ }
@@ -216,6 +216,29 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi
216
216
  * ```
217
217
  */
218
218
  withMutationOptionsType?: boolean;
219
+ /**
220
+ * @description Whether or not to include wrappers for Apollo's useFragment hook.
221
+ * @default false
222
+ *
223
+ * @exampleMarkdown
224
+ * ```ts filename="codegen.ts"
225
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
226
+ *
227
+ * const config: CodegenConfig = {
228
+ * // ...
229
+ * generates: {
230
+ * 'path/to/file.ts': {
231
+ * plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
232
+ * config: {
233
+ * withFragmentHooks: true
234
+ * },
235
+ * },
236
+ * },
237
+ * };
238
+ * export default config;
239
+ * ```
240
+ */
241
+ withFragmentHooks?: boolean;
219
242
  /**
220
243
  * @description Allows you to enable/disable the generation of docblocks in generated code.
221
244
  * Some IDE's (like VSCode) add extra inline information with docblocks, you can disable this feature if your preferred IDE does not.
@@ -240,8 +263,19 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi
240
263
  * ```
241
264
  */
242
265
  addDocBlocks?: boolean;
243
- defaultBaseOptions?: {
244
- [key: string]: string;
245
- };
266
+ /**
267
+ * @description Configure default mutation and query hook options.
268
+ */
269
+ defaultBaseOptions?: ReactApolloPluginConfigDefaultBaseOptions;
246
270
  hooksSuffix?: string;
247
271
  }
272
+ export interface ReactApolloPluginConfigDefaultBaseOptions {
273
+ awaitRefetchQueries?: boolean;
274
+ errorPolicy?: string;
275
+ fetchPolicy?: string;
276
+ ignoreResults?: boolean;
277
+ notifyOnNetworkStatusChange?: boolean;
278
+ returnPartialData?: boolean;
279
+ ssr?: boolean;
280
+ [key: string]: any;
281
+ }
@@ -1,13 +1,14 @@
1
1
  import { GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
2
  import { Types } from '@graphql-codegen/plugin-helpers';
3
3
  import { ClientSideBasePluginConfig, ClientSideBaseVisitor, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
4
- import { ReactApolloRawPluginConfig } from './config.cjs';
4
+ import { ReactApolloPluginConfigDefaultBaseOptions, ReactApolloRawPluginConfig } from './config.cjs';
5
5
  export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
6
6
  withComponent: boolean;
7
7
  withHOC: boolean;
8
8
  withHooks: boolean;
9
9
  withMutationFn: boolean;
10
10
  withRefetchFn: boolean;
11
+ withFragmentHooks?: boolean;
11
12
  apolloReactCommonImportFrom: string;
12
13
  apolloReactComponentsImportFrom: string;
13
14
  apolloReactHocImportFrom: string;
@@ -17,9 +18,7 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
17
18
  withResultType: boolean;
18
19
  withMutationOptionsType: boolean;
19
20
  addDocBlocks: boolean;
20
- defaultBaseOptions: {
21
- [key: string]: string;
22
- };
21
+ defaultBaseOptions: ReactApolloPluginConfigDefaultBaseOptions;
23
22
  hooksSuffix: string;
24
23
  }
25
24
  export declare class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPluginConfig, ReactApolloPluginConfig> {
@@ -51,4 +50,5 @@ export declare class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApoll
51
50
  private _buildWithMutationOptionsType;
52
51
  private _buildRefetchFn;
53
52
  protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
53
+ get fragments(): string;
54
54
  }
@@ -1,13 +1,14 @@
1
1
  import { GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
2
  import { Types } from '@graphql-codegen/plugin-helpers';
3
3
  import { ClientSideBasePluginConfig, ClientSideBaseVisitor, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
4
- import { ReactApolloRawPluginConfig } from './config.js';
4
+ import { ReactApolloPluginConfigDefaultBaseOptions, ReactApolloRawPluginConfig } from './config.js';
5
5
  export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
6
6
  withComponent: boolean;
7
7
  withHOC: boolean;
8
8
  withHooks: boolean;
9
9
  withMutationFn: boolean;
10
10
  withRefetchFn: boolean;
11
+ withFragmentHooks?: boolean;
11
12
  apolloReactCommonImportFrom: string;
12
13
  apolloReactComponentsImportFrom: string;
13
14
  apolloReactHocImportFrom: string;
@@ -17,9 +18,7 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
17
18
  withResultType: boolean;
18
19
  withMutationOptionsType: boolean;
19
20
  addDocBlocks: boolean;
20
- defaultBaseOptions: {
21
- [key: string]: string;
22
- };
21
+ defaultBaseOptions: ReactApolloPluginConfigDefaultBaseOptions;
23
22
  hooksSuffix: string;
24
23
  }
25
24
  export declare class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPluginConfig, ReactApolloPluginConfig> {
@@ -51,4 +50,5 @@ export declare class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApoll
51
50
  private _buildWithMutationOptionsType;
52
51
  private _buildRefetchFn;
53
52
  protected buildOperation(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string, hasRequiredVariables: boolean): string;
53
+ get fragments(): string;
54
54
  }