Giter VIP home page Giter VIP logo

Comments (10)

helfer avatar helfer commented on May 3, 2024 33

You're right, they're not defined in the example I gave above, so it wouldn't work. What you actually have to write is something like this (which wraps the graphqlHTTP middleware in a function that acts as middleware:

app.use('/graphql', (req, res) => {
  return graphqlHTTP({
    schema,
    graphiql: true, // or whatever you want
    context: { req, res },
  })(req, res);
);

from express-graphql.

helfer avatar helfer commented on May 3, 2024 27

I'm not sure why you thought you had to fork the project to do this... the following should work:

app.use('/graphql', graphqlHTTP({
  schema,
  graphiql: true, // or whatever you want
  context: { req, res },
});

and in your resolve function:

resolve: (root, args, context) => {
  let cookieValue;
 /* your code here */
  context.res.set('cookie', cookieValue);
}

from express-graphql.

leebyron avatar leebyron commented on May 3, 2024 5

response is now added to the middleware config function, and of course you can do what @helfer suggested as well, but definitely be careful in setting headers from a graphql function, it's typically a sign of something gone awry.

from express-graphql.

leebyron avatar leebyron commented on May 3, 2024 2

I would strongly suggest against using GraphQL as a replacement for OAuth or other forms of authentication.

from express-graphql.

helfer avatar helfer commented on May 3, 2024

You could wrap graphqlHTTP and pass res into the context, then you can access it from resolvers.

from express-graphql.

vieks avatar vieks commented on May 3, 2024

Yep of course there are workarounds, for instance I had fork this project and added the res to the options. It took only 3 lines of code... So why the heck only the req is set ???

from express-graphql.

vieks avatar vieks commented on May 3, 2024

Where did your req and res come from ?

from express-graphql.

shakaIsReal avatar shakaIsReal commented on May 3, 2024

it's typically a sign of something gone awry

@leebyron how else would you set a cookie if user signed up or logged in?

from express-graphql.

dionjwa avatar dionjwa commented on May 3, 2024

@leebyron at the end of a valid non-graphql OAuth flow (e.g. firebase) the client ends up with a token that you need to send to the server (and expecting the server to validate the token (from firebase) and set a secure cookie. This is one of the few ways to have graphql subscriptions since websockets cannot set custom headers (so only cookies provide security).

from express-graphql.

trevorezwaiver avatar trevorezwaiver commented on May 3, 2024

I apologize if this is the incorrect place to post something like this. I have GraphQL calling my rest api in it's resolver. When a person logs in I issue a jwt in a cookie. I have my api locked down and GraphQL is working fine if I manually pass over the cookie. However, when I'm in my app, I had planned to pass over the cookie as an argument but I can't since it's a cookie... Could I possibly use the above approach to issue another token from graphql? Something about that does not feel right.

from express-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.