Fork me on GitHub
#graphql
<
2021-04-19
>
fabrao03:04:36

Hello all, how do I use relationship one-to-many? Is it in object or query?

Lennart Buit08:04:30

I don’t fully understand the question. In lacinia, you can just mark a field as having type (list :Thing), then you can just return a list from your resolver

fabrao09:04:24

but how do you make the relationship one to many in resolver?

fabrao09:04:14

like if you have relationship product -> items

Lennart Buit12:04:08

You just return a list of things. So say that you have a type Product { }, that has a field Product#items, which has type (list :Item), you can create a resolver that returns that list of items

hlship16:04:03

So, typically, your root query resolver can hit a DB and pull a record that includes an id of some kind. The query document selects fields from that data, but will also pass it (not the selected map, the raw map as returned from your own code) down a level; that can extract the id and run a new query that returns a list of child objects. It's right in the name: a graph of objects. Complexities hidden on the server, client is free to navigate that graph all in one go (rather than REST where you either get too much data, or have to make multiple queries). Of course, nothing is free, but more advanced Lacinia users can mitigate costs by pre-selecting data.

fabrao21:04:10

Lennart, but the resolver is formatted like what?