Fetch a record and its relations with Prisma Client


#1

Let’s say I have this:

type User {
    id: ID! @id
    email: String! @unique
    customer: Customer
}

I want to retrieve the customer. I’ve seen two ways to do it in the docs but none of theme are satisfying.

Solution 1

let customer = await prisma.user({ email: args.email }).customer()

Now, how can I access to the user record itself?

Solution 2

let user: User = await prisma.user({ email: args.email }).$fragment(`
        fragment EnsureCustomer on User {
            id
            email
            customer {
                id
            }
        }
    `)

While I can use user.customer to access the data, this makes my IDE cry as the customer property doesn’t exist in User (I’m using typeScript).

Any thought on that ?


#2

Create a User.js resolver and do something like

User: { costumer: (root) => prisma.user({email:root.email}).costumer() }


#3

Thanks but this part of my code is not related to GraphQL so creating a resolver won’t help.