简体   繁体   English

如何在 GraphQL 服务器上接受来自远程服务器的 POST 请求

[英]How to accept POST request from a remote server on a GraphQL Server

I am working on a project currently using GrapQL.我正在做一个目前使用 GrapQL 的项目。 I am integrating a payment processor, when the user payment is successful, the payment processor sends a POST request to the webhook URL that is meant to point to my server.我正在集成支付处理器,当用户支付成功时,支付处理器向 webhook URL 发送一个 POST 请求,该请求旨在指向我的服务器。 Now I was wondering how to achieve this, considering that GraphQL exposes just one endpoint, in my case /graphql .现在我想知道如何实现这一点,考虑到 GraphQL 只暴露一个端点,在我的例子中是/graphql How do I capture the POST request coming in?如何捕获进来的 POST 请求? For better context,为了更好的上下文,

If I was using a REST API, after a successful payment, the webhook is meant to send a POST request with the payment data to /payment/verify .如果我使用的是 REST API,则在成功付款后,webhook 将发送带有付款数据的 POST 请求到/payment/verify But how can I achieve this using a GraphQL server, getting the data from the webhook directly to a resolver in my GraphQL server?但是,如何使用 GraphQL 服务器实现此目的,将数据从 webhook 直接获取到 GraphQL 服务器中的解析器? Thank you, anticipating your response.谢谢,期待您的回复。

Unless the post request sent by the payment processor meets the form of graphql request.除非支付处理器发送的 post 请求符合 graphql 请求的形式。 The request payload must meet the standard GraphQL AST.请求负载必须符合标准 GraphQL AST。 Only this way, the GraphQL in your server-side can parse the ASTs, do validation and execution works.只有这样,您服务器端的 GraphQL 才能解析 AST,进行验证执行工作。

The GraphQL post request form is: GraphQL 邮寄申请表为:

require('isomorphic-fetch');

fetch('http://localhost:4000', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: `
    query {
      todos {
        edges {
          node {
            completed
            id
            text
          }
        }
      }
    }` 
  }),
})
.then(res => res.json())
.then(res => console.log(res.data));

You can create additional routes and endpoints to use as webhooks, but a better way is to extract the webhooks function into an independent service.您可以创建其他路由和端点以用作 webhook,但更好的方法是将 webhook function 提取到独立服务中。 Connect to the database, execute business logic, and process data.连接数据库,执行业务逻辑,处理数据。

Your GraphQL API server that only has one endpoint /graphql is used for the API gateway service.您的 GraphQL API 服务器只有一个端点/graphql用于 API 网关服务。 The webhook service is one of the backend services. webhook 服务是后端服务之一。

在此处输入图像描述

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

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