【问题标题】:Use Websocket Client in graphql resolver for data communication Nodejs在graphql解析器中使用Websocket Client进行数据通信Nodejs
【发布时间】:2022-02-01 23:02:09
【问题描述】:

我想连接到 websocket 服务器,当我的 graphql 服务器启动时,在解析器中我想使用连接的 websocket 的 send 和 recv 函数进行数据通信。

简要介绍一下我的后端, 我有一个 python rest 客户端,也有一个 websocket 服务器,我可以通过 websocket 服务器获取单独的产品详细信息和产品列表。

(在 graphql 解析器中,我想收集我的产品数据和库存数据并将它们合并为 UI。由于节点是异步编程,然后所有示例都连接到服务器,因此使用然后阻塞和通信,我不想这样做,我想在解析器中使用连接对象,连接应该完成一次。

import { WebSocketClient } from './base';

const productWebSocket = new WebSocketClient("ws://192.168.0.109:8000/abi-connection");
productWebSocket.connect({reconnect: true});

export { productWebSocket };

现在我将导入这个 productWebSocket 并希望在解析器中使用它。

这个 websocket 和 graphql shi* 不是很流行,但是以这种方式设计这个 shi* 可以提高性能,因为我在 core-apis 中为我的 restapis 和 websocket 服务器使用了实用程序函数。我称之为 maksuDII+ 编程方式。

【问题讨论】:

    标签: node.js websocket graphql


    【解决方案1】:

    我无法用 nodejs 做到这一点,也没有得到任何帮助。所以我用python实现了graphql,并在websocket客户端中获得了更好的控制。

    我在 nodejs 中搜索 ws、websocket 一些其他糟糕的包都没有用

    websocket.on('connect', ws=> {
     websocket.on('message', data => {
      // this shit is a  argument how am i suppose to get this value in a express api end point.
      // search total 5 pages in google got nothing so have to shift to python.
     })
    })
    

    python版本

    from graphql import GraphQLError
    from ..service_base import query
    from app.websocket.product_websocket import product_ws_connection
    from app.websocket.inventory_websocket import inventory_ws_connection
    import json
    from app.utils.super_threads import SuperThreads
    
    
    def get_websocket_data(socket_connection, socket_send_data):
        socket_connection.send(json.dumps(socket_send_data))
        raw_data = socket_connection.recv()
        jsonified_data = json.loads(raw_data)
        return jsonified_data
    
    
    @query.field("productDetails")
    def product_details(*_, baseCode: str):
        
        product_ws = product_ws_connection.get_connection() # connected client, with proper connection to my websocket server
        inventory_ws = inventory_ws_connection.get_connection()
    
        if not product_ws:
            raise GraphQLError("Product Api Down")
        if not inventory_ws:
            raise GraphQLError("Inventory Api Down")
    
        product_ws_data = {
            "operation": "PRODUCT_FETCH",
            "baseCode": baseCode
        }
    
        inventory_ws_data = {
            "operation": "STOCK_FETCH",
            "baseCode": baseCode
        }
    
    
        # super threads here is a diffrent topic, it a wrapper around standard python Threads.
        ws_product_thread = SuperThreads(target=get_websocket_data, args=(product_ws, product_ws_data))
        ws_inventory_thread = SuperThreads(target=get_websocket_data, args=(inventory_ws, inventory_ws_data))
    
        ws_product_thread.start() # asking one of my websocket server for data.
        ws_inventory_thread.start() # same with this thread.
    
    
        product_data_payload = ws_product_thread.join() # i get the what websocket gives me as response
        inventory_data_payload = ws_inventory_thread.join() # needed this type of shit in nodejs could not have it.
    
        if "Fetched" in product_data_payload["abi_response_info"]["message"] and \
            "Fetched" in inventory_data_payload["abi_response_info"]["message"]:
            
            # data filtering here
    
            return product_data
        else:
            raise GraphQLError("Invalid Product Code")
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2014-03-18
      • 2021-03-28
      • 2020-06-23
      • 1970-01-01
      • 2019-03-28
      相关资源
      最近更新 更多