Skip to content

Commit

Permalink
Fix issues with missing sub-fragments when skipTypename: true (#6161)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jun 20, 2021
1 parent 7f152fc commit 6aaecf1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/gold-ties-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-operations': patch
'@graphql-codegen/flow-operations': patch
---

Fix issues with missing sub-fragments when skipTypename: true
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
}
} else {
// it must be an interface type that is spread on an interface field

for (const possibleType of possibleTypes) {
if (!node.typeCondition) {
throw new Error('Invalid state. Expected type condition for interface spread on a interface field.');
Expand Down Expand Up @@ -497,13 +496,16 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
const subTypes: { name: string; content: string }[] = Object.keys(grouped)
.map(typeName => {
const possibleFields = grouped[typeName].filter(Boolean);
const declarationName = this.buildFragmentTypeName(fragmentName, fragmentSuffix, typeName);

if (possibleFields.length === 0) {
if (!this._config.addTypename) {
return { name: declarationName, content: '{}' };
}

return null;
}

const declarationName = this.buildFragmentTypeName(fragmentName, fragmentSuffix, typeName);

return { name: declarationName, content: possibleFields.join(' & ') };
})
.filter(Boolean);
Expand Down
58 changes: 58 additions & 0 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4701,6 +4701,64 @@ function test(q: GetEntityBrandDataQuery): void {
`);
});

it('#3950 - Invalid output with fragments and skipTypename: true', async () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
animals: [Animal!]!
}
interface Animal {
id: ID!
}
type Duck implements Animal {
id: ID!
}
type Lion implements Animal {
id: ID!
}
type Puma implements Animal {
id: ID!
}
type Wolf implements Animal {
id: ID!
}
`);

const query = parse(/* GraphQL */ `
fragment CatFragment on Animal {
... on Lion {
id
}
... on Puma {
id
}
}
query kitty {
animals {
...CatFragment
}
}
`);

const { content } = await plugin(
schema,
[{ location: '', document: query }],
{
skipTypename: true,
},
{
outputFile: 'graphql.ts',
}
);

expect(content).toContain('type CatFragment_Duck_Fragment = {}');
expect(content).toContain('type CatFragment_Wolf_Fragment = {}');
expect(content).toContain(
'export type KittyQuery = { animals: Array<CatFragment_Duck_Fragment | CatFragment_Lion_Fragment | CatFragment_Puma_Fragment | CatFragment_Wolf_Fragment> };'
);
});

it('#2489 - Union that only covers one possible type with selection set and no typename', async () => {
const schema = buildSchema(/* GraphQL */ `
type NotFoundError {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,12 @@
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*", "@types/node@14.17.3", "@types/node@^14.14.33":
"@types/node@*", "@types/node@^15.0.1":
version "15.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.6.0.tgz#f0ddca5a61e52627c9dcb771a6039d44694597bc"
integrity sha512-gCYSfQpy+LYhOFTKAeE8BkyGqaxmlFxe+n4DKM6DR0wzw/HISUE/hAmkC/KT8Sw5PCJblqg062b3z9gucv3k0A==

"@types/node@14.17.3", "@types/node@^14.14.33":
version "14.17.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.3.tgz#6d327abaa4be34a74e421ed6409a0ae2f47f4c3d"
integrity sha512-e6ZowgGJmTuXa3GyaPbTGxX17tnThl2aSSizrFthQ7m9uLGZBXiGhgE55cjRZTF5kjZvYn9EOPOMljdjwbflxw==
Expand All @@ -3267,11 +3272,6 @@
resolved "https://registry.npmjs.org/@types/node/-/node-12.12.55.tgz#0aa266441cb9e1fd3e415a8f619cb7d776667cdd"
integrity sha512-Vd6xQUVvPCTm7Nx1N7XHcpX6t047ltm7TgcsOr4gFHjeYgwZevo+V7I1lfzHnj5BT5frztZ42+RTG4MwYw63dw==

"@types/node@^15.0.1":
version "15.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.6.0.tgz#f0ddca5a61e52627c9dcb771a6039d44694597bc"
integrity sha512-gCYSfQpy+LYhOFTKAeE8BkyGqaxmlFxe+n4DKM6DR0wzw/HISUE/hAmkC/KT8Sw5PCJblqg062b3z9gucv3k0A==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -13563,12 +13563,12 @@ rxjs@^6.3.3, rxjs@^6.4.0, rxjs@^6.5.1, rxjs@^6.6.0, rxjs@^6.6.3, rxjs@^6.6.7:
dependencies:
tslib "^1.9.0"

safe-buffer@5.1.2, safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==

safe-buffer@^5.2.0, safe-buffer@~5.2.0:
safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down

0 comments on commit 6aaecf1

Please sign in to comment.