<https://www.graph.cool/forum/t/authentication-wit...
# prisma-whats-new
w
https://www.graph.cool/forum/t/authentication-with-graphql-yoga-prisma-and-middleware/2285 this seems to show it using the four argument express middlware function, but when you pass express middlware 4 arguments it assumes it’s an error handler
this adds the
db
object to the context. Is this what you need?
w
yes - how do I access context ina yoga middlware?
is it
server.context().db
?
n
Is your server setup similar to the index.js I shared, or did you make some express--specific addititions?
w
nope -it looks like that
I figured out server.context().db works
is that the best way?
s
Can you share a code snippet and briefly describe again what you’d like to do?
Generally you should never run
server.context().db
but rather use the injected
context
argument (3rd argument)
w
Just trying to make a middlware that will put the user on the request object
the third argument is next() isn’t it?
s
Ah! I’ll look into it and try to come back to you shortly! (~30 - 60min)
w
cool - it’s working with context() right now so it’s not a blocker - thank you!
👍 1
and just FYI (im sure you already know) if an express middlware has 4 args it’s skipped and used as an error handler
👍 1
s
can you also share your
createServer
fn?
w
createServer === http://wes.io/qe7K
l
Hey @wesbos, super excited that you looked at my post. I feel like the world came full circle. 🙂 In my example, I defined
db
outside of the
GraphQLServer
constructor so I could pass it to middleware
w
ahhhhh
and thne you just import/export the DB?
l
Exactly
w
smart
l
So
getUser
is just a function taking the middleware arguments and the database
w
awesome thank you
💯 1
Here is another question while you are here, when I make a query from Yoga to the prisma DB, it only returns some fields - how does prisma determine which fields to return?
l
It's all in the
info
object. I have to keep rereading the Nik's blog post. It's one of those where every time I read it, I get something new. https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a
You can specify which fields to return with a string, e.g.
db.query.users({}, '{ id }')
will give you just the id back
But if you pass
info
, it will return whatever was queried, e.g.
{ users { id name email } }
w
yeah I’m doing that now, I’m just curious how it decides which fields to bring back if I don’t give it an info
for example in my middleware
l
Then you can just think of it like a SQL look up. You get whatever is in the row, but no joins
So if user had a
posts
relations, you won't get any posts
w
ahhhh
that makes sense
thank you
l
But if you need relations in your middleware, you can ask for them...
db.query.user({...}, '{ id posts { id } }'
w
great thats what im doing now
l
Awesome! I'm excited to see the course. I have my money burning a hole in my pocket!
w
😄
n
just to add to this great conversation (thanks guys!), I described the behavior if info is missing here: https://www.graph.cool/forum/t/querying-specific-fields-in-db-from-local-service-with-prisma/2075/4?u=nilan
❤️ 1