Hey guys, I’m having some trouble with returning n...
# orm-help
j
Hey guys, I’m having some trouble with returning nested information from a query located in my resolver. The line
const user = await ctx.db.query.user({ where: { id } })
is working as expected, but it’s not returning any nested data (in this case a nested type containing Stripe customer information is what I want to get back). These two nodes have a relation set up in my datamodel, and I’m correctly able to return the data using the same query in my playground. Am I missing something here? Why am I not able to get the nested data back in my resolver? And how else would I approach this, if it’s not doable?
a
By default, prisma-binding methods will return all the scalar values for the type. You need to pass the
info
argument as the second argument to you prisma-binding methods to get any relational data.
j
That example illustrates how I have my queries set up, but the issue I’m having is slightly different. I basically want to do exactly that, but within one of my mutations. So I want to do something like:
Copy code
makePayment: async(parent, args, ctx, info) => {

const user = await ctx.db.query.user({ where: { id } }, info)
if(user.stripe.stripeId) { dosomething }

}
Currently, my
user
variable, without
info
, does not return any nested data but returns everything else associated with the
user
. If I include
info
, it only returns the node type.
a
"If I include
info
, it only returns the node type" Could you clarify what you're getting back?
j
{ __typename: 'User' }
is the only thing returned.
a
Ah. And what does the mutation operation itself look like?
j
Well, essentially what I need it to be able to do is check if the
user
object has an existing relation to a
stripe
object, and find the
stripeId
from within that object. If it does, return it to be used in the
createCharge
mutation, which creates a charge to be sent to the Stripe account. Else, create a new
stripe
object and connect it to the
user
. Then use it in the
createCharge
mutation. I would copy-paste the whole thing in right now, but it’s kind of a mess, haha. Tried for a while to find some workarounds and/or better ways to approach the problem. Gimme a second and I’ll format it more appropriately.
I hope that wasn’t too much word salad.
a
It sounds like you want to query for Stripe profiles with a specific user id. That could look similar to the code they use in one of the prisma examples here: https://github.com/graphql-boilerplates/node-graphql-server/blob/master/advanced/src/resolvers/Query.js#L11
In this case, there looking for a posts by a specific author. In your case, you'd be looking for strip profiles from a specific user.
You can even use the exists functionality as they do here to see if the strip profile exists at all: https://github.com/graphql-boilerplates/node-graphql-server/blob/e62f28e4d2e4d17734413591a1279b7d8b636c8c/advanced/src/resolvers/Mutation/post.js#L23
j
Ahhhhh, so just create a specific query that handles this case in my resolvers, that makes more sense! I’ll see if that works, I feel like that sets me on the right track. Seems obvious now that you pointed it out. 😛
The
exists
function was something I was playing with, but the problem was that it doesn’t return the data. So it would tell me if it exists, but if it found that it DOES exist, that didn’t help me get the info I need. But I’ve definitely employed it in some other places in my application, very useful.
a
Sounds good!
j
So my resolution for this turned out to be less obvious and a bit annoying, but it worked. I couldn’t get the
user
object to return it’s Stripe information when looking for a single instance, Prisma just would not generate a way to query for nested data in this way. I ended up having to query for an array of
stripeCustomer
objects on the user and just return the first (and only) result in the array. Not optimal, but I don’t think it’s necessarily an issue? This approach also forced me to have to run a separate query for the user object so I could retrieve their email address for a different reason in the same mutation. I would’ve preferred to do one query and get all this data, but I can’t for the life of me get it working the way I would expect it to.