简体   繁体   English

如何在 NodeJS graphql-apollo 服务器和 Python 项目之间进行通信?

[英]How to communicate between a NodeJS graphql-apollo server and a Python project?

Since this stack is new to me I'd figure it wouldnt hurt to ask the community for input.由于这个堆栈对我来说是新的,我认为向社区征求意见不会有什么坏处。

I'm exploring a new type of project and have been brainstorming how/ if it's possible to communicate between an apollo-graphql server (NodeJS) and a client Python program.我正在探索一种新型项目,并且一直在集思广益如何/是否可以在 apollo-graphql 服务器 (NodeJS) 和客户端 Python 程序之间进行通信。 I have a web development background and I intend to keep my app and server in Typescript/Node.我有 web 开发背景,我打算将我的应用程序和服务器保留在 Typescript/Node.js 中。

However...然而...

As a project and exercise, I would like to play around with connecting a watering system via the GPIO package in Python + RaspberryPi and then send the updates to my apollo-graphql server.作为一个项目和练习,我想尝试通过 Python + RaspberryPi 中的 GPIO package 连接浇水系统,然后将更新发送到我的 apollo-graphql 服务器。 I think conceptually, in a traditional RESTful api it would be straightforward... However, with GraphQL things are a bit more nebulous to me.我认为从概念上讲,在传统的 RESTful api 中它会很简单......但是,对于 GraphQL 来说,事情对我来说有点模糊。 It would be nice to build out hardware connected services in various languages as appropriate and not be bottled into having everything in typescript/node and to that end Python is the most widely used language within the RaspberryPi community.最好用各种语言构建硬件连接服务,而不是把所有东西都装在 typescript/node 中,为此 Python 是 RaspberryPi 社区中使用最广泛的语言。

Has anyone done this before, or something similar, and have insights and experience to share?有没有人以前做过这个或类似的事情,并且有见解和经验可以分享?


edit : since posting I became more familiar with graphql and played around with it in Postman. It simply has an endpoint that I can post to it with queries (and I assume mutations and everything else) and it's captured by my NodeJS apollo-graphQL server.编辑:自发布以来,我对 graphql 变得更加熟悉,并在 Postman 中使用它。它只有一个端点,我可以将查询发布到它(我假设突变和其他一切),它被我的 NodeJS apollo-graphQL 服务器捕获. I ended up using this python package to query against my node API and it seems to work well.我最终使用这个python package来查询我的节点 API,它似乎运行良好。

Your server is node and your client is python. The server doesn't care at all whether your client is python or java or C#, it only wants to see GraphQL requests.你的服务器是node,你的客户端是python。服务器根本不关心你的客户端是python还是java还是C#,它只想看到GraphQL个请求。

I suggest using a python-based GraphQL client package such as python-graphql-client我建议使用基于 python 的 GraphQL 客户端 package 例如python-graphql-client

You're right that in the end everything looks like a POST.你是对的,最后一切看起来都像一个 POST。 Using an actual client might make life a little bit easier for you though.不过,使用实际客户端可能会让您的生活更轻松一些。

You can try calling your GraphQL service using a RESTful wrapper: https://graphql.org/blog/rest-api-graphql-wrapper/ .您可以尝试使用 RESTful 包装器调用 GraphQL 服务: https://graphql.org/blog/rest-api-graphql-wrapper/ There's a similar question asked before: link .之前有一个类似的问题: 链接

Secondly, instead of calling the server directly, you could also try using a message broker in the middle.其次,您也可以尝试在中间使用消息代理,而不是直接调用服务器。 In short: the raspi sends messages to a queue that is polled by your server.简而言之:raspi 将消息发送到由您的服务器轮询的队列。 There are several solutions available like: Rabbitmq .有几种可用的解决方案,例如: Rabbitmq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM