Skip to content

Commit

Permalink
fix __typename not selected if already selected with alias (#5963)
Browse files Browse the repository at this point in the history
* fix __typename not selected if already selected with alias

* changeset

* fix unit tests
  • Loading branch information
EmrysMyrddin committed Mar 11, 2024
1 parent 4468fb8 commit 8199416
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-buckets-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/wrap": patch
---

Fix missing `__typename` field when it is already present but aliased.
42 changes: 24 additions & 18 deletions packages/wrap/src/transforms/TransformCompositeFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,25 @@ export default class TransformCompositeFields<TContext = Record<string, any>>

const parentTypeName = parentType.name;
let newSelections: Array<SelectionNode> = [];
let typeNameExists = node.selections.some(
selection => selection.kind === Kind.FIELD && selection.name.value === '__typename',
);
let isTypenameSelected = false;

for (const selection of node.selections) {
if (selection.kind !== Kind.FIELD || selection.name.value === '__typename') {
if (selection.kind !== Kind.FIELD) {
newSelections.push(selection);
continue;
}

const newName = selection.name.value;

// See https://github.com/ardatan/graphql-tools/issues/2282
// The `__typename` selection should not be aliased
// to be accessible with this name
if (
!typeNameExists &&
(this.dataTransformer != null || this.errorsTransformer != null) &&
(this.subscriptionTypeName == null || parentTypeName !== this.subscriptionTypeName)
selection.name.value === '__typename' &&
(!selection.alias || selection.alias.value === '__typename')
) {
newSelections.push({
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: '__typename',
},
});
typeNameExists = true;
isTypenameSelected = true;
}

const newName = selection.name.value;

let transformedSelection: Maybe<SelectionNode | Array<SelectionNode>>;
if (this.fieldNodeTransformer == null) {
transformedSelection = selection;
Expand Down Expand Up @@ -228,6 +219,21 @@ export default class TransformCompositeFields<TContext = Record<string, any>>
});
}

// See https://github.com/ardatan/graphql-tools/issues/2282
if (
!isTypenameSelected &&
(this.dataTransformer != null || this.errorsTransformer != null) &&
(this.subscriptionTypeName == null || parentTypeName !== this.subscriptionTypeName)
) {
newSelections.push({
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: '__typename',
},
});
}

return {
...node,
selections: newSelections,
Expand Down
6 changes: 3 additions & 3 deletions packages/wrap/tests/transformTransformCompositeFields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ describe('TransformCompositeFields', () => {
2,
expect.objectContaining({
selections: [
expect.objectContaining({
name: expect.objectContaining({ kind: 'Name', value: '__typename' }),
}),
expect.objectContaining({
name: expect.objectContaining({ kind: 'Name', value: 'product' }),
selectionSet: expect.objectContaining({
Expand All @@ -141,6 +138,9 @@ describe('TransformCompositeFields', () => {
],
}),
}),
expect.objectContaining({
name: expect.objectContaining({ kind: 'Name', value: '__typename' }),
}),
],
}),
);
Expand Down

0 comments on commit 8199416

Please sign in to comment.