尝试使用k6通过WebSocket连接到GraphQL API时收到错误消息

rlcwz9us  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(156)

我将使用k6对使用Hasura的graphql API进行订阅测试,以下是我所做的尝试:

import ws from "k6/ws";
import { check } from "k6";

export const myFunc = (url, access_token, id, query) => {
  const headers = {
    Authorization: access_token,
  };

  const res = ws.connect(url, { headers }, function (socket) {
    socket.on("open", function open() {
      console.log(`VU ${__VU}: connected`);

      socket.send(
        JSON.stringify({
          type: "connection_init",
          payload: headers,
        })
      );
      console.log("sending query");
      socket.send(
        JSON.stringify({
          type: "start",
          payload: {
            query: query,
            variables: {
              id,
            }
          },
        })
      );
    });
...

    socket.on("message", function (msg) {
      console.log(`VU ${__VU}: received message: ${msg}`);
      const message = JSON.parse(msg);
      if (message.type == "connection_ack")
        console.log("Connection Established with WebSocket");
      if (message.type == "data") console.log(`Message Received: ${message}`);
    });

...
};

以及出错日志:

INFO[0001] VU 1: connected                               source=console
INFO[0001] sending query                                 source=console
INFO[0001] VU 1: received message: {"type":"ka"}         source=console
INFO[0001] VU 1: received message: {"type":"connection_ack"}  source=console
INFO[0001] Connection Established with WebSocket         source=console
INFO[0001] VU 1: received message: {"type":"ka"}         source=console
INFO[0001] VU 1: received message: {"type":"connection_error","payload":"parsing ClientMessage failed: Error in $: When parsing the record StartMsg of type Hasura.GraphQL.Transport.WebSocket.Protocol.StartMsg the key id was not present."}  source=console

为什么我收到密钥ID不存在错误?我不知道这意味着什么,当我搜索它时找不到任何东西。

3htmauhk

3htmauhk1#

当你通过已建立的WebSocket连接发送消息时,协议规定你也需要发送id
有效载荷示例:

{
  "id": "1",
  "type":"start",
  "payload": {
    "variables":{},
    "query":"query MyQuery {\n  test {\n    id\n  }\n}\n"
  }
}

id可以是客户端决定的任何字符串。当服务器发回响应时,它将包含相同的ID。对于parsing ClientMessage failed错误消息,Hasura抱怨它无法找到id

相关问题