apollographql / apollographql/datasource-rest

[apollo-datasource-rest] Handling 404 errors

Open
#203 2 comments 40 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
48
Forks
21
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue concerns RESTDataSource and its protected methods, but names no specific files or tests. Start by tracing how RESTDataSource handles REST 404 responses and how GraphQL null results are expected to behave. Done should be a clearly defined, supported way to handle a 404 as null without rewriting protected methods, with coverage for non-404 errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.