graphql

fabrao 2020-05-10T15:38:00.092800Z

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.

fabrao 2020-05-10T15:44:44.093900Z

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?

fabrao 2020-05-10T15:45:08.094200Z

or do I have to use cpu-load resolver?

2020-05-10T15:56:52.096900Z

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).

2020-05-10T16:03:57.100400Z

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.

2020-05-10T16:04:44.101Z

(Skipping details here)

2020-05-10T16:05:39.101500Z

Does that help you?

fabrao 2020-05-10T16:23:47.101800Z

yes, I saw something like this in https://github.com/promesante/hn-clj-pedestal-re-frame

fabrao 2020-05-10T16:24:40.102100Z

: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

fabrao 2020-05-10T16:24:56.102300Z

in this example I can use like in that

fabrao 2020-05-10T16:25:04.102500Z

thanks a lot

2020-05-10T17:21:14.102700Z

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

2020-05-10T18:42:22.103Z

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

👍 2
1