我试图在登录时使用express会话初始化cookie,但它似乎只是在从同一个站点请求时才起作用。
Apollo客户:
const endPoint = createUploadLink({
uri: 'http://localhost:4000/graphql'
})
const client = new ApolloClient({
link: endPoint,
credentials: 'include',
cache: new InMemoryCache(),
})
Apollo服务器:
const corsOptions = {
origin: "http://localhost:3000",
credentials: true,
allowedHeaders: ['Content-Type', 'Authorization']
}
app.use(session({
name: 'foo',
secret: 'bar',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 360000,
sameSite: 'none',
httpOnly: true
}
}))
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
playground: {
settings: {
'request.credentials': 'include'
}
},
cors: corsOptions
});
现在,如果请求是通过graphql游乐场发出的,则会设置cookie,但如果登录请求是从localhost:3000发出的,并且会话已初始化,则不会设置cookie。
求你了,我在这里失踪了。
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache,
fetchOptions: {
credentials: "include",
}
})
它对我有用。。。试试这个