diff --git a/CHANGELOG.md b/CHANGELOG.md index 130ccd4450b..91faabb8a12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Expect active development and potentially significant breaking changes in the `0 - Fix bug where there could be store inconsistencies for two dependent optimistic updates [PR #1144](https://github.com/apollostack/apollo-client/pull/1144) - Allow optional mutation arguments. [PR #1174](https://github.com/apollostack/apollo-client/pull/1174) - Fix typings error with `strictNullChecks` [PR #1188](https://github.com/apollostack/apollo-client/pull/1188) +- Gracefully handle `null` GraphQL errors. [PR #1208](https://github.com/apollostack/apollo-client/pull/1208) - Allow optional mutation arguments. [PR #1174](https://github.com/apollostack/apollo-client/pull/1174) ### 0.7.3 diff --git a/src/errors/ApolloError.ts b/src/errors/ApolloError.ts index 7544a414969..4cf937810fb 100644 --- a/src/errors/ApolloError.ts +++ b/src/errors/ApolloError.ts @@ -16,7 +16,8 @@ import { GraphQLError } from 'graphql'; // If we have GraphQL errors present, add that to the error message. if (Array.isArray(err.graphQLErrors) && err.graphQLErrors.length !== 0) { err.graphQLErrors.forEach((graphQLError: GraphQLError) => { - message += 'GraphQL error: ' + graphQLError.message + '\n'; + const errorMessage = graphQLError ? graphQLError.message : 'Error message not found.'; + message += `GraphQL error: ${errorMessage}\n`; }); } diff --git a/test/QueryManager.ts b/test/QueryManager.ts index 545a18803d1..796692d85f4 100644 --- a/test/QueryManager.ts +++ b/test/QueryManager.ts @@ -414,6 +414,36 @@ describe('QueryManager', () => { }); }); + // Easy to get into this state if you write an incorrect `formatError` + // function with graphql-server or express-graphql + it('error array with nulls (handle non-spec-compliant server) #1185', (done) => { + assertWithObserver({ + done, + query: gql` + query people { + allPeople(first: 1) { + people { + name + } + } + }`, + result: { + errors: [null], + }, + observer: { + next() { + done(new Error('Should not fire next for an error')); + }, + error(error) { + assert.deepEqual((error as any).graphQLErrors, [null]); + assert.equal(error.message, 'GraphQL error: Error message not found.'); + done(); + }, + }, + }); + }); + + it('handles network errors', (done) => { assertWithObserver({ done,