-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Operation headers are not interpolated when context.req.headers is a HeaderMap #5226
Comments
Did you try |
We don't recommend using Apollo Server with Mesh because Apollo Server doesn't allow us to hook into the different phases of GraphQL Execution then we cannot use all the features of Envelop which is the plugin system Mesh uses under the hood. But if you still want to use Apollo Server with Mesh, you have to make sure request object is received properly in this line; |
@ardatan I think we went with Apollo Server because it was the natural fit with our frontend that uses the ApolloClient, but now we have quite a few Apollo Server plugins which makes it harder to switch to GraphQL Yoga.
What do you mean by that? I re-used the code you wrote in nextjs-apollo-example and when I add a debugger, |
Could you try the following? const { schema, execute, contextFactory } = getEnveloped({
- req: requestContext.request.http,
+ request: requestContext.request.http,
}); |
I'm not sure I understand what we're trying to do here. The request object is passed as expected, the issue is just that the I tried what you suggested, but I have the same result: operationHeaders: {
'access-token': "{context.request.headers.get('access-token')}",
}, |
Mesh has this plugin that converts header maps to regular objects so you can access them like |
Got it, thanks. I checked and this plugin is only running on this url Nevertheless, the https://github.com/Urigo/graphql-mesh/blob/master/packages/utils/src/getHeadersObj.ts#L10 |
Hmm it seems Apollo doesn't use the standard Headers. But if it implements a Map interface, we can support it actually. I'll create a PR for this change. |
#5240 |
Issue workflow progress
Progress of the issue based on the
Contributor Workflow
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 becausecontext.req.headers
was not an object anymore, but a HeaderMap (you need to callheaders.get('key')
).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. It was also 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 usingapollo-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:
@graphql-mesh/string-interpolation
: 0.4.2 (latest)express
: 4.18.2 (latest)@apollo/server
: 4.5.0 (latest)Suggested solution
See #5227 for details.
🚀 See it in action by checking out the
fixed
branch of the issue reproduction repo.The text was updated successfully, but these errors were encountered: