Skip to content
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

Closed
saurabhtank opened this issue Jan 22, 2021 · 2 comments
Closed

How to inject headers while calling graphql query #663

saurabhtank opened this issue Jan 22, 2021 · 2 comments

Comments

@saurabhtank
Copy link

saurabhtank commented Jan 22, 2021

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.

@3imed-jaberi
Copy link

3imed-jaberi commented Jan 29, 2021

Hi @saurabhtank, if you need to use request and response from express from ctx, you need to do something like that:

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} 🚀 .. `))

@saihaj saihaj mentioned this issue Dec 5, 2021
32 tasks
@saihaj
Copy link
Collaborator

saihaj commented Feb 19, 2022

Hey @saurabhtank @3imed-jaberi we @the-guild-org are the new maintainers of this project. We are actively developing v2. Testing is a critical part of software development and the new Yoga Server comes with built in way to test https://www.graphql-yoga.com/docs/testing. You can try out the beta release @graphql-yoga/node@beta and give us feedback. Thanks!

@saihaj saihaj closed this as completed Feb 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants