From 298017fab4f7135e4145a0b2b6548b778fe5b29e Mon Sep 17 00:00:00 2001 From: beerose Date: Wed, 10 May 2023 17:37:43 +0200 Subject: [PATCH 1/5] Make isFragmentReady function work with enabled noUncheckedIndexedAccess tsc setting --- dev-test/gql-tag-operations-masking/gql/fragment-masking.ts | 2 +- dev-test/gql-tag-operations-urql/gql/fragment-masking.ts | 2 +- dev-test/gql-tag-operations/gql/fragment-masking.ts | 2 +- dev-test/gql-tag-operations/graphql/fragment-masking.ts | 2 +- .../src/gql/fragment-masking.ts | 2 +- examples/persisted-documents/src/gql/fragment-masking.ts | 2 +- .../react/apollo-client-defer/src/gql/fragment-masking.ts | 2 +- examples/react/apollo-client-defer/tsconfig.json | 3 ++- .../apollo-client-swc-plugin/src/gql/fragment-masking.ts | 2 +- examples/react/apollo-client/src/gql/fragment-masking.ts | 2 +- examples/react/http-executor/src/gql/fragment-masking.ts | 2 +- examples/react/nextjs-swr/gql/fragment-masking.ts | 2 +- .../react/tanstack-react-query/src/gql/fragment-masking.ts | 2 +- examples/react/urql/src/gql/fragment-masking.ts | 2 +- examples/typescript-esm/src/gql/fragment-masking.ts | 2 +- .../typescript-graphql-request/src/gql/fragment-masking.ts | 2 +- examples/vite/vite-react-cts/src/gql/fragment-masking.ts | 2 +- examples/vite/vite-react-mts/src/gql/fragment-masking.ts | 2 +- examples/vite/vite-react-ts/src/gql/fragment-masking.ts | 2 +- examples/vue/apollo-composable/src/gql/fragment-masking.ts | 2 +- examples/vue/urql/src/gql/fragment-masking.ts | 2 +- examples/vue/villus/src/gql/fragment-masking.ts | 2 +- examples/yoga-tests/src/gql/fragment-masking.ts | 2 +- packages/presets/client/src/fragment-masking-plugin.ts | 4 ++-- packages/presets/client/tests/client-preset.spec.ts | 2 +- 25 files changed, 27 insertions(+), 26 deletions(-) diff --git a/dev-test/gql-tag-operations-masking/gql/fragment-masking.ts b/dev-test/gql-tag-operations-masking/gql/fragment-masking.ts index 0cedfd53e94..f109b6f5459 100644 --- a/dev-test/gql-tag-operations-masking/gql/fragment-masking.ts +++ b/dev-test/gql-tag-operations-masking/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/dev-test/gql-tag-operations-urql/gql/fragment-masking.ts b/dev-test/gql-tag-operations-urql/gql/fragment-masking.ts index 0cedfd53e94..f109b6f5459 100644 --- a/dev-test/gql-tag-operations-urql/gql/fragment-masking.ts +++ b/dev-test/gql-tag-operations-urql/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/dev-test/gql-tag-operations/gql/fragment-masking.ts b/dev-test/gql-tag-operations/gql/fragment-masking.ts index 0cedfd53e94..f109b6f5459 100644 --- a/dev-test/gql-tag-operations/gql/fragment-masking.ts +++ b/dev-test/gql-tag-operations/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/dev-test/gql-tag-operations/graphql/fragment-masking.ts b/dev-test/gql-tag-operations/graphql/fragment-masking.ts index 0cedfd53e94..f109b6f5459 100644 --- a/dev-test/gql-tag-operations/graphql/fragment-masking.ts +++ b/dev-test/gql-tag-operations/graphql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts b/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts index f86d241c2c2..96caca5d060 100644 --- a/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts +++ b/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts @@ -58,6 +58,6 @@ export function isFragmentReady( const fragName = fragmentNode.__meta__?.fragmentName; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/persisted-documents/src/gql/fragment-masking.ts b/examples/persisted-documents/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/persisted-documents/src/gql/fragment-masking.ts +++ b/examples/persisted-documents/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/apollo-client-defer/src/gql/fragment-masking.ts b/examples/react/apollo-client-defer/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/react/apollo-client-defer/src/gql/fragment-masking.ts +++ b/examples/react/apollo-client-defer/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/apollo-client-defer/tsconfig.json b/examples/react/apollo-client-defer/tsconfig.json index b557c4047ca..9d8d03efd4b 100644 --- a/examples/react/apollo-client-defer/tsconfig.json +++ b/examples/react/apollo-client-defer/tsconfig.json @@ -11,7 +11,8 @@ "esModuleInterop": true, "lib": ["ESNext", "DOM"], "skipLibCheck": true, - "noEmit": true + "noEmit": true, + "noUncheckedIndexedAccess": true }, "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.node.json" }] diff --git a/examples/react/apollo-client-swc-plugin/src/gql/fragment-masking.ts b/examples/react/apollo-client-swc-plugin/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/react/apollo-client-swc-plugin/src/gql/fragment-masking.ts +++ b/examples/react/apollo-client-swc-plugin/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/apollo-client/src/gql/fragment-masking.ts b/examples/react/apollo-client/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/react/apollo-client/src/gql/fragment-masking.ts +++ b/examples/react/apollo-client/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/http-executor/src/gql/fragment-masking.ts b/examples/react/http-executor/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/react/http-executor/src/gql/fragment-masking.ts +++ b/examples/react/http-executor/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/nextjs-swr/gql/fragment-masking.ts b/examples/react/nextjs-swr/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/react/nextjs-swr/gql/fragment-masking.ts +++ b/examples/react/nextjs-swr/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/tanstack-react-query/src/gql/fragment-masking.ts b/examples/react/tanstack-react-query/src/gql/fragment-masking.ts index f86d241c2c2..96caca5d060 100644 --- a/examples/react/tanstack-react-query/src/gql/fragment-masking.ts +++ b/examples/react/tanstack-react-query/src/gql/fragment-masking.ts @@ -58,6 +58,6 @@ export function isFragmentReady( const fragName = fragmentNode.__meta__?.fragmentName; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/urql/src/gql/fragment-masking.ts b/examples/react/urql/src/gql/fragment-masking.ts index f86d241c2c2..96caca5d060 100644 --- a/examples/react/urql/src/gql/fragment-masking.ts +++ b/examples/react/urql/src/gql/fragment-masking.ts @@ -58,6 +58,6 @@ export function isFragmentReady( const fragName = fragmentNode.__meta__?.fragmentName; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/typescript-esm/src/gql/fragment-masking.ts b/examples/typescript-esm/src/gql/fragment-masking.ts index 0cedfd53e94..f109b6f5459 100644 --- a/examples/typescript-esm/src/gql/fragment-masking.ts +++ b/examples/typescript-esm/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/typescript-graphql-request/src/gql/fragment-masking.ts b/examples/typescript-graphql-request/src/gql/fragment-masking.ts index f86d241c2c2..96caca5d060 100644 --- a/examples/typescript-graphql-request/src/gql/fragment-masking.ts +++ b/examples/typescript-graphql-request/src/gql/fragment-masking.ts @@ -58,6 +58,6 @@ export function isFragmentReady( const fragName = fragmentNode.__meta__?.fragmentName; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vite/vite-react-cts/src/gql/fragment-masking.ts b/examples/vite/vite-react-cts/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/vite/vite-react-cts/src/gql/fragment-masking.ts +++ b/examples/vite/vite-react-cts/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vite/vite-react-mts/src/gql/fragment-masking.ts b/examples/vite/vite-react-mts/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/vite/vite-react-mts/src/gql/fragment-masking.ts +++ b/examples/vite/vite-react-mts/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vite/vite-react-ts/src/gql/fragment-masking.ts b/examples/vite/vite-react-ts/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/vite/vite-react-ts/src/gql/fragment-masking.ts +++ b/examples/vite/vite-react-ts/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vue/apollo-composable/src/gql/fragment-masking.ts b/examples/vue/apollo-composable/src/gql/fragment-masking.ts index 7fdcdff8304..bd7082b3c46 100644 --- a/examples/vue/apollo-composable/src/gql/fragment-masking.ts +++ b/examples/vue/apollo-composable/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vue/urql/src/gql/fragment-masking.ts b/examples/vue/urql/src/gql/fragment-masking.ts index 7fdcdff8304..bd7082b3c46 100644 --- a/examples/vue/urql/src/gql/fragment-masking.ts +++ b/examples/vue/urql/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/vue/villus/src/gql/fragment-masking.ts b/examples/vue/villus/src/gql/fragment-masking.ts index 7fdcdff8304..bd7082b3c46 100644 --- a/examples/vue/villus/src/gql/fragment-masking.ts +++ b/examples/vue/villus/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/yoga-tests/src/gql/fragment-masking.ts b/examples/yoga-tests/src/gql/fragment-masking.ts index f895cdecd67..1e02cd84dae 100644 --- a/examples/yoga-tests/src/gql/fragment-masking.ts +++ b/examples/yoga-tests/src/gql/fragment-masking.ts @@ -61,6 +61,6 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/packages/presets/client/src/fragment-masking-plugin.ts b/packages/presets/client/src/fragment-masking-plugin.ts index 66cbdd1fe89..b0e671e9ecc 100644 --- a/packages/presets/client/src/fragment-masking-plugin.ts +++ b/packages/presets/client/src/fragment-masking-plugin.ts @@ -86,7 +86,7 @@ export function isFragmentReady( const fragName = fragmentNode.__meta__?.fragmentName; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } `; @@ -105,7 +105,7 @@ export function isFragmentReady( const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } `; diff --git a/packages/presets/client/tests/client-preset.spec.ts b/packages/presets/client/tests/client-preset.spec.ts index 60a6260df75..271d0bc1c08 100644 --- a/packages/presets/client/tests/client-preset.spec.ts +++ b/packages/presets/client/tests/client-preset.spec.ts @@ -817,7 +817,7 @@ export * from "./gql";`); const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined; const fragName = fragDef?.name?.value; - const fields = fragName ? deferredFields[fragName] : []; + const fields = (fragName && deferredFields[fragName]) || []; return fields.length > 0 && fields.every(field => data && field in data); } " From 6ac24d7964030ab21a0da0b085453460d568fdcc Mon Sep 17 00:00:00 2001 From: beerose Date: Wed, 10 May 2023 20:11:26 +0200 Subject: [PATCH 2/5] Add changeset --- .changeset/four-buckets-carry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/four-buckets-carry.md diff --git a/.changeset/four-buckets-carry.md b/.changeset/four-buckets-carry.md new file mode 100644 index 00000000000..6bd7cd5db86 --- /dev/null +++ b/.changeset/four-buckets-carry.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/client-preset': patch +--- + +Improve isFragmentReady utility function to work with noUncheckedIndexedAccess TSC setting From a0824b52f2d5353813fe47428e4e989fd58c6a87 Mon Sep 17 00:00:00 2001 From: beerose Date: Wed, 10 May 2023 22:39:23 +0200 Subject: [PATCH 3/5] Trigger ci From 742e6af1bb824ba3759e27b15d755d9986259f41 Mon Sep 17 00:00:00 2001 From: beerose Date: Thu, 11 May 2023 16:52:51 +0200 Subject: [PATCH 4/5] Change function for string mode --- examples/programmatic-typescript/src/gql.generated.ts | 2 ++ .../typescript-graphql-request/src/gql/fragment-masking.ts | 7 +++---- packages/presets/client/src/fragment-masking-plugin.ts | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/programmatic-typescript/src/gql.generated.ts b/examples/programmatic-typescript/src/gql.generated.ts index be76243437c..66e27a84caf 100644 --- a/examples/programmatic-typescript/src/gql.generated.ts +++ b/examples/programmatic-typescript/src/gql.generated.ts @@ -5,6 +5,8 @@ export type InputMaybe = Maybe; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; +export type MakeEmpty = { [_ in K]?: never }; +export type Incremental = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; diff --git a/examples/typescript-graphql-request/src/gql/fragment-masking.ts b/examples/typescript-graphql-request/src/gql/fragment-masking.ts index 96caca5d060..d34cc62bbfc 100644 --- a/examples/typescript-graphql-request/src/gql/fragment-masking.ts +++ b/examples/typescript-graphql-request/src/gql/fragment-masking.ts @@ -53,11 +53,10 @@ export function isFragmentReady( data: FragmentType, any>> | null | undefined ): data is FragmentType { const deferredFields = queryNode.__meta__?.deferredFields as Record; + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined; - if (!deferredFields) return true; + if (!deferredFields || !fragName) return true; - const fragName = fragmentNode.__meta__?.fragmentName; - - const fields = (fragName && deferredFields[fragName]) || []; + const fields = deferredFields[fragName] ?? []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/packages/presets/client/src/fragment-masking-plugin.ts b/packages/presets/client/src/fragment-masking-plugin.ts index b0e671e9ecc..fa374b18132 100644 --- a/packages/presets/client/src/fragment-masking-plugin.ts +++ b/packages/presets/client/src/fragment-masking-plugin.ts @@ -81,12 +81,11 @@ export function isFragmentReady( data: FragmentType, any>> | null | undefined ): data is FragmentType { const deferredFields = queryNode.__meta__?.deferredFields as Record; + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined; - if (!deferredFields) return true; - - const fragName = fragmentNode.__meta__?.fragmentName; + if (!deferredFields || !fragName) return true; - const fields = (fragName && deferredFields[fragName]) || []; + const fields = deferredFields[fragName] ?? []; return fields.length > 0 && fields.every(field => data && field in data); } `; From d407521f7555ec0aa841a858834615652769033a Mon Sep 17 00:00:00 2001 From: beerose Date: Fri, 12 May 2023 17:07:17 +0200 Subject: [PATCH 5/5] Update isFragmentReady in examples --- .../src/gql/fragment-masking.ts | 7 +++---- .../react/tanstack-react-query/src/gql/fragment-masking.ts | 7 +++---- examples/react/urql/src/gql/fragment-masking.ts | 7 +++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts b/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts index 96caca5d060..d34cc62bbfc 100644 --- a/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts +++ b/examples/persisted-documents-string-mode/src/gql/fragment-masking.ts @@ -53,11 +53,10 @@ export function isFragmentReady( data: FragmentType, any>> | null | undefined ): data is FragmentType { const deferredFields = queryNode.__meta__?.deferredFields as Record; + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined; - if (!deferredFields) return true; + if (!deferredFields || !fragName) return true; - const fragName = fragmentNode.__meta__?.fragmentName; - - const fields = (fragName && deferredFields[fragName]) || []; + const fields = deferredFields[fragName] ?? []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/tanstack-react-query/src/gql/fragment-masking.ts b/examples/react/tanstack-react-query/src/gql/fragment-masking.ts index 96caca5d060..d34cc62bbfc 100644 --- a/examples/react/tanstack-react-query/src/gql/fragment-masking.ts +++ b/examples/react/tanstack-react-query/src/gql/fragment-masking.ts @@ -53,11 +53,10 @@ export function isFragmentReady( data: FragmentType, any>> | null | undefined ): data is FragmentType { const deferredFields = queryNode.__meta__?.deferredFields as Record; + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined; - if (!deferredFields) return true; + if (!deferredFields || !fragName) return true; - const fragName = fragmentNode.__meta__?.fragmentName; - - const fields = (fragName && deferredFields[fragName]) || []; + const fields = deferredFields[fragName] ?? []; return fields.length > 0 && fields.every(field => data && field in data); } diff --git a/examples/react/urql/src/gql/fragment-masking.ts b/examples/react/urql/src/gql/fragment-masking.ts index 96caca5d060..d34cc62bbfc 100644 --- a/examples/react/urql/src/gql/fragment-masking.ts +++ b/examples/react/urql/src/gql/fragment-masking.ts @@ -53,11 +53,10 @@ export function isFragmentReady( data: FragmentType, any>> | null | undefined ): data is FragmentType { const deferredFields = queryNode.__meta__?.deferredFields as Record; + const fragName = fragmentNode.__meta__?.fragmentName as string | undefined; - if (!deferredFields) return true; + if (!deferredFields || !fragName) return true; - const fragName = fragmentNode.__meta__?.fragmentName; - - const fields = (fragName && deferredFields[fragName]) || []; + const fields = deferredFields[fragName] ?? []; return fields.length > 0 && fields.every(field => data && field in data); }