Skip to content

Commit

Permalink
Make isFragmentReady function work with enabled noUncheckedIndexedAcc…
Browse files Browse the repository at this point in the history
…ess tsc setting (#9385)

* Make isFragmentReady function work with enabled noUncheckedIndexedAccess tsc setting

* Add changeset

* Trigger ci

* Change function for string mode

* Update isFragmentReady in examples
  • Loading branch information
beerose authored May 12, 2023
1 parent 88c4b49 commit a7dda35
Show file tree
Hide file tree
Showing 27 changed files with 44 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-buckets-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/client-preset': patch
---

Improve isFragmentReady utility function to work with noUncheckedIndexedAccess TSC setting
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion dev-test/gql-tag-operations-urql/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion dev-test/gql-tag-operations/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion dev-test/gql-tag-operations/graphql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export function isFragmentReady<TQuery, TFrag>(
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
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);
}
2 changes: 1 addition & 1 deletion examples/persisted-documents/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 2 additions & 0 deletions examples/programmatic-typescript/src/gql.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
export type Incremental<T> = 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
3 changes: 2 additions & 1 deletion examples/react/apollo-client-defer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/react/apollo-client/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/react/http-executor/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/react/nextjs-swr/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export function isFragmentReady<TQuery, TFrag>(
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
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);
}
7 changes: 3 additions & 4 deletions examples/react/urql/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export function isFragmentReady<TQuery, TFrag>(
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
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);
}
2 changes: 1 addition & 1 deletion examples/typescript-esm/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export function isFragmentReady<TQuery, TFrag>(
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
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);
}
2 changes: 1 addition & 1 deletion examples/vite/vite-react-cts/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/vite/vite-react-mts/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/vite/vite-react-ts/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/vue/apollo-composable/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/vue/urql/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/vue/villus/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
2 changes: 1 addition & 1 deletion examples/yoga-tests/src/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
9 changes: 4 additions & 5 deletions packages/presets/client/src/fragment-masking-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ export function isFragmentReady<TQuery, TFrag>(
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
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);
}
`;
Expand All @@ -105,7 +104,7 @@ export function isFragmentReady<TQuery, TFrag>(
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);
}
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/presets/client/tests/client-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
"
Expand Down

0 comments on commit a7dda35

Please sign in to comment.