apollo-client: Request cookies not being passed when `same-origin` is set.
Intended outcome:
Want request cookies to be sent with query when same-origin
is set.
Actual outcome: I followed the new docs for Authentication but the Request Cookies are not being passed.
This worked before upgrading and I haven’t changed any code besides what was in the migration guide.
Code from 1.0.1 (worked fine, Request cookie is passed and came back with data)
import { ApolloProvider } from 'react-apollo'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
const networkInterface = createNetworkInterface({
uri: '/graphql',
opts: {
credentials: 'same-origin',
},
});
const client = new ApolloClient({
networkInterface,
});
const Root = () => {
return (
<ApolloProvider client={ client }>
<Header />
</ApolloProvider>
);
};
ReactDOM.render(<Root />, document.querySelector('#root'));
Network tab:
Updated code to reflect 2.0.1 API:
import ApolloClient from 'apollo-client'
import { ApolloProvider } from 'react-apollo'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import Header from './components/Header'
const link = createHttpLink({
uri: '/graphql',
opts: {
credentials: 'same-origin',
},
});
const client = new ApolloClient({
cache: new InMemoryCache(),
link
});
const Root = () => {
return (
<ApolloProvider client={ client }>
<Header />
</ApolloProvider>
)
}
ReactDOM.render(<Root />, document.querySelector('#root'));
Network Tab:
How to reproduce the issue:
Upgrade and use createHttpLink
with opts: { credentials: ‘same-origion’ } and see no Request cookie passed. I’m using express-session
on the backend, and have tried using cors
as mentioned in the docs but to no avail.
I’m not sure how to better debug but would love to know how so I can help fix it (if it is a problem). I could be messing something up but only upgrade-to-2.0 code changed so it seems suspect.
Version
- apollo-client@2.0.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 31 (3 by maintainers)
I moved to credentials: ‘include’
same-origin will check scheme, hostname and port.
After switching to ‘include’ I also had to fix up my CORS.
In some docs. it says to use HttpLink :
import { HttpLink } from 'apollo-link-http'
; and in some other part of the docs (in migration) it says to use createHttpLink :import { createHttpLink } from 'apollo-link-http'
. I tried both but the request cookies are still not passing.Here is what I have.
Decided to look at the source code to see whats wrong, the documentation is incorrect for passing options.
You don’t need to pass the credentials in an
opts
object, from https://www.apollographql.com/docs/react/recipes/authentication.html#CookieShould actually read:
If you are here in 2020 With next JS => https://github.com/apollographql/apollo-client/issues/5089
For me it was a server side issue and nothing to do with the Apollo. Once I set credentials to ‘include’ I updated my CORS policy and whitelisted the client uri where Apollo is used. If you are using Django as a I am lmk and I can walk you through it
any update on this? I am using every possible credentails configs and I still cannot send cookies. Doesn’t matter whether I use apollo-client or apollo-boost
@mnmkng neither with
apollo-boost
to me! How to fix? I cannot request with cookies, no one is sent!See https://github.com/apollographql/apollo-link/pull/364