HTTP GET for fetching?
See original GitHub issueIf my understanding were correct, Apollo always uses HTTP POST to fetch the data. Unfortunately it becomes more challenging to cache since many Varnish based solutions cache GET request only (https://www.fastly.com/blog/api-caching-part-i & http://stackoverflow.com/questions/31539043/how-to-cache-post-requests-with-varnish)
public fetchFromRemoteEndpoint({
request,
options,
}: RequestAndOptions): Promise<IResponse> {
return fetch(this._uri, assign({}, this._opts, {
body: JSON.stringify(printRequest(request)),
method: 'POST',
}, options, {
headers: assign({}, {
Accept: '*/*',
'Content-Type': 'application/json',
}, options.headers),
}));
};
Is it possible to change this to HTTP GET? If not, what would be best way to do server side edge caching on Apollo/GraphQL?
Issue Analytics
- State:
- Created 7 years ago
- Comments:23 (16 by maintainers)
Top Results From Across the Web
Using the Fetch API - MDN Web Docs
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses.
Read more >Fetch API – How to Make a GET Request and POST Request ...
What is the Fetch API? ... fetch() is a mechanism that lets you make simple AJAX (Asynchronous JavaScript and XML) calls with JavaScript....
Read more >Fetch - HTTP GET Request Examples | Jason Watmore's Blog
Simple GET request using fetch ... This sends an HTTP GET request to the Reqres api which is a fake online REST api...
Read more >Fetch API (JavaScript)- How to Make GET and POST Requests
The Fetch API is a promise-based interface for fetching resources by making HTTP requests to servers from web browsers. It is similar to...
Read more >Get and Post method using Fetch API - GeeksforGeeks
The fetch() method is used to send the requests to the server without refreshing the page. It is an alternative to the XMLHttpRequest...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So does basically every authenticated API. That does not eliminate the value or desire to cache responses. Applications using GraphQL would still benefit from ETag caching which could drastically reduce the amount of unchanged data that needs to be transferred across the wire when re-fetching data. There’s some good background on how that technology works in this Heroku Dev Center article.
It should be up to the server to determine if and when resources can be cached by downstream clients (and to opt into performance enhancements like ETag). The server (along with any intermediaries like caching proxies, CDNs, etc) is still responsible for ensuring that caches respect any authentication headers or cookies, and for making sure that query params are respected as part of cache keys.
By using
POST
instead ofGET
for idempotent, read-only requests, you’re merely preventing servers from being able to opt into the performance benefits that caching offers.The simplest change to support this in apollo-client would just be to tweak the network interface to use
GET
if a query (not mutation) is being performed. If you’re primarily concerned about compatibility with existing clients, then you could make this an option. But there’s really no reason for it not to be the default.This section on the GraphQL docs explicitly states that GraphQL HTTP servers should handle HTTP
GET
andPOST
methods.It seems like a good GraphQL client should use
GET
whenever possible in order to convey intent for safe/non-modifying queries, and to enable caching by intermediaries. Ultimately it would be great if servers supported Etags and Cache-Control headers to fully take advantage of this.POST
should only be used if the query is too long for aGET
request (per-browser limits here) or if the request contains mutations.