graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

cookie support

Open DxCx opened this issue 9 years ago • 7 comments

hey,

i am trying to use couple of GraphQLClients in a script used to send graphql requests with different login types. however, the GraphQLClient not supporting cookies or returning back the headers of the original request. what do you think about adding cookies support (with isolated store) per client?

DxCx avatar Jun 28 '17 10:06 DxCx

Your problem might be caused by not including credentials: same-origin in your request header (https://github.com/github/fetch/issues/386).

This should now be possible with the release of 1.3.2: https://github.com/graphcool/graphql-request/pull/9

Like:

new GraphQLClient(YOUR_ENDPOINT_URL, {
    credentials: "same-origin",
});

mwld avatar Jul 19 '17 10:07 mwld

hey @mwld is that actually working for you? from what i've found isomrophic-fetch will just ignore cookies (or set-cookie header) i had to write temporary wrapper to support cookies.. :\

DxCx avatar Jul 19 '17 13:07 DxCx

Yes this is working for me :)

mwld avatar Jul 19 '17 13:07 mwld

yeah well i forgot to mention that i'm using the library from node and not browser.

DxCx avatar Jul 19 '17 13:07 DxCx

You made me dig again with your replay. i was able to replace my ugly big wrapper with just this line:

global['fetch'] = require('fetch-cookie/node-fetch')(require('node-fetch'))

Now graphql-request just works for node as well. @schickling i think this is worth mentioning in README.MD would you like me to open a PR?

NOTE: this won't solve per client cookie, since fetch is global. if we want to support per GraphQLClient cookie we will need to be able to inject fetch function on client constructor. then we can pass different fetch for each client.

DxCx avatar Jul 19 '17 13:07 DxCx

Thanks a lot for bringing this up @DxCx! It would be great if you could mention it briefly in the readme.

Regarding injecting fetch: Let's keep this issue open to see whether other people also have this problem before we're changing the API. @DxCx can you close this issue and create a new one with this concrete suggestion?

schickling avatar Jul 19 '17 15:07 schickling

the injection is optional, which means, if no fetch is given in option, it will use fetch as it uses it today. but great! i'll update the README

DxCx avatar Jul 19 '17 15:07 DxCx