Fork me on GitHub
#graphql
<
2020-05-10
>
fabrao15:05:00

Hello all, I´m starting with lacinia and I´d like to know how to deal with one-to-many relationship in schema and resolver.

fabrao15:05:44

like here

{:objects
 {:Server
  {:description "Servers monitoring"
   :fields
   {:hostname {:type (non-null String)}
    :ip {:type (non-null String)}
	:cpu-load {:type (list :CPU)}}}
  :CPU {:description
		:fields {:time (non-null String)
				 :value (non-null Int)}}}}
like in cpu-load , it will automatic try to find relationship in database?

Lennart Buit15:05:52

You can deal with it in two ways, either provide a resolver on the field level that does a database query, or make sure that you also return the relation when you are resolving the containing object (in this case Server).

Lennart Buit16:05:57

Maybe the easiest way to think about it: you provide a resolver on an query that returns some form of map, for each field in your query, lacinia invokes a resolver you specify or otherwise just gets the value at the field name in the map. Lacinia does this recursively until the entire query is resolved.

Lennart Buit16:05:44

(Skipping details here)

Lennart Buit16:05:39

Does that help you?

fabrao16:05:40

:posted_by {:type :User
                :description "User who posted this link"
                :resolve :Link/user}
I didn´t know if I can use resolve inside field domain

fabrao16:05:56

in this example I can use like in that

fabrao16:05:04

thanks a lot

Lennart Buit17:05:14

There is one caveat: you may end up with N+1 queries, so lets say that you are resolving a list of Posts made by Users, if you define a resolver for :posted_by like ^there, you may end up with 1 query for your Posts and N queries for each user belonging to that post

oliy18:05:22

In which case for the n+1 issue consider superlifter (dataloader pattern) which is written to work with lacinia - but get a good understanding of Lacinia first! https://github.com/oliyh/superlifter

👍 8
thanks2 4
fabrao15:05:08

or do I have to use cpu-load resolver?