Skip to content

Commit

Permalink
fix(gateway): pass null required fields correctly to resolvers (#4157)
Browse files Browse the repository at this point in the history
This commit fixes a bug in the Apollo Gateway, where certain subfields that
were null in the parent object that is passed to resolvers were getting
expanded into objects that contained all null subfields.
  • Loading branch information
jakeblaxon authored Jul 24, 2020
1 parent c25b278 commit eeaeaf0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/apollo-gateway/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.
- _Nothing yet! Stay tuned!_
- __FIX__: Pass null required fields correctly within the parent object to resolvers. When a composite field was null, it would sometimes be expanded into an object with all null subfields and passed to the resolver. This fix prevents this expansion and sets the field to null, as originally intended.

## v0.18.0

Expand Down
91 changes: 91 additions & 0 deletions packages/apollo-gateway/src/__tests__/integration/requires.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,94 @@ it('collapses nested requires with user-defined fragments', async () => {
expect(queryPlan).toCallService('a');
expect(queryPlan).toCallService('b');
});

it('passes null values correctly', async () => {
const serviceA = {
name: 'a',
typeDefs: gql`
type Query {
user: User
}
type User @key(fields: "id") {
id: ID!
favorite: Color
dislikes: [Color]
}
type Color {
name: String!
}
`,
resolvers: {
Query: {
user() {
return {
id: '1',
favorite: null,
dislikes: [null],
};
},
},
},
};

const serviceB = {
name: 'b',
typeDefs: gql`
extend type User @key(fields: "id") {
id: ID! @external
favorite: Color @external
dislikes: [Color] @external
favoriteColor: String @requires(fields: "favorite { name }")
dislikedColors: String @requires(fields: "dislikes { name }")
}
extend type Color {
name: String! @external
}
`,
resolvers: {
User: {
favoriteColor(user: any) {
if (user.favorite !== null) {
throw Error(
'Favorite color should be null. Instead, got: ' +
JSON.stringify(user.favorite),
);
}
return 'unknown';
},
dislikedColors(user: any) {
const color = user.dislikes[0];
if (color !== null) {
throw Error(
'Disliked colors should be null. Instead, got: ' +
JSON.stringify(user.dislikes),
);
}
return 'unknown';
},
},
},
};

const query = `#graphql
query UserFavorites {
user {
favoriteColor
dislikedColors
}
}
`;

const { data, errors } = await execute({ query }, [serviceA, serviceB]);

expect(errors).toEqual(undefined);
expect(data).toEqual({
user: {
favoriteColor: 'unknown',
dislikedColors: 'unknown',
},
});
});
17 changes: 8 additions & 9 deletions packages/apollo-gateway/src/executeQueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,14 @@ async function executeFetch<TContext>(
function executeSelectionSet(
source: Record<string, any> | null,
selectionSet: SelectionSetNode,
): Record<string, any> {
): Record<string, any> | null {

// If the underlying service has returned null for the parent (source)
// then there is no need to iterate through the parent's selection set
if (source === null) {
return null;
}

const result: Record<string, any> = Object.create(null);

for (const selection of selectionSet.selections) {
Expand All @@ -398,14 +405,6 @@ function executeSelectionSet(
const responseName = getResponseName(selection);
const selectionSet = selection.selectionSet;

// Null is a valid value for a response, provided that the types match.
// Presumably the underlying service has validated that result, so we
// can pass it through here
// Note: undefined is unexpected here due to GraphQL's type coercion / nullability rules
if (source === null) {
result[responseName] = null;
break;
}
if (typeof source[responseName] === 'undefined') {
throw new Error(`Field "${responseName}" was not found in response.`);
}
Expand Down

0 comments on commit eeaeaf0

Please sign in to comment.