简体   繁体   中英

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

I am working on a project currently using 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. Now I was wondering how to achieve this, considering that GraphQL exposes just one endpoint, in my case /graphql . How do I capture the POST request coming in? 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 . But how can I achieve this using a GraphQL server, getting the data from the webhook directly to a resolver in my GraphQL server? Thank you, anticipating your response.

Unless the post request sent by the payment processor meets the form of graphql request. The request payload must meet the standard GraphQL AST. Only this way, the GraphQL in your server-side can parse the ASTs, do validation and execution works.

The GraphQL post request form is:

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. 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. The webhook service is one of the backend services.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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