Comments (2)
I am also looking for best practices on this...
My current challenge is how to handle this during a server-side render(SSR). In the case of the REST API returning a 404, it causes a 404 error to be thrown, which then has to be handled within a catch statement. I have logic in the Query component for handling this behavior but during a SSR this logic is never hit because it throws exceptions instead.
Your solution is an option but painful like you said.
Another option, which I am not happy with, is setting the errorPolicy="all" on the Query component.
This causes the SSR to succeed and allows the apollo client to cache all the other successful responses. However, it then causes the client to re-issue the request and handle it accordingly, which results in the re-issuing same request the server already performed.
from datasource-rest.
I just ran into this myself, although in my case it was in the context of testing with an intentionally bad id for the API I'm hitting - i.e. expect(doTheThing(badId)).toThrow()
or .toEqual(null)
and the 404 was breaking it.
Here's a less painful version of the original solution, making use of the fact that await
is just a fancy Promise:
return dataSources.listingApi.getListing(args.reference)
.catch(err => {
if(err.extensions.response.status === 404) {
return null;
}
throw err;
}
})
- Note that
return await
is unnecessary in an async function; you can just return the awaited expression. - This will get even shorter if throw expressions are added to ES (currently Stage 2, and available with Babel:
.catch(err => ((err.extensions.response.status === 404) ? null : throw err)
from datasource-rest.
Related Issues (20)
- Document `contextValue` recipe for circular references in context object
- Posting FormData as body (for file uploads) not possible anymore HOT 7
- Forward all cache options to key value cache HOT 6
- Unable to connect to Localhost server HOT 1
- @apollo/datasource-rest willSendRequest param type missing `agent` HOT 7
- Feature request: More control over readResponseAndWriteToCache method HOT 4
- Why the introduction of response.clone() in HTTPCache class? HOT 4
- Refine `Content-Type` negotiation
- Add url as an argument to the RESTDataSource.didEncounterError() function
- Make errorFromResponse parameters optional
- Parse blob data HOT 1
- RequestDeduplicationPolicy is not exported in datasource-rest index.ts
- Fix CacheOptions Related Types HOT 1
- Typescript project build failing due to missing type declarations HOT 1
- Unable to prevent reading from the Cache HOT 3
- Question about cacheOptions HOT 1
- 304 throws if last-modified doesn't match across requests
- HELP: Proper way to set http proxy HOT 1
- "DeprecationWarning: The `punycode` module is deprecated." in Node 22 HOT 2
- JSON body is not parsed when Content-Type contains a charset
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datasource-rest.