const { GraphQLClient } = require("graphql-request");

/**
 * Middleware to add a GraphQL Client to the request object
 * Adds the following to the request object:
 * req.userGraphQLClient - GraphQL Client with user Bearer Token
 * req.BearerToken - Bearer Token
 * @param req
 * @param res
 * @param next
 */
const withUserGraphQLClientMiddleware = (req, res, next) => {
  const BearerToken = req.headers.authorization;
  req.userGraphQLClient = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
    headers: {
      Authorization: BearerToken
    }
  });
  req.BearerToken = BearerToken;

  next();
};

module.exports = withUserGraphQLClientMiddleware;
