Skip to content

Commit

Permalink
enhance(delegate): simplify wrapConcreteTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed May 2, 2024
1 parent d49a5b5 commit 4ce3ffc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-donuts-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-tools/delegate": patch
---

Simplify the logic in `wrapConcreteTypes`
48 changes: 22 additions & 26 deletions packages/delegate/src/prepareGatewayDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
isAbstractType,
isCompositeType,
isInterfaceType,
isNonNullType,
isUnionType,
Kind,
SelectionNode,
SelectionSetNode,
Expand Down Expand Up @@ -444,32 +442,30 @@ function wrapConcreteTypes(
}
},
[Kind.FIELD]: (node: FieldNode) => {
let type = typeInfo.getType();
type = isNonNullType(type) ? type.ofType : type;
if (
type != null &&
isAbstractType(getNamedType(type)) &&
(!isUnionType(type) || type.name === '_Entity') // unnecessary spread on union types, except for federation's "_Entity" (https://www.apollographql.com/docs/federation/subgraph-spec/#union-_entity)
) {
return {
...node,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: namedType.name,
const fieldType = typeInfo.getType();
if (fieldType) {
const fieldNamedType = getNamedType(fieldType);
if (isAbstractType(fieldNamedType) && fieldNamedType.name !== namedType.name) {
return {
...node,
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: namedType.name,
},
},
selectionSet: node.selectionSet,
},
selectionSet: node.selectionSet,
},
],
},
};
],
},
};
}
}
},
}),
Expand Down

0 comments on commit 4ce3ffc

Please sign in to comment.