Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup for #3581 - gateway over-merging unions #3616

Merged
merged 2 commits into from Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 3 additions & 20 deletions packages/apollo-gateway/src/FieldSet.ts
Expand Up @@ -64,25 +64,8 @@ function groupBy<T, U>(keyFunction: (element: T) => U) {
};
}

// The response name isn't sufficient for determining uniqueness. In the case of
// unions, for example, we can see a response name collision where the parent type
// is different. In this case, these should not be merged (media)!
// query {
// content {
// ... on Audio {
// media {
// url
// }
// }
// ... on Video {
// media {
// aspectRatio
// }
// }
// }
// }
export const groupByParentTypeAndResponseName = groupBy<Field, string>(field =>
`${field.scope.parentType}:${getResponseName(field.fieldNode)}`,
export const groupByResponseName = groupBy<Field, string>(field =>
getResponseName(field.fieldNode)
);

export const groupByParentType = groupBy<Field, GraphQLCompositeType>(
Expand All @@ -98,7 +81,7 @@ export function selectionSetFromFieldSet(
selections: Array.from(groupByParentType(fields)).flatMap(
([typeCondition, fieldsByParentType]: [GraphQLCompositeType, FieldSet]) =>
wrapInInlineFragmentIfNeeded(
Array.from(groupByParentTypeAndResponseName(fieldsByParentType).values()).map(
Array.from(groupByResponseName(fieldsByParentType).values()).map(
fieldsByResponseName => {
return combineFields(fieldsByResponseName)
.fieldNode;
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-gateway/src/buildQueryPlan.ts
Expand Up @@ -31,7 +31,7 @@ import {
Field,
FieldSet,
groupByParentType,
groupByParentTypeAndResponseName,
groupByResponseName,
matchesField,
selectionSetFromFieldSet,
Scope,
Expand Down Expand Up @@ -373,7 +373,7 @@ function splitFields(
fields: FieldSet,
groupForField: (field: Field<GraphQLObjectType>) => FetchGroup,
) {
for (const fieldsForResponseName of groupByParentTypeAndResponseName(fields).values()) {
for (const fieldsForResponseName of groupByResponseName(fields).values()) {
for (const [parentType, fieldsForParentType] of groupByParentType(
fieldsForResponseName,
)) {
Expand Down Expand Up @@ -413,7 +413,7 @@ function splitFields(
scope as Scope<typeof parentType>,
group,
path,
fieldsForResponseName,
fieldsForParentType,
),
);
} else {
Expand Down Expand Up @@ -452,7 +452,7 @@ function splitFields(
field.fieldNode,
);

const fieldsWithRuntimeParentType = fieldsForResponseName.map(field => ({
const fieldsWithRuntimeParentType = fieldsForParentType.map(field => ({
...field,
fieldDef,
}));
Expand Down