Skip to content

Commit

Permalink
fix: error when server throws and we are also querying for a local fi…
Browse files Browse the repository at this point in the history
…eld (#10362)

* fix: error when server returns an error and we are also querying for a local field

closes #10345

* Apply suggestions from code review

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>

---------

Co-authored-by: Alessia Bellisario <alessia@apollographql.com>
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
3 people committed Feb 2, 2023
1 parent 8ac151e commit 14a56b1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-lemons-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix error when server returns an error and we are also querying for a local field
40 changes: 40 additions & 0 deletions src/__tests__/local-state/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
});
});
});
4 changes: 4 additions & 0 deletions src/core/LocalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ export class LocalState<TCacheShape> {
rootValue: any,
execContext: ExecContext,
): Promise<any> {
if (!rootValue) {
return null;
}

const { variables } = execContext;
const fieldName = field.name.value;
const aliasedFieldName = resultKeyNameFromField(field);
Expand Down

0 comments on commit 14a56b1

Please sign in to comment.