React Apollo客户端不发送cookie [英] React Apollo Client not sending cookies

查看:152
本文介绍了React Apollo客户端不发送cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试按照Apollo Client上的说明与graphql请求一起发送cookie,但是快递服务器没有收到任何cookie,当我检查该请求时,它表明没有随响应发送任何cookie。



此页之后:



https://www.apollographql.com/docs/react/recipes/authentication.html



我的代码是

  const link = createHttpLink({
uri:'http:// localhost:3000 / api / graphql',
选择:{
凭据:'include',
}
});

const client = new ApolloClient({
link,
cache:new InMemoryCache()
});

我的设置非常简单,我只是在localhost:3000上使用create-react-app服务器,将对api的请求发送到localhost:5000(express api服务器)。我可以在其他路由上通过localhost:3000设置和检索cookie,只有Apollo Client不会发送它们。

解决方案

我不可以理解为什么文档说明了它的作用,尽管也许经验丰富的开发人员不会遇到问题。



无论如何,经验不足的开发人员应该注意,而不是:

  const link = createHttpLink({
uri:'http:// localhost:3000 / api / graphql' ,
选择:{
凭据: include,
}
});

应该只是:

  const httpLink = createHttpLink({
uri:'http:// localhost:3000 / api / graphql',
凭据:'same-origin'
} );

换句话说,没有单独的 opts 在createHttpLink配置对象中的对象。


I have tried following the instructions on Apollo Client for sending cookies along with the graphql request, but the express server is not receiving any cookies and when I inspect the request it shows that no cookies are being sent with the response.

Following this page:

https://www.apollographql.com/docs/react/recipes/authentication.html

My code is

const link = createHttpLink({
  uri: 'http://localhost:3000/api/graphql',
  opts: {
    credentials: 'include',
  }
});

const client = new ApolloClient({
  link,
  cache: new InMemoryCache()
});

My setup is very simple, I am just using the create-react-app server on localhost:3000, sending requests on for the api to localhost:5000 (express api server). I can set and retrieve cookies via localhost:3000 on other routes, only Apollo Client is not sending them.

解决方案

I do not understand this why the documentation says what it does, although perhaps a developer with more experience would not have had a problem.

Anyway, the less experienced developer should note that instead of:

const link = createHttpLink({
  uri: 'http://localhost:3000/api/graphql',
  opts: {
    credentials: 'include',
  }
});

It should just be:

const httpLink = createHttpLink({
  uri: 'http://localhost:3000/api/graphql',
  credentials: 'same-origin'
});

In other words, there is no separate opts object within the createHttpLink configuration object.

这篇关于React Apollo客户端不发送cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆