Skip to content

Commit fd55e20

Browse files
authored
fix: inlineFragmentTypes config option (#7555)
* test: add test suite * fix: use correct conditional * chore: add changeset
1 parent 3da3733 commit fd55e20

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

‎.changeset/pretty-planets-occur.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/typescript-operations': patch
4+
---
5+
6+
fix incorrect type generation when using the inlineFragmentTypes 'combine' option that resulted in generating masked fragment output.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
492492
const fields = [...allStrings, mergedObjectsAsString].filter(Boolean);
493493

494494
if (fragmentsSpreadUsages.length) {
495-
if (this._config.inlineFragmentTypes === 'inline') {
495+
if (this._config.inlineFragmentTypes === 'combine') {
496496
fields.push(...fragmentsSpreadUsages);
497-
} else {
497+
} else if (this._config.inlineFragmentTypes === 'mask') {
498498
fields.push(`{ ' $fragmentRefs': { ${fragmentsSpreadUsages.map(name => `'${name}': ${name}`).join(`;`)} } }`);
499499
}
500500
}

‎packages/plugins/typescript/operations/tests/ts-documents.spec.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6029,4 +6029,93 @@ function test(q: GetEntityBrandDataQuery): void {
60296029
);
60306030
`);
60316031
});
6032+
6033+
describe('inlineFragmentTypes option', () => {
6034+
it("'combine' yields correct types", async () => {
6035+
const ast = parse(/* GraphQL */ `
6036+
query {
6037+
me {
6038+
...UserFragment
6039+
}
6040+
}
6041+
fragment UserFragment on User {
6042+
id
6043+
}
6044+
`);
6045+
const result = await plugin(
6046+
schema,
6047+
[{ location: 'test-file.ts', document: ast }],
6048+
{ inlineFragmentTypes: 'combine' },
6049+
{ outputFile: '' }
6050+
);
6051+
expect(result.content).toBeSimilarStringTo(`
6052+
export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never; }>;
6053+
6054+
6055+
export type Unnamed_1_Query = { __typename?: 'Query', me?: (
6056+
{ __typename?: 'User' }
6057+
& UserFragmentFragment
6058+
) | null };
6059+
6060+
export type UserFragmentFragment = { __typename?: 'User', id: string };
6061+
`);
6062+
});
6063+
6064+
it("'inline' yields correct types", async () => {
6065+
const ast = parse(/* GraphQL */ `
6066+
query {
6067+
me {
6068+
...UserFragment
6069+
}
6070+
}
6071+
fragment UserFragment on User {
6072+
id
6073+
}
6074+
`);
6075+
const result = await plugin(
6076+
schema,
6077+
[{ location: 'test-file.ts', document: ast }],
6078+
{ inlineFragmentTypes: 'inline' },
6079+
{ outputFile: '' }
6080+
);
6081+
expect(result.content).toBeSimilarStringTo(`
6082+
export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never; }>;
6083+
6084+
6085+
export type Unnamed_1_Query = { __typename?: 'Query', me?: { __typename?: 'User', id: string } | null };
6086+
6087+
export type UserFragmentFragment = { __typename?: 'User', id: string };
6088+
`);
6089+
});
6090+
6091+
it("'mask' yields correct types", async () => {
6092+
const ast = parse(/* GraphQL */ `
6093+
query {
6094+
me {
6095+
...UserFragment
6096+
}
6097+
}
6098+
fragment UserFragment on User {
6099+
id
6100+
}
6101+
`);
6102+
const result = await plugin(
6103+
schema,
6104+
[{ location: 'test-file.ts', document: ast }],
6105+
{ inlineFragmentTypes: 'mask' },
6106+
{ outputFile: '' }
6107+
);
6108+
expect(result.content).toBeSimilarStringTo(`
6109+
export type Unnamed_1_QueryVariables = Exact<{ [key: string]: never; }>;
6110+
6111+
6112+
export type Unnamed_1_Query = { __typename?: 'Query', me?: (
6113+
{ __typename?: 'User' }
6114+
& { ' $fragmentRefs': { 'UserFragmentFragment': UserFragmentFragment } }
6115+
) | null };
6116+
6117+
export type UserFragmentFragment = { __typename?: 'User', id: string } & { ' $fragmentName': 'UserFragmentFragment' };
6118+
`);
6119+
});
6120+
});
60326121
});

0 commit comments

Comments
 (0)