GraphQLWS在建立WebSocket连接之前设置cookie

发布于 2025-02-12 16:11:51 字数 660 浏览 0 评论 0原文

应要求,我正在处理一个刮擦应用程序,该应用程序轻轻刮擦外部网站,该网站使用GraphQL和GraphQl-WS package(因此,在WebSocket上使用GraphQl)。我以前从未使用过任何类型的GraphQl。

我正在尝试访问的网站使用会话cookie来验证用户。现在,我正在尝试在我的Nodejs应用程序中模拟这一点。我只是找不到为开放插座的初始HTTP请求设置cookie的方法。以下是我当前拥有的代码。

代码

import { createClient } from 'graphql-ws';
import WebSocket from 'ws';

const client = createClient({
  url: 'wss://target.io/graphql',
  webSocketImpl: WebSocket,
  on: {
    connected: (socket) => {
      console.log("Connected!");
    },
  },
});

(async () => {
  // Create subscription
})();

本身就可以正常工作,只要我不需要使用任何身份验证的订阅即可。我该如何制作它,以便在建立连接之前设置会话cookie?

Upon request I'm working on a scraping application that lightly scrapes a external website that makes use of graphql and the graphql-ws package (so graphql over a websocket). I've never used any sort of graphql before.

The site I'm trying to gain access to makes use of a session cookie to authenticate users. Now I'm trying to simulate this in my NodeJS application. I just can't find a way of setting a cookie for the initial http request for opening the socket. Below is the code I currently have.

Code

import { createClient } from 'graphql-ws';
import WebSocket from 'ws';

const client = createClient({
  url: 'wss://target.io/graphql',
  webSocketImpl: WebSocket,
  on: {
    connected: (socket) => {
      console.log("Connected!");
    },
  },
});

(async () => {
  // Create subscription
})();

This in itself works fine, as long as I don't need to use any authenticated subscriptions. How can I make it so a session cookie is set before establishing a connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文