【问题标题】:Possible to post to Apollo server with protobuf?可以使用 protobuf 发布到 Apollo 服务器吗?
【发布时间】:2020-11-12 01:46:56
【问题描述】:

我正在开发一个项目,该项目目前使用 protobuf 从客户端与 api 进行通信。我想做一个概念证明,显示通过 graphql 进行的通信,并且想知道是否可以将 protobuf 发送到 apollo 服务器而不是 json。

还询问了阿波罗服务器: https://spectrum.chat/apollo/apollo-server/is-there-support-in-the-road-map-at-all-for-sending-protobuf-from-the-client~dbdbc639-d7c7-4d0f-9caa-b58bb3744a90

我看到一些 protobuf 相关的包, https://www.npmjs.com/package/apollo-engine-reporting-protobuf https://www.npmjs.com/package/@apollo/protobufjs

我想知道是否有任何计划或是否有可能。在这里问是因为我在社区网站上没有收到任何回复。

【问题讨论】:

    标签: graphql protocol-buffers apollo apollo-server protobuf.js


    【解决方案1】:

    如果你可以使用apollo-server-express,只要你能在服务器上再次将protobuf转换为json,你就可以实现,我想protobuf.js可以做到。

    apollo-serverapollo-server-express 需要请求正文中的三个密钥对值

    1. 操作名称

    2. 变量

    3. 查询

    这是请求正文的样子(使用 morgan-body 记录)

    如果你使用apollo-server-express,你可以使用你的自定义express middleware,你可以在其中将protobuf反序列化为json,并将json添加到req.body用于Apollo Server。

    apollo-server 更改为 apollo-server-express 只需几个步骤,并且您的 apollo 服务器配置没有任何变化

    // import it from apollo-server-express instead of apollo-server
    const { ApolloServer } = require('apollo-server-express');
    const express = require('express');
    const app = express();
    
    app.use((req, res, next) => {
        // Intercept the request before it reaches the Apollo Server
        // Use protobuf.js to deserialize the request body into json
        // Add the json to req.body
        // Call the next() to go to next middleware
        next();
    });
    
    const server = new ApolloServer({/* Your apollo server config */})
    
    server.applyMiddleware({ app });
    app.listen({ port: 4000 }, () => console.log(`? Server ready at http://localhost:4000${server.graphqlPath}`));
    

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多