datasource-rest icon indicating copy to clipboard operation
datasource-rest copied to clipboard

[apollo-datasource-rest] Handling 404 errors

Open timReynolds opened this issue 5 years ago • 2 comments

From my understanding of GraphQL data that is not available as part of a request should be returned as null, therefore allowing other parts of the query to return for the client to render as best.

If this is the case and your rest API returns 404 status codes however you'll need to write something like the following to capture the errors and return null;

      try {
        return await dataSources.listingApi.getListing(args.reference);
      } catch (err) {
        
        if(err.extensions.response.status === 404) {
          return null;
        }
        throw err;
      }

This works but isn't particularly elegant for what seems to be a common use case. Have I misunderstood how to deal with 404 errors in GraphQL or is this part of the rest datasource that could have better handling around it?

I'd be interested to know how other people are dealing with this in a more elegant manor or at least encapsulating the null return inside the class which extends RESTDataSource. From skimming the code this didn't look easy to handle without rewriting some of the protected methods.

timReynolds avatar Oct 18 '18 19:10 timReynolds