Skip to content

Commit 5f10733

Browse files
authored
Support defer directive on nested fragments (#9384)
* Support defer directive on nested fragments * Add changeset
1 parent 2f8e904 commit 5f10733

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

.changeset/chilly-moons-yawn.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/client-preset': patch
4+
---
5+
6+
Support @defer directive in nested fragments

packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,15 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
756756
return null;
757757
}
758758

759-
return { name: declarationName, content: possibleFields.join(' & ') };
759+
const content = possibleFields
760+
.map(selectionObject => {
761+
if (typeof selectionObject === 'string') return selectionObject;
762+
763+
return '(' + selectionObject.union.join(' | ') + ')';
764+
})
765+
.join(' & ');
766+
767+
return { name: declarationName, content };
760768
})
761769
.filter(Boolean);
762770

packages/presets/client/tests/client-preset.spec.ts

+60-2
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,7 @@ export * from "./gql.js";`);
18031803
}
18041804
18051805
type Foo {
1806+
id: String
18061807
value: String
18071808
}
18081809
`,
@@ -1837,6 +1838,7 @@ export * from "./gql.js";`);
18371838
18381839
export type Foo = {
18391840
__typename?: 'Foo';
1841+
id?: Maybe<Scalars['String']>;
18401842
value?: Maybe<Scalars['String']>;
18411843
};
18421844
@@ -1864,7 +1866,10 @@ export * from "./gql.js";`);
18641866
18651867
export type FooFragment = { __typename?: 'Foo', value?: string | null } & { ' $fragmentName'?: 'FooFragment' };
18661868
1869+
export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };
1870+
18671871
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>;
1872+
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>;
18681873
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>;
18691874
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>;"
18701875
`);
@@ -1880,6 +1885,7 @@ export * from "./gql.js";`);
18801885
}
18811886
18821887
type Foo {
1888+
id: String
18831889
value: String
18841890
}
18851891
`,
@@ -1917,6 +1923,7 @@ export * from "./gql.js";`);
19171923
19181924
export type Foo = {
19191925
__typename?: 'Foo';
1926+
id?: Maybe<Scalars['String']>;
19201927
value?: Maybe<Scalars['String']>;
19211928
};
19221929
@@ -1944,7 +1951,10 @@ export * from "./gql.js";`);
19441951
19451952
export type FooFragment = { __typename?: 'Foo', value?: string | null } & { ' $fragmentName'?: 'FooFragment' };
19461953
1954+
export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };
1955+
19471956
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>;
1957+
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>;
19481958
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>;
19491959
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>;"
19501960
`);
@@ -1960,6 +1970,7 @@ export * from "./gql.js";`);
19601970
}
19611971
19621972
type Foo {
1973+
id: String
19631974
value: String
19641975
}
19651976
`,
@@ -1997,6 +2008,7 @@ export * from "./gql.js";`);
19972008
19982009
export type Foo = {
19992010
__typename?: 'Foo';
2011+
id?: Maybe<Scalars['String']>;
20002012
value?: Maybe<Scalars['String']>;
20012013
};
20022014
@@ -2024,6 +2036,8 @@ export * from "./gql.js";`);
20242036
20252037
export type FooFragment = { __typename?: 'Foo', value?: string | null } & { ' $fragmentName'?: 'FooFragment' };
20262038
2039+
export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };
2040+
20272041
export class TypedDocumentString<TResult, TVariables>
20282042
extends String
20292043
implements DocumentTypeDecoration<TResult, TVariables>
@@ -2043,6 +2057,14 @@ export * from "./gql.js";`);
20432057
value
20442058
}
20452059
\`, {"fragmentName":"Foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
2060+
export const FooFragmentDoc = new TypedDocumentString(\`
2061+
fragment foo on Foo {
2062+
id
2063+
... on Foo @defer {
2064+
value
2065+
}
2066+
}
2067+
\`, {"fragmentName":"foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
20462068
export const FooDocument = new TypedDocumentString(\`
20472069
query Foo {
20482070
foo {
@@ -2051,6 +2073,12 @@ export * from "./gql.js";`);
20512073
}
20522074
fragment Foo on Foo {
20532075
value
2076+
}
2077+
fragment foo on Foo {
2078+
id
2079+
... on Foo @defer {
2080+
value
2081+
}
20542082
}\`, {"deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FooQuery, FooQueryVariables>;
20552083
export const FoosDocument = new TypedDocumentString(\`
20562084
query Foos {
@@ -2060,6 +2088,12 @@ export * from "./gql.js";`);
20602088
}
20612089
fragment Foo on Foo {
20622090
value
2091+
}
2092+
fragment foo on Foo {
2093+
id
2094+
... on Foo @defer {
2095+
value
2096+
}
20632097
}\`, {"deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FoosQuery, FoosQueryVariables>;"
20642098
`);
20652099
});
@@ -2074,6 +2108,7 @@ export * from "./gql.js";`);
20742108
}
20752109
20762110
type Foo {
2111+
id: String
20772112
value: String
20782113
}
20792114
`,
@@ -2114,6 +2149,7 @@ export * from "./gql.js";`);
21142149
21152150
export type Foo = {
21162151
__typename?: 'Foo';
2152+
id?: Maybe<Scalars['String']>;
21172153
value?: Maybe<Scalars['String']>;
21182154
};
21192155
@@ -2141,6 +2177,8 @@ export * from "./gql.js";`);
21412177
21422178
export type FooFragment = { __typename?: 'Foo', value?: string | null } & { ' $fragmentName'?: 'FooFragment' };
21432179
2180+
export type FooFragment = { __typename?: 'Foo', id?: string | null } & ({ __typename?: 'Foo', value?: string | null } | { __typename?: 'Foo', value?: never }) & { ' $fragmentName'?: 'FooFragment' };
2181+
21442182
export class TypedDocumentString<TResult, TVariables>
21452183
extends String
21462184
implements DocumentTypeDecoration<TResult, TVariables>
@@ -2160,6 +2198,14 @@ export * from "./gql.js";`);
21602198
value
21612199
}
21622200
\`, {"fragmentName":"Foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
2201+
export const FooFragmentDoc = new TypedDocumentString(\`
2202+
fragment foo on Foo {
2203+
id
2204+
... on Foo @defer {
2205+
value
2206+
}
2207+
}
2208+
\`, {"fragmentName":"foo"}) as unknown as TypedDocumentString<FooFragment, unknown>;
21632209
export const FooDocument = new TypedDocumentString(\`
21642210
query Foo {
21652211
foo {
@@ -2168,7 +2214,13 @@ export * from "./gql.js";`);
21682214
}
21692215
fragment Foo on Foo {
21702216
value
2171-
}\`, {"hash":"39c47d2da0fb0e6867abbe2ec942d9858f2d76c7","deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FooQuery, FooQueryVariables>;
2217+
}
2218+
fragment foo on Foo {
2219+
id
2220+
... on Foo @defer {
2221+
value
2222+
}
2223+
}\`, {"hash":"2687841b00fe0b3b4fd0dfa2e943f80936594f58","deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FooQuery, FooQueryVariables>;
21722224
export const FoosDocument = new TypedDocumentString(\`
21732225
query Foos {
21742226
foos {
@@ -2177,7 +2229,13 @@ export * from "./gql.js";`);
21772229
}
21782230
fragment Foo on Foo {
21792231
value
2180-
}\`, {"hash":"8aba765173b2302b9857334e9959d97a2168dbc8","deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FoosQuery, FoosQueryVariables>;"
2232+
}
2233+
fragment foo on Foo {
2234+
id
2235+
... on Foo @defer {
2236+
value
2237+
}
2238+
}\`, {"hash":"8db613cc1f12f64dbde9cd6fef167fd12246330d","deferredFields":{"Foo":["value"]}}) as unknown as TypedDocumentString<FoosQuery, FoosQueryVariables>;"
21812239
`);
21822240
});
21832241
});

packages/presets/client/tests/fixtures/with-deferred-fragment.ts

+10
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ const Fragment = gql(/* GraphQL */ `
2424
value
2525
}
2626
`);
27+
28+
//@ts-ignore
29+
const NestedFragment = gql(/* GraphQL */ `
30+
fragment foo on Foo {
31+
id
32+
... on Foo @defer {
33+
value
34+
}
35+
}
36+
`);

0 commit comments

Comments
 (0)