GraphQL 游乐场 - 将 Cookie 作为 Http 标头“消失"发送; [英] GraphQL playground - sending Cookie as Http Header "disappears"

查看:27
本文介绍了GraphQL 游乐场 - 将 Cookie 作为 Http 标头“消失"发送;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 GraphQL Playground 中测试一些实现,我想在其中发送一个特定的 cookie,以便我可以在我的解析器中获取它.我在操场上使用内置的 Http Headers 窗格:

但是,当我添加名为 Cookiecookie 的标头时,当我尝试在我的解析器中 console.log 时它不会显示.所有其他自定义 Http 标头均正常显示.

如上面的屏幕截图所示,出现了测试标头,但没有出现 cookie 标头.我正在使用

当我尝试对 req.cookies 进行 console.log 记录时,我什么也没得到,这是使用 cookieParser 的好处之一.

我的 ApolloServer 实现如下:

const server = new ApolloServer({typeDefs:架构解析器,数据源:() =>({//...}),上下文:({req, res}) =>({楷模,会话:req.session,请求,资源}),//... 其余的不重要});

创建自定义"cookie 标头可以解决问题,例如 somecookie: <key>=<value>,但我认为这不是最佳做法,并且希望避免这种情况.我希望有人知道为什么我的 cookie header 没有出现,或者我可以做些什么让它出现?

解决方案

经过大量搜索、阅读文档等之后,我想出了如何实现这一目标.

在位于窗口右上角的 GraphQL playground 设置(齿轮图标)中:

我将行 "request.credentials" 更改为 "include" 并保存 UI 中的设置.在

I'm testing some implementations in the GraphQL Playground, in which I want to send a specific cookie, so that I can fetch it in my resolver. I'm using the built in Http Headers pane in the playground:

However, when I add headers named either Cookie or cookie, it doesn't show up when I try to console.log it in my resolver. All other custom Http Headers show up with no issues.

As seen in the above screenshoot the testheader appears, but the cookie header doesn't. I'm using cookieParser, which might to blame for the cookie header disappearing, however I'm not sure. Here is a screenshot of my console.log section:

And when I try to console.log the req.cookies, I get nothing, which is to be one of the benefits of using the cookieParser.

My ApolloServer implementation is as follows:

const server = new ApolloServer({
  typeDefs: schema
  resolvers,
  dataSources: () => ({
    // ...
  }),
  context: ({req, res}) => ({
    models,
    session: req.session,
    req,
    res
  }),
  // ... and the rest is not important
});

Creating a "custom" cookie header could do the trick, such as somecookie: <key>=<value>, but I don't think that's the best practice, and would prefer to avoid that. I'm hoping someone out there got an idea why my cookie header doesn't appear, or what I can do for it to appear?

解决方案

After extensive searching, documentation reading and etc. I figured out how I could make this work.

In the GraphQL playground settings (gear icon), located in the upper right corner of the window:

I changed the line "request.credentials" to "include" and SAVING the settings in the UI. Read more here. This line is taken directly from the documentation:

'request.credentials': 'omit', // possible values: 'omit', 'include', 'same-origin'

Then following that, I opened the developer tools window (usually F12), went to the tab Application. In here I simply added a cookie as seen in the screenshot. That cookie was sent together with my request.

这篇关于GraphQL 游乐场 - 将 Cookie 作为 Http 标头“消失"发送;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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