Comments (4)
@kowther Sorry for the delay: we are finally returning this project to active maintenance. What you're describing does seem like a bug: RESTDataSource is supposed to memoize these simultaneous identical calls (and in fact if anything it's too aggressive in doing so). (The two-level caching strategy used by RESTDataSource is finally documented.
Unfortunately the repo you linked is no longer available so there's no way to debug this issue. Were you talking about two REST invocations coming from completely separate GraphQL operations/HTTP requests?
from datasource-rest.
I'm going to close this because I don't know how to reproduce it; I recognize that this is our responsibility for leaving RESTDataSource unmaintained for so long. If you're able to restore that repository (even without updating it to the latest version, which has changed a fair amount) I'd be happy to investigate!
from datasource-rest.
@glasser I made my repository public again so you can check it out
from datasource-rest.
RESTDataSource has two levels of caching (a fact that wasn't documented until very recently).
The first level is an in-memory "deduplication"/memoization cache that is shared only within a single RESTDataSource object (and ignores HTTP response cache headers). This cache allows you to skip a second concurrent fetch of the same URL when the first fetch isn't done.
The second level is shared across RESTDataSource objects (and across processes if you use a shared cache
) (and observes HTTP response cache headers). This one only is written once the fetch is finished.
The Apollo Server v2/v3 dataSources
API runs the dataSources
handler once for each request, so the RESTDataSource objects you're creating in your handler are not shared across requests (this is the expected way to do things). We felt that this was not particularly clear which is why there is no dataSources
API in Apollo Server v4 (you can set up data sources in your context
function which is much more obviously request-specific). That means that the "in progress" cache correctly doesn't trigger.
The second level cache actually does trigger if you run the two requests in series in your shell script, so it's not entirely broken.
So overall I'd say this is working as intended. If you really do want to have cross-request de-duplication of identical requests you could create a single global instance of your RESTDataSource and share it in the context of multiple operations. However this is a very bad idea with the current implementation of RESTDataSource because the de-duplication cache caches all GET responses essentially forever. We are working on a v5 release which changes this to only de-duplicate concurrent requests (#100) and relies on the main header-sensitive cache for de-duplication of requests that don't overlap in time. So in the forthcoming v5 it would be more reasonable to have a long-lived RESTDataSource. (Of course you'd want to be sure that there's nothing user-specific about the requests that are made — by default the cache key for deduplication is just the URL, so if you're setting different headers based on the user context or something you'd want to add that to the cache key.)
from datasource-rest.
Related Issues (20)
- RESTDataSource no longer have access to all datasources through context HOT 2
- 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
- [apollo-datasource-rest] Handling 404 errors HOT 2
- Feature request - be able to tell if API was resolved by memory, cache, or source HOT 3
- RESTDataSource - body based cacheKeyFor POST request HOT 3
- 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
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.