graphql-schema-generator-rest icon indicating copy to clipboard operation
graphql-schema-generator-rest copied to clipboard

`errors` mapper for server response

Open jhen0409 opened this issue 6 years ago • 4 comments

I've realized I have no way to resolve error of server response, looks like this line ignored server error and did nothing. I expected that can be resolved to errors of GraphQL response, is this lib have any plans to support or PR welcome? 😃

jhen0409 avatar May 14 '18 07:05 jhen0409

@jhen0409 Sure feel free to pitch your idea/file in a pull request. Error handling is important!

n1ru4l avatar May 14 '18 08:05 n1ru4l

Hi,

I can’t speak for Laurin but I would welcome a PR that stops ignoring errors. In the meantime, you can provide a custom fetcher to generateRestSchema. That custom fetcher could be something like:

const fetcher = (...args) => fetch(...args).then((response) => {
  if (!response.ok) {
    throw new Error('Network Error');
  }
  return response;
});

The thrown error will appear in the GraphQL response.

fodier avatar May 14 '18 08:05 fodier

@jhen0409 @fodier We could call response.json()either way, however some API do not include a response body when sending a 500 or 404 error (https://github.com/n1ru4l/graphql-schema-generator-rest/issues/32).

We could also change the behaviour of responseMapper completely. When not declared everyting would behave like the current implementation. Otherwise the responseMapperfunction receives the raw response object returned by fetch and the user has to determine whether it is an error by returning a Promise that either resolves or rejects.

n1ru4l avatar May 14 '18 08:05 n1ru4l

@jhen0409 @fodier what do you think?

n1ru4l avatar Jun 01 '18 10:06 n1ru4l