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.
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?or do I have to use cpu-load
resolver?
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
).
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.
(Skipping details here)
Does that help you?
yes, I saw something like this in https://github.com/promesante/hn-clj-pedestal-re-frame
:posted_by {:type :User
:description "User who posted this link"
:resolve :Link/user}
I didn´t know if I can use resolve
inside field domainin this example I can use like in that
thanks a lot
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
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