-
Notifications
You must be signed in to change notification settings - Fork 575
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
How to inject headers while calling graphql query #663
Comments
Hi @saurabhtank, if you need to use const server = new GraphQLServer({
// ...
context: async (yoga) => (
yoga.connection
? yoga.connection.context
: {
request: yoga.request, // then from resolvers: ctx.request.headers ...
response: yoga.response
}
)
// ...
}); Otherwise, if you need to implement JWT authentication middleware : // modules deps.
import jwt from 'jsonwebtoken';
import { GraphQLServer } from 'graphql-yoga';
// schema modules.
import typeDefs from './typeDefs';
import resolvers from './resolvers';
// server setup.
const server = new GraphQLServer({
typeDefs,
resolvers,
context: async (yoga) => ({ user: yoga.request.user })
// other props.
});
// auth middleware.
function AuthMiddleware (req, res, next) {
const token = req.headers['x-token']; // or any header field used for toke.
if (token) {
try {
const { user } = jwt.verify(token, process.env.SECRET_TOKEN);
req.user = user;
} catch (err) {
throw new Error('some thing worng in auth middelware: ' + err)
}
}
next();
}
// load auth middelware to express directly.
server.express.use(AuthMiddleware);
// server options.
const options = {
port: 4000,
endpoint: '/graphql',
subscriptions: {
path: '/subscriptions'
}
}
// run server.
server.start(options, console.log(`Server ready at http://localhost:${options.port} 🚀 .. `)) |
Hey @saurabhtank @3imed-jaberi we @the-guild-org are the new maintainers of this project. We are actively developing |
while calling graphql query we are not able to inject headers.
ctx.db.query.moduleattributess({ ...args.input }, info);
We have tried adding by below syntax.
ctx.headers["Authorization"] = "Bearer Test"
but we are not able to find it in server request.
@maticzav Please let me know if there is any way we can do this.
The text was updated successfully, but these errors were encountered: