question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject custom headers to express graphql

See original GitHub issue

Hey 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
acaocommented, Dec 9, 2019

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 executed

0reactions
acaocommented, Dec 9, 2019

I’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 a headers 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found