diff --git a/.changeset/small-lemons-grow.md b/.changeset/small-lemons-grow.md new file mode 100644 index 00000000000..1fd7e1dc905 --- /dev/null +++ b/.changeset/small-lemons-grow.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Fix error when server returns an error and we are also querying for a local field diff --git a/src/__tests__/local-state/general.ts b/src/__tests__/local-state/general.ts index 0ac52d61aca..2be807a54ea 100644 --- a/src/__tests__/local-state/general.ts +++ b/src/__tests__/local-state/general.ts @@ -1196,4 +1196,44 @@ describe('Combining client and server state/operations', () => { }, }); }); + + itAsync('handles server errors when root data property is null', (resolve, reject) => { + const query = gql` + query GetUser { + user { + firstName @client + lastName + } + } + `; + + const cache = new InMemoryCache(); + const link = new ApolloLink(operation => { + return Observable.of({ + data: null, + errors: [new GraphQLError("something went wrong", { + extensions: { + code: "INTERNAL_SERVER_ERROR" + }, + path: ["user"] + })] + }); + }); + + const client = new ApolloClient({ + cache, + link, + resolvers: {}, + }); + + client.watchQuery({ query }).subscribe({ + error(error) { + expect(error.message).toEqual("something went wrong"); + resolve(); + }, + next() { + reject(); + }, + }); + }); }); diff --git a/src/core/LocalState.ts b/src/core/LocalState.ts index 77be25b4e4d..e9506def330 100644 --- a/src/core/LocalState.ts +++ b/src/core/LocalState.ts @@ -370,6 +370,10 @@ export class LocalState { rootValue: any, execContext: ExecContext, ): Promise { + if (!rootValue) { + return null; + } + const { variables } = execContext; const fieldName = field.name.value; const aliasedFieldName = resultKeyNameFromField(field);