Hello, everybody. Wanted to ask about fields’ reso...
# orm-help
j
Hello, everybody. Wanted to ask about fields’ resolution with prisma client. Here is 2 queries: 1.
Copy code
context.prisma.someModel.findUnique({ where: { id: someId } }).someField()
2.
Copy code
(await context.prisma.someModel.findUnique({ where: { id: someId } })).someField
When I use
await
it seems like I don’t get access to the relation fields. In the second example if the field is virtual(some relation) it won’t be available, why is that ? But if I use the example #1 I am able to somehow call the field and resolve its value. How does it work ? How to properly retrieve fields of a particular model ? Maybe some links to the doc? Thank you.
s
With the second example you can do: const obj = (await prisma.whatever.findUnique()).someField
j
Thanks for the reply, yes that’s what I meant of course. I’m gonna edit the question. It just for some fields I can call them and resolve in that way, as it mentioned in the docs. https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/upgrading-prisma-binding-to-sdl-first#21-implementing-th[…]-for-the-user-type Like here we call
posts()
and
profile()
. But for some fields there is an error like
ctx.prisma.salesDomain.findUnique(...).shippingMethodOrder is not a function
but
shippingMethodOrder
exists on the model. I guess regular field are resolved just as properties, but relations in some other way. And I wanted to read more about fields’ resolution: regular and relations.
s
Got it, and yes. Regular fields are simply properties. You don’t need to call them as functions. Prisma eagerly loads relations only if you mention them with
include
So if you want consistency, you can add an include clause. Then you can access relations and properties directly instead of calling some and not others
r
The relation chaining that you see via the fluent API and the other fields are just the normal fields of the model. Prisma doesn’t exactly return a Promise but a wrapper over a Promise called
PrismaPromise
and that makes this possible.
j
Thank you
💯 1