Giter VIP home page Giter VIP logo

graphql-mesh-headers-not-interpolated's Introduction

graphql-mesh-headers-not-interpolated

GraphQL Mesh: Issue reproduction for the operation headers not being interpolated when req.headers is a Map

Getting started

  • Install dependencies with yarn install
  • Start the services then the gateway with yarn start
  • Open the Apollo Sandbox at http://localhost:4000 and execute a query:
# Taken from https://rickandmortyapi.com/documentation/

query {
  characters(page: 2, filter: { name: "rick" }) {
    info {
      count
    }
    results {
      name
    }
  }
}

Issue reproduction

Instructions

query {
  me {
    id
    name
    email
  }
}
  • Click on the "Headers" tab to add the access-token header which is also passed in the operationHeaders mesh config:
Key Value
access-token 456
  • Run the query in the Apollo Sandbox
  • โš ๏ธ data.me is null in the response while it should return the user object because we passed the correct access-token header

Using a debugger

  • Open the Node.js DevTools
    • Visit chrome://inspect on Chrome
    • Click on the hyperlink "Open dedicated DevTools for Node"
    • In Node.js DevTools, go to the "Connection" tab and add one for localhost:8123
  • Add a debugger statement right before return headers; at the end of this file: node_modules/@graphql-mesh/string-interpolation/dist/cjs/resolver-data-factory.js
  • Start the server with node --inspect=8123 server (change the start script of the gateway)
  • Run the query in the Apollo Sandbox
  • You should see the headers being returned and used as operationHeaders: the access-token should be empty

Screenshot 2023-03-15 at 14 21 22

How to fix

๐Ÿš€ Run it by checking out the fixed branch

We can simply allow the operationHeaders mesh config to be a function to use as a hook. This way, we trust Javascript to interprete the code instead of adding extra string interpolation based on regular expressions.

operationHeaders: ({ context }) => ({
  'access-token': context.req.headers.get('access-token'),
}),

graphql-mesh-headers-not-interpolated's People

Contributors

pmrotule avatar

Watchers

 avatar

graphql-mesh-headers-not-interpolated's Issues

Operation headers are not interpolated when context.req.headers is a HeaderMap

Issue workflow progress

Progress of the issue based on the
Contributor Workflow

  • 1. The issue provides a reproduction available on Github
  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug

Context

Since we are using GraphQL Mesh together with Apollo Server and Express, upgrading some of the mesh dependencies to the latest versions became a challenge because we couldn't find instructions for Express in your documentation like we used to. We finally dug a bit deeper and found the nextjs-apollo-example which made us understand how to upgrade using an executor in Apollo Server.

The issue

We managed to upgrade all our graphql-mesh dependencies and everything was working as expected besides one thing: the operationHeaders based on request headers were not interpolated because context.req.headers was not an object anymore, but a HeaderMap (you need to call headers.get('key')).

Screenshot 2023-03-15 at 14 21 22

Using "{context.req.headers.get('access-token')}" instead of "{context.req.headers['access-token']}" doesn't fix the issue because of the way graph-mesh is interpolating the string value: it only allows to access the property of an object as the last bit of the interpolated string (see here).

We were able to work around the issue (see workaround branch) by creating a new object with one property and access it right away:

"{({ x: context.req.headers.get('access-token') }).x}"

Nevertheless, the workaround is only working in our real GraphQL Mesh integration and was working in the issue reproduction repo at first with only one public GraphQL API, but once I made it a monorepo to add a local account service, the workaround stopped working and the interpolation was broken. It was returning x: context.req.headers.get('access-token') }).x} like it would consider the opening bracket of the object being the start of a string interpolation. We had the same issue when I tried to write integration tests using apollo-server-integration-testing.

To Reproduce

Find the steps to reproduce in the README of the issue reproduction repo:
https://github.com/pmrotule/graphql-mesh-headers-not-interpolated#issue-reproduction

Expected behavior

"{context.req.headers.get('access-token')}" would be interpolated as expected instead of returning an empty string.

Environment:

  • OS: MacOS Monterey 12.6.1
  • @graphql-mesh/string-interpolation: 0.4.2 (latest)
  • NodeJS: 18.14.0
  • express: 4.18.2 (latest)
  • @apollo/server: 4.5.0 (latest)

How to fix

๐Ÿš€ Run it by checking out the fixed branch

We can simply allow the operationHeaders mesh config to be a function to use as a hook. This way, we trust Javascript to interprete the code instead of adding extra string interpolation based on regular expressions.

operationHeaders: ({ context }) => ({
  'access-token': context.req.headers.get('access-token'),
}),

See PR for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.