-
Notifications
You must be signed in to change notification settings - Fork 344
apollo-link-http does not respect a GET method override #236
Comments
I'll add it if nobody else wants to. |
@szdc @Outlaw11A @arackaf here is how to do it: const customFetch = (uri, options) => {
const { body, ...newOptions } = options;
const queryString = objectToQuery(JSON.parse(body));
requestedString = uri + queryString;
return fetch(requestedString, newOptions);
};
const link = createHttpLink({
uri: "data",
fetchOptions: { method: "GET" },
fetch: customFetch
}); |
@jbaxleyiii that looks awesome! Just curious from where objectToQuery is coming? Is there a util on npm for that? I imagine it'd be pretty easy to just roll your own if needed - basically map Object.keys onto escapeUriComponnt(`${k}=${obj[k]}`).join("&"); or something similar |
https://www.apollographql.com/docs/link/links/http.html#Sending-GET-requests-custom-fetching @arackaf see the comment:
|
@jbaxleyiii it works! It's amazing! I just have a problem. The querystring created:
contains this: How to fix this? |
That looks like standard URI serialization - don't think you can avoid it - why's it a problem? |
Standard URI? This: |
encodeURIComponent('books(title:"Hello World"){Book{title}}')
|
My query in Apollo is:
and so I have the problem with |
Here's a barebones react app (created using create-react-app) that fetches via GET and works - see |
@johnunclesam obviously const customFetch = (uri, options) => {
const {body, ...newOptions} = options;
const parsedBody = JSON.parse(body);
const command = omitBy(parsedBody, isEmpty);
if (command.variables) {
command.variables = JSON.stringify(command.variables);
}
const requestedString = uri + '?' + queryString.stringify(command);
return fetch(requestedString, newOptions);
}; |
So what is |
This is an old issue that predates useGETForQueries, which was added a bit after this was fixed by #510 |
umm, useGETForQueries seems to not be working. Specifically on this line there seems to be a bug:
should be something like:
|
What makes you say that? How are you calling |
My observation above was from stepping through the code with a debugger, but I'd have to do it over again to reproduce. The code we ended up settling on is this:
|
I guess my question is, if you were passing |
As per the docs, the
fetchOptions
option allows us to set the method of the request toGET
. I'd expect that after doing so, the query would be included as a query string parameter rather than the request body; Chrome throws an error if you try to make a GET request with a body.Intended outcome:
A query string should be constructed if the request method is GET.
Actual outcome:
The request body is used.
How to reproduce the issue:
The text was updated successfully, but these errors were encountered: