Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export class ClientSideBaseVisitor<
}

let metaString = '';
if (this._onExecutableDocumentNode && node.kind === Kind.OPERATION_DEFINITION) {
if (this._onExecutableDocumentNode) {
const meta = this._getGraphQLCodegenMetadata(node, definitions);

if (meta) {
Expand All @@ -415,7 +415,9 @@ export class ClientSideBaseVisitor<

if (this.config.documentMode === DocumentMode.string) {
if (node.kind === Kind.FRAGMENT_DEFINITION) {
return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify({ fragmentName: node.name.value })})`;
const meta = this._getGraphQLCodegenMetadata(node, gqlTag([doc]).definitions);

return `new TypedDocumentString(\`${doc}\`, ${JSON.stringify({ fragmentName: node.name.value, ...meta })})`;
}

if (this._onExecutableDocumentNode && node.kind === Kind.OPERATION_DEFINITION) {
Expand All @@ -438,15 +440,17 @@ export class ClientSideBaseVisitor<
}

protected _getGraphQLCodegenMetadata(
node: OperationDefinitionNode,
node: OperationDefinitionNode | FragmentDefinitionNode,
definitions?: ReadonlyArray<DefinitionNode>
): Record<string, any> | void | undefined {
let meta: Record<string, any> | void | undefined;

meta = this._onExecutableDocumentNode({
kind: Kind.DOCUMENT,
definitions,
});
if (node.kind === Kind.OPERATION_DEFINITION) {
meta = this._onExecutableDocumentNode({
kind: Kind.DOCUMENT,
definitions,
});
}

const deferredFields = this._findDeferredFields(node);
if (Object.keys(deferredFields).length) {
Expand All @@ -459,7 +463,9 @@ export class ClientSideBaseVisitor<
return meta;
}

protected _findDeferredFields(node: OperationDefinitionNode): { [fargmentName: string]: string[] } {
protected _findDeferredFields(node: OperationDefinitionNode | FragmentDefinitionNode): {
[fargmentName: string]: string[];
} {
const deferredFields: { [fargmentName: string]: string[] } = {};
const queue: SelectionNode[] = [...node.selectionSet.selections];
while (queue.length) {
Expand Down
50 changes: 50 additions & 0 deletions packages/presets/client/tests/client-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2289,8 +2289,14 @@ export * from "./gql.js";`);

export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };

export type FooNestedFragment = { __typename?: 'Foo', id?: string | null } & (
{ __typename?: 'Foo' }
& { ' $fragmentRefs'?: { 'FooFragment': Incremental<FooFragment> } }
) & { ' $fragmentName'?: 'FooNestedFragment' };

export const FooFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FooFragment, unknown>;
export const FooFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]}}]} as unknown as DocumentNode<FooFragment, unknown>;
export const FooNestedFragmentDoc = {"__meta__":{"deferredFields":{"foo":["id"]}},"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"fooNested"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]}}]} as unknown as DocumentNode<FooNestedFragment, unknown>;
export const FooDocument = {"__meta__":{"deferredFields":{"Foo":["value"]}},"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Foo"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"foo"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FooQuery, FooQueryVariables>;
export const FoosDocument = {"__meta__":{"deferredFields":{"Foo":["value"]}},"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Foos"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"foos"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FoosQuery, FoosQueryVariables>;"
`);
Expand Down Expand Up @@ -2374,8 +2380,14 @@ export * from "./gql.js";`);

export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };

export type FooNestedFragment = { __typename?: 'Foo', id?: string | null } & (
{ __typename?: 'Foo' }
& { ' $fragmentRefs'?: { 'FooFragment': Incremental<FooFragment> } }
) & { ' $fragmentName'?: 'FooNestedFragment' };

export const FooFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FooFragment, unknown>;
export const FooFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]}}]} as unknown as DocumentNode<FooFragment, unknown>;
export const FooNestedFragmentDoc = {"__meta__":{"deferredFields":{"foo":["id"]}},"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"fooNested"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]}}]} as unknown as DocumentNode<FooNestedFragment, unknown>;
export const FooDocument = {"__meta__":{"hash":"39c47d2da0fb0e6867abbe2ec942d9858f2d76c7","deferredFields":{"Foo":["value"]}},"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Foo"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"foo"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FooQuery, FooQueryVariables>;
export const FoosDocument = {"__meta__":{"hash":"8aba765173b2302b9857334e9959d97a2168dbc8","deferredFields":{"Foo":["value"]}},"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Foos"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"foos"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"Foo"},"directives":[{"kind":"Directive","name":{"kind":"Name","value":"defer"}}]}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"Foo"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Foo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<FoosQuery, FoosQueryVariables>;"
`);
Expand Down Expand Up @@ -2459,6 +2471,11 @@ export * from "./gql.js";`);

export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };

export type FooNestedFragment = { __typename?: 'Foo', id?: string | null } & (
{ __typename?: 'Foo' }
& { ' $fragmentRefs'?: { 'FooFragment': Incremental<FooFragment> } }
) & { ' $fragmentName'?: 'FooNestedFragment' };

export class TypedDocumentString<TResult, TVariables>
extends String
implements DocumentTypeDecoration<TResult, TVariables>
Expand Down Expand Up @@ -2490,6 +2507,20 @@ export * from "./gql.js";`);
}
}
\`, {"fragmentName":"foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
export const FooNestedFragmentDoc = new TypedDocumentString(\`
fragment fooNested on Foo {
id
...foo @defer
}
fragment Foo on Foo {
value
}
fragment foo on Foo {
id
... on Foo @defer {
value
}
}\`, {"fragmentName":"fooNested","deferredFields":{"foo":["id"]}}) as unknown as TypedDocumentString<FooNestedFragment, unknown>;
export const FooDocument = new TypedDocumentString(\`
query Foo {
foo {
Expand Down Expand Up @@ -2604,6 +2635,11 @@ export * from "./gql.js";`);

export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };

export type FooNestedFragment = { __typename?: 'Foo', id?: string | null } & (
{ __typename?: 'Foo' }
& { ' $fragmentRefs'?: { 'FooFragment': Incremental<FooFragment> } }
) & { ' $fragmentName'?: 'FooNestedFragment' };

export class TypedDocumentString<TResult, TVariables>
extends String
implements DocumentTypeDecoration<TResult, TVariables>
Expand Down Expand Up @@ -2635,6 +2671,20 @@ export * from "./gql.js";`);
}
}
\`, {"fragmentName":"foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
export const FooNestedFragmentDoc = new TypedDocumentString(\`
fragment fooNested on Foo {
id
...foo @defer
}
fragment Foo on Foo {
value
}
fragment foo on Foo {
id
... on Foo @defer {
value
}
}\`, {"fragmentName":"fooNested","deferredFields":{"foo":["id"]}}) as unknown as TypedDocumentString<FooNestedFragment, unknown>;
export const FooDocument = new TypedDocumentString(\`
query Foo {
foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ const NestedFragment = gql(/* GraphQL */ `
}
}
`);

//@ts-ignore
const NestedFragmentWithFragment = gql(/* GraphQL */ `
fragment fooNested on Foo {
id
...foo @defer
}
`);
Loading