Skip to content

Commit

Permalink
enhance/fix(federation): prevent extra queries and merge iterables co…
Browse files Browse the repository at this point in the history
…rrectly
  • Loading branch information
ardatan committed Nov 1, 2024
1 parent 8e897a9 commit 342e044
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/chilled-ads-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-tools/delegate': patch
'@graphql-tools/federation': patch
---

Prevent extra queries to the same subgraph multiple times on the same plan, and merge iterables
correctly
15 changes: 9 additions & 6 deletions packages/delegate/src/extractUnavailableFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
GraphQLField,
GraphQLInterfaceType,
GraphQLNamedOutputType,
GraphQLNamedType,
GraphQLObjectType,
GraphQLSchema,
isAbstractType,
Expand All @@ -17,7 +16,7 @@ import {
SelectionSetNode,
visit,
} from 'graphql';
import { Maybe, memoize4 } from '@graphql-tools/utils';
import { memoize4 } from '@graphql-tools/utils';

export const extractUnavailableFieldsFromSelectionSet = memoize4(
function extractUnavailableFieldsFromSelectionSet(
Expand Down Expand Up @@ -88,11 +87,12 @@ export const extractUnavailableFieldsFromSelectionSet = memoize4(
}
}
} else if (selection.kind === Kind.INLINE_FRAGMENT) {
const subFieldType: Maybe<GraphQLNamedType> = selection.typeCondition
? schema.getType(selection.typeCondition.name.value)
: fieldType;
const subFieldName = selection.typeCondition?.name.value || fieldType.name;
const subFieldType =
(selection.typeCondition && (schema.getType(subFieldName) as GraphQLObjectType)) ||
fieldType;
if (
subFieldType === fieldType ||
subFieldName === fieldType.name ||
((isObjectType(subFieldType) || isInterfaceType(subFieldType)) &&
isAbstractType(fieldType) &&
schema.isSubType(fieldType, subFieldType))
Expand All @@ -114,6 +114,9 @@ export const extractUnavailableFieldsFromSelectionSet = memoize4(
}
} else if (isObjectType(subFieldType) || isInterfaceType(subFieldType)) {
for (const subSelection of selection.selectionSet.selections) {
if (subSelection.kind === Kind.FIELD && subSelection.name.value === '__typename') {
continue;
}
if (shouldAdd(subFieldType, subSelection as FieldNode)) {
unavailableSelections.push(subSelection);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/federation/src/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ function mergeResults(results: unknown[]) {
if (datas.length === 1) {
return makeExternalObject(datas[0], errors);
}
return makeExternalObject(mergeDeep(datas), errors);
return makeExternalObject(mergeDeep(datas, undefined, true, true), errors);
}
if (errors.length) {
if (errors.length === 1) {
Expand Down

0 comments on commit 342e044

Please sign in to comment.