@graphql-codegen/client-preset 3.0.0 → 3.0.1-alpha-20230404095953-c13d5890d

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.
@@ -20,15 +20,19 @@ export function makeFragmentData<
20
20
  return data as FragmentType<F>;
21
21
  }`;
22
22
  const defaultUnmaskFunctionName = 'useFragment';
23
- const modifyType = (rawType, opts) => `${opts.list === 'only-list'
24
- ? `ReadonlyArray<${rawType}>`
25
- : opts.list === 'with-list'
26
- ? `${rawType} | ReadonlyArray<${rawType}>`
27
- : rawType}${opts.nullable ? ' | null | undefined' : ''}`;
28
- const createUnmaskFunctionTypeDefinition = (unmaskFunctionName = defaultUnmaskFunctionName, opts) => `export function ${unmaskFunctionName}<TType>(
23
+ const modifyType = (rawType, opts) => {
24
+ return `${opts.list === 'only-list'
25
+ ? `ReadonlyArray<${rawType}>`
26
+ : opts.list === 'with-list'
27
+ ? `${rawType} | ReadonlyArray<${rawType}>`
28
+ : rawType}${opts.nullable ? ' | null | undefined' : ''}`;
29
+ };
30
+ const createUnmaskFunctionTypeDefinition = (unmaskFunctionName = defaultUnmaskFunctionName, opts) => {
31
+ return `export function ${unmaskFunctionName}<TType>(
29
32
  _documentNode: DocumentTypeDecoration<TType, any>,
30
- fragmentType: ${modifyType('FragmentType<DocumentTypeDecoration<TType, any>>', opts)}
33
+ fragmentType: ${modifyType(`FragmentType<DocumentTypeDecoration<TType, any>>`, opts)}
31
34
  ): ${modifyType('TType', opts)}`;
35
+ };
32
36
  const createUnmaskFunctionTypeDefinitions = (unmaskFunctionName = defaultUnmaskFunctionName) => [
33
37
  `// return non-nullable if \`fragmentType\` is non-nullable\n${createUnmaskFunctionTypeDefinition(unmaskFunctionName, { nullable: false, list: false })}`,
34
38
  `// return nullable if \`fragmentType\` is nullable\n${createUnmaskFunctionTypeDefinition(unmaskFunctionName, {
@@ -45,20 +49,64 @@ ${createUnmaskFunctionTypeDefinitions(unmaskFunctionName)
45
49
  return fragmentType as any;
46
50
  }
47
51
  `;
52
+ const isFragmentReadyFunction = (isStringDocumentMode) => {
53
+ if (isStringDocumentMode) {
54
+ return `\
55
+ export function isFragmentReady<TQuery, TFrag>(
56
+ queryNode: TypedDocumentString<TQuery, any>,
57
+ fragmentNode: TypedDocumentString<TFrag, any>,
58
+ fragment: Partial<TFrag>
59
+ ): fragment is FragmentType<typeof fragmentNode> {
60
+ const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
61
+
62
+ if (!deferredFields) return true;
63
+
64
+ const fragName = fragmentNode.__meta__?.fragmentName;
65
+
66
+ const fields = fragName ? deferredFields[fragName] : [];
67
+ return fields.length > 0 && fields.some(field => fragment && field in fragment);
68
+ }
69
+ `;
70
+ }
71
+ return `\
72
+ export function isFragmentReady<TQuery, TFrag>(
73
+ queryNode: DocumentTypeDecoration<TQuery, any>,
74
+ fragmentNode: TypedDocumentNode<TFrag>,
75
+ fragment: Partial<TFrag>
76
+ ): fragment is FragmentType<typeof fragmentNode> {
77
+ const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
78
+ ?.deferredFields;
79
+
80
+ if (!deferredFields) return true;
81
+
82
+ const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
83
+ const fragName = fragDef?.name?.value;
84
+
85
+ const fields = fragName ? deferredFields[fragName] : [];
86
+ return fields.length > 0 && fields.some(field => fragment && field in fragment);
87
+ }
88
+ `;
89
+ };
48
90
  /**
49
91
  * Plugin for generating fragment masking helper functions.
50
92
  */
51
- const plugin = (_, __, { useTypeImports, augmentedModuleName, unmaskFunctionName }, _info) => {
52
- const documentNodeImport = `${useTypeImports ? 'import type' : 'import'} { ResultOf, DocumentTypeDecoration, } from '@graphql-typed-document-node/core';\n`;
93
+ const plugin = (_, __, { useTypeImports, augmentedModuleName, unmaskFunctionName, emitLegacyCommonJSImports, isStringDocumentMode }, _info) => {
94
+ const documentNodeImport = `${useTypeImports ? 'import type' : 'import'} { ResultOf, DocumentTypeDecoration${isStringDocumentMode ? '' : ', TypedDocumentNode'} } from '@graphql-typed-document-node/core';\n`;
95
+ const deferFragmentHelperImports = isStringDocumentMode
96
+ ? `${useTypeImports ? 'import type' : 'import'} { TypedDocumentString } from './graphql${emitLegacyCommonJSImports ? '' : '.js'}';\n`
97
+ : `${useTypeImports ? 'import type' : 'import'} { FragmentDefinitionNode } from 'graphql';\n`;
53
98
  if (augmentedModuleName == null) {
54
99
  return [
55
100
  documentNodeImport,
101
+ deferFragmentHelperImports,
56
102
  `\n`,
57
103
  fragmentTypeHelper,
58
104
  `\n`,
59
105
  createUnmaskFunction(unmaskFunctionName),
60
106
  `\n`,
61
107
  makeFragmentDataHelper,
108
+ `\n`,
109
+ isFragmentReadyFunction(isStringDocumentMode),
62
110
  ].join(``);
63
111
  }
64
112
  return [
package/cjs/index.js CHANGED
@@ -141,6 +141,8 @@ exports.preset = {
141
141
  config: {
142
142
  useTypeImports: options.config.useTypeImports,
143
143
  unmaskFunctionName: fragmentMaskingConfig.unmaskFunctionName,
144
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
145
+ isStringDocumentMode: options.config.documentMode === visitor_plugin_common_1.DocumentMode.string,
144
146
  },
145
147
  documents: [],
146
148
  documentTransforms: options.documentTransforms,
@@ -17,15 +17,19 @@ export function makeFragmentData<
17
17
  return data as FragmentType<F>;
18
18
  }`;
19
19
  const defaultUnmaskFunctionName = 'useFragment';
20
- const modifyType = (rawType, opts) => `${opts.list === 'only-list'
21
- ? `ReadonlyArray<${rawType}>`
22
- : opts.list === 'with-list'
23
- ? `${rawType} | ReadonlyArray<${rawType}>`
24
- : rawType}${opts.nullable ? ' | null | undefined' : ''}`;
25
- const createUnmaskFunctionTypeDefinition = (unmaskFunctionName = defaultUnmaskFunctionName, opts) => `export function ${unmaskFunctionName}<TType>(
20
+ const modifyType = (rawType, opts) => {
21
+ return `${opts.list === 'only-list'
22
+ ? `ReadonlyArray<${rawType}>`
23
+ : opts.list === 'with-list'
24
+ ? `${rawType} | ReadonlyArray<${rawType}>`
25
+ : rawType}${opts.nullable ? ' | null | undefined' : ''}`;
26
+ };
27
+ const createUnmaskFunctionTypeDefinition = (unmaskFunctionName = defaultUnmaskFunctionName, opts) => {
28
+ return `export function ${unmaskFunctionName}<TType>(
26
29
  _documentNode: DocumentTypeDecoration<TType, any>,
27
- fragmentType: ${modifyType('FragmentType<DocumentTypeDecoration<TType, any>>', opts)}
30
+ fragmentType: ${modifyType(`FragmentType<DocumentTypeDecoration<TType, any>>`, opts)}
28
31
  ): ${modifyType('TType', opts)}`;
32
+ };
29
33
  const createUnmaskFunctionTypeDefinitions = (unmaskFunctionName = defaultUnmaskFunctionName) => [
30
34
  `// return non-nullable if \`fragmentType\` is non-nullable\n${createUnmaskFunctionTypeDefinition(unmaskFunctionName, { nullable: false, list: false })}`,
31
35
  `// return nullable if \`fragmentType\` is nullable\n${createUnmaskFunctionTypeDefinition(unmaskFunctionName, {
@@ -42,20 +46,64 @@ ${createUnmaskFunctionTypeDefinitions(unmaskFunctionName)
42
46
  return fragmentType as any;
43
47
  }
44
48
  `;
49
+ const isFragmentReadyFunction = (isStringDocumentMode) => {
50
+ if (isStringDocumentMode) {
51
+ return `\
52
+ export function isFragmentReady<TQuery, TFrag>(
53
+ queryNode: TypedDocumentString<TQuery, any>,
54
+ fragmentNode: TypedDocumentString<TFrag, any>,
55
+ fragment: Partial<TFrag>
56
+ ): fragment is FragmentType<typeof fragmentNode> {
57
+ const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
58
+
59
+ if (!deferredFields) return true;
60
+
61
+ const fragName = fragmentNode.__meta__?.fragmentName;
62
+
63
+ const fields = fragName ? deferredFields[fragName] : [];
64
+ return fields.length > 0 && fields.some(field => fragment && field in fragment);
65
+ }
66
+ `;
67
+ }
68
+ return `\
69
+ export function isFragmentReady<TQuery, TFrag>(
70
+ queryNode: DocumentTypeDecoration<TQuery, any>,
71
+ fragmentNode: TypedDocumentNode<TFrag>,
72
+ fragment: Partial<TFrag>
73
+ ): fragment is FragmentType<typeof fragmentNode> {
74
+ const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
75
+ ?.deferredFields;
76
+
77
+ if (!deferredFields) return true;
78
+
79
+ const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
80
+ const fragName = fragDef?.name?.value;
81
+
82
+ const fields = fragName ? deferredFields[fragName] : [];
83
+ return fields.length > 0 && fields.some(field => fragment && field in fragment);
84
+ }
85
+ `;
86
+ };
45
87
  /**
46
88
  * Plugin for generating fragment masking helper functions.
47
89
  */
48
- export const plugin = (_, __, { useTypeImports, augmentedModuleName, unmaskFunctionName }, _info) => {
49
- const documentNodeImport = `${useTypeImports ? 'import type' : 'import'} { ResultOf, DocumentTypeDecoration, } from '@graphql-typed-document-node/core';\n`;
90
+ export const plugin = (_, __, { useTypeImports, augmentedModuleName, unmaskFunctionName, emitLegacyCommonJSImports, isStringDocumentMode }, _info) => {
91
+ const documentNodeImport = `${useTypeImports ? 'import type' : 'import'} { ResultOf, DocumentTypeDecoration${isStringDocumentMode ? '' : ', TypedDocumentNode'} } from '@graphql-typed-document-node/core';\n`;
92
+ const deferFragmentHelperImports = isStringDocumentMode
93
+ ? `${useTypeImports ? 'import type' : 'import'} { TypedDocumentString } from './graphql${emitLegacyCommonJSImports ? '' : '.js'}';\n`
94
+ : `${useTypeImports ? 'import type' : 'import'} { FragmentDefinitionNode } from 'graphql';\n`;
50
95
  if (augmentedModuleName == null) {
51
96
  return [
52
97
  documentNodeImport,
98
+ deferFragmentHelperImports,
53
99
  `\n`,
54
100
  fragmentTypeHelper,
55
101
  `\n`,
56
102
  createUnmaskFunction(unmaskFunctionName),
57
103
  `\n`,
58
104
  makeFragmentDataHelper,
105
+ `\n`,
106
+ isFragmentReadyFunction(isStringDocumentMode),
59
107
  ].join(``);
60
108
  }
61
109
  return [
package/esm/index.js CHANGED
@@ -3,7 +3,7 @@ import * as gqlTagPlugin from '@graphql-codegen/gql-tag-operations';
3
3
  import * as typedDocumentNodePlugin from '@graphql-codegen/typed-document-node';
4
4
  import * as typescriptPlugin from '@graphql-codegen/typescript';
5
5
  import * as typescriptOperationPlugin from '@graphql-codegen/typescript-operations';
6
- import { ClientSideBaseVisitor } from '@graphql-codegen/visitor-plugin-common';
6
+ import { ClientSideBaseVisitor, DocumentMode } from '@graphql-codegen/visitor-plugin-common';
7
7
  import * as fragmentMaskingPlugin from './fragment-masking-plugin.js';
8
8
  import { generateDocumentHash, normalizeAndPrintDocumentNode } from './persisted-documents.js';
9
9
  import { processSources } from './process-sources.js';
@@ -136,6 +136,8 @@ export const preset = {
136
136
  config: {
137
137
  useTypeImports: options.config.useTypeImports,
138
138
  unmaskFunctionName: fragmentMaskingConfig.unmaskFunctionName,
139
+ emitLegacyCommonJSImports: options.config.emitLegacyCommonJSImports,
140
+ isStringDocumentMode: options.config.documentMode === DocumentMode.string,
139
141
  },
140
142
  documents: [],
141
143
  documentTransforms: options.documentTransforms,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/client-preset",
3
- "version": "3.0.0",
3
+ "version": "3.0.1-alpha-20230404095953-c13d5890d",
4
4
  "description": "GraphQL Code Generator preset for client.",
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"
@@ -9,12 +9,12 @@
9
9
  "@babel/helper-plugin-utils": "^7.20.2",
10
10
  "@babel/template": "^7.20.7",
11
11
  "@graphql-codegen/add": "^4.0.1",
12
- "@graphql-codegen/typed-document-node": "^4.0.0",
13
- "@graphql-codegen/typescript": "^3.0.3",
14
- "@graphql-codegen/typescript-operations": "^3.0.3",
15
- "@graphql-codegen/gql-tag-operations": "3.0.0",
12
+ "@graphql-codegen/typed-document-node": "4.0.1-alpha-20230404095953-c13d5890d",
13
+ "@graphql-codegen/typescript": "3.0.4-alpha-20230404095953-c13d5890d",
14
+ "@graphql-codegen/typescript-operations": "3.1.0-alpha-20230404095953-c13d5890d",
15
+ "@graphql-codegen/gql-tag-operations": "3.0.1-alpha-20230404095953-c13d5890d",
16
16
  "@graphql-codegen/plugin-helpers": "^4.2.0",
17
- "@graphql-codegen/visitor-plugin-common": "^3.1.0",
17
+ "@graphql-codegen/visitor-plugin-common": "3.2.0-alpha-20230404095953-c13d5890d",
18
18
  "@graphql-typed-document-node/core": "3.2.0",
19
19
  "@graphql-tools/documents": "^0.1.0",
20
20
  "@graphql-tools/utils": "^9.0.0",
@@ -6,4 +6,6 @@ export declare const plugin: PluginFunction<{
6
6
  useTypeImports?: boolean;
7
7
  augmentedModuleName?: string;
8
8
  unmaskFunctionName?: string;
9
+ emitLegacyCommonJSImports?: boolean;
10
+ isStringDocumentMode?: boolean;
9
11
  }>;
@@ -6,4 +6,6 @@ export declare const plugin: PluginFunction<{
6
6
  useTypeImports?: boolean;
7
7
  augmentedModuleName?: string;
8
8
  unmaskFunctionName?: string;
9
+ emitLegacyCommonJSImports?: boolean;
10
+ isStringDocumentMode?: boolean;
9
11
  }>;