Inject custom headers to express graphql
See original GitHub issueHey everyone,
I’d like to know how to inject headers when using express graphql. The problem is, if I use multiple instances like this, importing modules to work as a SPA every single header coming from the module get overwritten by the first module I import:
app.use('/route1/graphql', expressGraphQL({
schema: firstSchema,
graphiql: (process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'development'),
formatError: e => AccelerateErrorHandler(e, 'an error')
}));
app.use('/route2/graphql', expressGraphQL({
schema: secondSchema,
graphiql: (process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'development'),
formatError: e => AccelerateErrorHandler(e, 'an error')
}));
So, is there a way to inject headers directly as params in the expressGraphQL
function?
app.use('/route1/graphql', expressGraphQL({
schema: secondSchema,
headers (?)...
graphiql: (process.env.NODE_ENV === 'local' || process.env.NODE_ENV === 'development'),
formatError: e => AccelerateErrorHandler(e, 'an error')
}));
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Add custom headers to 'request' - node.js - Stack Overflow
I am proxying my api via following setup in my express config // Proxy api calls app.
Read more >Advanced HTTP networking - Apollo GraphQL Docs
Customizing request logic. The following example demonstrates adding a custom link to Apollo Client. This link adds an Authorization header to every HTTP ......
Read more >express-graphql-header - npm
Make graphiql send token from header 'Authorization' or pass json string to set header. Latest version: 1.0.1, last published: 3 years ago.
Read more >Context - GraphQL Yoga
request - Fetch API Request object that represents the incoming HTTP request in platform-independent way. It can be useful for accessing headers ......
Read more >How to set headers in request in Node.js - Educative.io
We will use request.setHeader() to set header of our request. The header tells the server details about the request such as what type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
good question! because you are reusing the same express routes each time to instantiate each middleware, the middlewares will overwrite eachother, so in this example only the third schema will be executable. you may want to read the docs to express app.use(). do you want to combine schemas?
as far as adding headers in express, you can do this via a middleware that uses
res.headers.set()
. make sure this middleware is added before graphql express middleware, as the latter is a terminating middleware and thus it sends response, any middlewares after it will not be executedI’m sorry but it just doesn’t look like you can override inbound request headers, and i’m still not really sure why. if
graphql-express
had aheaders
option it would be for setting response headers, not overriding the inbound request headers.Maybe you’ll find some answers from the express community: https://gitter.im/expressjs/express