-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add support for adding response
to the context on custom Express server
#506
Comments
response
to the contextresponse
to the context on custom Express server
@robin-macpherson can you share a link to or snippet here of the use-case. I want to better understand if middleware should or should not play a role here for example. |
@jasonkuhrt absolutely. Relevant files are:
Does that give you enough context? |
Thanks @robin-macpherson, yep (same thing I had checked out the other day IIUC). So, I don't think The need is that, after a particular resolver runs, a header must be set on the response. The API we design here cannot assume that Express server will be present. One plugin could contribute schema fields while another swaps the server (Fastify instead of Express). We could add to core a A simple If we're going to have Considering that we should abbreviate to Example resolve: async (parent, args, ctx) => {
const Password = await bcrypt.hash(args.Password, 10)
const user = await ctx.db.user.create({
data: {
Name: args.Name,
Email: args.Email.toLowerCase(),
Password,
},
})
const token = jwt.sign({ userId: user.Id }, process.env.APP_SECRET!)
ctx.res.cookie({
name: 'token',
value: token,
httpOnly: true,
maxAge: ONE_YEAR,
})
return user
},
}) And no longer needed: -schema.addToContext((request: any) => ({
- request,
- response: request.response,
-})) |
@jasonkuhrt that definitely seems like a fair point regarding the Regarding abbreviations, can the base property name still be the unabbreviated version which could then be aliased to the abbreviated names that you described, so that we can support both? Perhaps my mixed feelings about making the property names themselves abbreviated just comes from my weird style of keeping things super clear and explicit so I often actually do write It just feels like it would be nice to support both but perhaps this just makes the whole thing more convoluted. Curious to know your thoughts. |
👌 Good to know!
Minimizing the property footprint is a pretty effective way to maximize the leverage of intellisense I feel. Users could do the following fwiw but I don't see much value in it in practice (in the sense that, I don't think many users will actually want to do this). (_root, args, { response: res, request: req }) => {
// ...
} |
Had a longish discussion with @Weakky about this and #509. It involved things like:
We'll continue to mull. Needs a bit more attention to get this right. The framework server abstraction is a bit too weak right now to fully address the issue. Meaning we may need to iterate on what the framework's server system is generally. Then return to this issue concretely. |
@jasonkuhrt Good to know! That makes sense to me regarding maximizing leverage of intellisense, then. Thanks for sharing those notes from the discussion with @Weakky . I agree that I definitely appreciate being a part of the discussion around this 😊 |
I think its fair to say that @Weakky and I have directionally aligned that Nexus will ship with a server middleware system. A dedicated issue for this will be created soon. |
Hey @jasonkuhrt, thanks for the through explanation above. I was curious if you had any update regarding adding response to the context variable. I'm itching to use Nexus next, but am blocked as I use the graphql mutation to set my user's cookie once logged in. Or maybe you have a recommendation in the meantime on how to achieve this in the meantime? |
Hi @emroot , in the meantime we found that you can access the response by grabbing it off of the So for example, if you wanted to add it to the context you could do something like:
And then you could use it inside of your resolvers for the mutations where you are setting the user's cookie. Something like:
|
oh smart! Duh! Thanks for sharing! |
Haha, no worries! I was stuck on this for a while, too 🙂 |
Hey @robin-macpherson, I have the same use-case as you describe, and I tried to grab |
Talked with @Weakky today about where we want to go with this. Once we have the concept of Until then, @robin-macpherson would be great if you can contribute a recipe for your solution to the docs! |
@jasonkuhrt will do. I'll write to you directly to co-ordinate and @orjellingsen I'm going to update this thread and tag you when the recipe is ready. Hang tight! 💪🏼 |
@robin-macpherson any update by chance on your recipe? |
Hi @emroot , sorry been completely slammed. In the meantime perhaps you can tell me what error you are getting and I might be able to help quickly unblock you. |
@robin-macpherson yeah super simple. When I try to use |
@emroot, please try with this sintax:
Working here so, hope it helps. |
@decosvaldo Thanks for the above example. It works well, but one issue I'm running into is typescript telling me Any way around this? Trying to ignore that specific line using |
Yeah, I don't see res or response on the request object either. |
@decosvaldo this works thanks. There's a typescript error but I can easily override that. |
@robin-macpherson having the same problem as @emroot , trying to access "nexus": "^0.25.0",
"nexus-plugin-prisma": "^0.16.1", |
@yuyao17 The code mentioned above works for me! Are you having issues with typescript? Just set the type of the context argument as "any". If "res" is not correct try "response" |
@victormidp I think I found the root cause. |
@yuyao17 I don't believe that is the case because we are now using Next's API routes and everything is working fine. I'm super busy today running behind on a few things so I'm not able to look into each person's case but please feel free to take a look at the Journaly repo and see exactly how we're doing things and let me know if you have any questions or if I can point you to something in particular: https://github.com/Journaly/journaly/tree/master/frontend/nexus |
@emroot did you get all sorted out with these issues? |
yeah works for me. just a typescript error that I was able to override. |
What
express
server so that we can access theresponse
from inside of resolvers.Why
cookie
on the response inside of resolvers, which could be important for a lot of basic authentication setups, for example with Jason Web Tokens as I am doing.How
schema.addToContext
along with therequest
, with an implementation that would look something like:The text was updated successfully, but these errors were encountered: