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

Gateway over-merging fields of unioned types #3581

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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: 20 additions & 3 deletions packages/apollo-gateway/src/FieldSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,25 @@ function groupBy<T, U>(keyFunction: (element: T) => U) {
};
}

export const groupByResponseName = groupBy<Field, string>(field =>
getResponseName(field.fieldNode),
// 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 groupByParentType = groupBy<Field, GraphQLCompositeType>(
Expand All @@ -81,7 +98,7 @@ export function selectionSetFromFieldSet(
selections: Array.from(groupByParentType(fields)).flatMap(
([typeCondition, fieldsByParentType]: [GraphQLCompositeType, FieldSet]) =>
wrapInInlineFragmentIfNeeded(
Array.from(groupByResponseName(fieldsByParentType).values()).map(
Array.from(groupByParentTypeAndResponseName(fieldsByParentType).values()).map(
fieldsByResponseName => {
return combineFields(fieldsByResponseName)
.fieldNode;
Expand Down
79 changes: 79 additions & 0 deletions packages/apollo-gateway/src/__tests__/integration/unions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import gql from 'graphql-tag';
import { astSerializer, queryPlanSerializer } from '../../snapshotSerializers';
import { execute } from '../execution-utils';

expect.addSnapshotSerializer(astSerializer);
expect.addSnapshotSerializer(queryPlanSerializer);

it('handles multiple union type conditions that share a response name (media)', async () => {
const query = gql`
query {
content {
...Audio
... on Video {
media {
aspectRatio
}
}
}
}
fragment Audio on Audio {
media {
url
}
}
`;

const { queryPlan, errors } = await execute(
[
{
name: 'contentService',
typeDefs: gql`
extend type Query {
content: Content
}
union Content = Audio | Video
type Audio {
media: AudioURL
}
type AudioURL {
url: String
}
type Video {
media: VideoAspectRatio
}
type VideoAspectRatio {
aspectRatio: String
}
`,
resolvers: {
Query: {},
},
},
],
{ query },
);

expect(errors).toBeUndefined();
expect(queryPlan).toMatchInlineSnapshot(`
QueryPlan {
Fetch(service: "contentService") {
{
content {
__typename
... on Audio {
media {
url
}
}
... on Video {
media {
aspectRatio
}
}
}
}
},
}
`);
});
4 changes: 2 additions & 2 deletions packages/apollo-gateway/src/buildQueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
Field,
FieldSet,
groupByParentType,
groupByResponseName,
groupByParentTypeAndResponseName,
matchesField,
selectionSetFromFieldSet,
Scope,
Expand Down Expand Up @@ -373,7 +373,7 @@ function splitFields(
fields: FieldSet,
groupForField: (field: Field<GraphQLObjectType>) => FetchGroup,
) {
for (const fieldsForResponseName of groupByResponseName(fields).values()) {
for (const fieldsForResponseName of groupByParentTypeAndResponseName(fields).values()) {
for (const [parentType, fieldsForParentType] of groupByParentType(
fieldsForResponseName,
)) {
Expand Down