expressgraphqlexpress-gateway

Modifying graphql query variable using express-gateway


I'm trying to modify a graphql query variable using express-gateway.

The code on the gateway is as below,

const axios = require("axios");
const jsonParser = require("express").json();
const { PassThrough } = require("stream");

module.exports = {
  name: 'gql-transform',
  schema: {
    ... // removed for brevity sakes
  },
  policy: (actionParams) => {
    return (req, res, next) => {
      req.egContext.requestStream = new PassThrough();
      req.pipe(req.egContext.requestStream);
  
      return jsonParser(req, res, () => {
        req.body = JSON.stringify({
          ...req.body,
          variables: {
            ...req.body.variables,
            clientID: '1234'
          }
        });

        console.log(req.body); // "clientID": "1234" is logged in the body.variables successfully here
        return next();
      });
    };
  }
};

Now, when I hit the request from POSTMAN, the request goes through and returns a 200OK only when I include clientID, otherwise, it throws as error

"message": "Variable "$clientID" of required type "ID!" was not provided."

Any idea what could be going wrong here?


Solution

  • The only way I could get this working was by using node-fetch and then making a fetch request to the graphql-sever from my middleware instead of doing a return next() and following the middleware chain.

    My setup is something like the following,

    Client (vue.js w/ apollo-client) ---> Gateway (express-gateway) ---> Graphql (apollo-server) ---> Backend REST API (*)
    

    When my client makes a graphql request to my gateway, I've modified my middleware to do the following (as opposed to what's in the question),

    const jsonParser = require("express").json();
    const fetch = require('node-fetch');
    
    module.exports = {
      name: 'gql-transform',
      schema: {
        ... // removed for brevity sakes
      },
      policy: () => {
        return (req, res) => {
          jsonParser(req, res, async () => {
            try {
              const response = await fetch(`${host}/graphql`, {...}) // removed config from fetch for brevity
              res.send(response);
            } catch (error) {
              res.send({ error });
            }
          });
        };
      }
    };