0

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?

painotpi
  • 6,894
  • 1
  • 37
  • 70
  • Can you share type for mutation / query (operation that is throwing error) from graphql schema? it looks like clientID is defined as mandatory field either in schema or UI code. – Raeesaa Feb 16 '21 at 11:30
  • @Raeesaa the only way I could get this working was by using `node-fetch` and writing a `fetch` request to the graphql server instead of doing a `return next()` from the middleware. I'm fine with this approach for now. – painotpi Feb 16 '21 at 12:10

1 Answers1

0

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 });
        }
      });
    };
  }
};
painotpi
  • 6,894
  • 1
  • 37
  • 70