0
votes

I'm unable to get any network error with my Apollo Client, only GraphQL errors.

I have the client set up like so:

    const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
    );
  if (networkError) console.log(`[Network error]: ${networkError}`);
});

  const client = new ApolloClient({
    link: ApolloLink.from([
      errorLink,
      new HttpLink({
        uri: 'http://localhost:4000/graphql'
      }),
    ]),
  });

On the server i'm making a request which i parse like this:

 .then(res => {
  if (res.ok) {
    return { blah: res.json(), count };
  }
  throw new Error();
})
.catch(error => {
  throw new AuthenticationError('Must Authenticate');
});

The AuthenicationError just bubbles up to the Client as a GraphQL Error ([GraphQL error]: Message: Must Authenticate,, i basically just want to be able to get the HTTP Status Code from the server request, on the client.

Thanks

1

1 Answers

0
votes

Your client side implementation looks correct.

I guess you are using express with some graphl middleware to handle the requests. The main thing is that you would need to handle the authentication process before the graphql middleware comes in to play.

So the authentication is a graphql mutation which you handle directly. In my case it looked kind of like this:

...

const app = express();

app.use(
  "/graphql",
  bodyParser.json(),
  (request, response, next) => authenticationMiddleware(request) // authentication is handled before graphql
  .then(() => next())
  .catch(error => next(error)),
  graphqlExpress(request => {
    return {
      schema: executable.schema,
      context: {
        viewer: request.headers.viewer
      },
    };
  }),
);

...

app.listen(...)

Another possibility would be to add the status code to the error response in the graphql errors. But that depends on the npm package you are using. e.g. formatError in apollo-server-graphql https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#constructor-options-lt-ApolloServer-gt