diff --git a/packages/apollo-gateway/CHANGELOG.md b/packages/apollo-gateway/CHANGELOG.md index a7f3d278110..0e3f323d593 100644 --- a/packages/apollo-gateway/CHANGELOG.md +++ b/packages/apollo-gateway/CHANGELOG.md @@ -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 diff --git a/packages/apollo-gateway/src/__tests__/integration/requires.test.ts b/packages/apollo-gateway/src/__tests__/integration/requires.test.ts index 25e940a9a23..1ea07cce2b4 100644 --- a/packages/apollo-gateway/src/__tests__/integration/requires.test.ts +++ b/packages/apollo-gateway/src/__tests__/integration/requires.test.ts @@ -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', + }, + }); +}); diff --git a/packages/apollo-gateway/src/executeQueryPlan.ts b/packages/apollo-gateway/src/executeQueryPlan.ts index b92aec7d61e..5c477c0b353 100644 --- a/packages/apollo-gateway/src/executeQueryPlan.ts +++ b/packages/apollo-gateway/src/executeQueryPlan.ts @@ -389,7 +389,14 @@ async function executeFetch( function executeSelectionSet( source: Record | null, selectionSet: SelectionSetNode, -): Record { +): Record | 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 = Object.create(null); for (const selection of selectionSet.selections) { @@ -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.`); }