How exactly sangria "Prepared query" uses *root* parameter?

206 Views Asked by At

how exactly root parameter used in sangria? here is documentation

preparedQuery.execute(userContext = someCustomCtx, root = event))

query already contains resolver, how than root would be used?

1

There are 1 best solutions below

0
Gagandeep Kalra On

Graphql queries are nested in nature, meaning that every GraphQL query has the shape of a tree, they are never circular.

The execution begins at the root of the query. First, the executor calls the resolve function of the fields at the top level. It waits until all these resolve functions have returned a value. As soon as that happens, the GraphQL server will take the return value of this resolve function and pass it to the resolve functions of the second level fields because those are the fields that were requested in the query.

Results from one node of the tree are available to all its immediate children, this continues in a cascading fashion down the tree.

Meanwhile, Context is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. Context is different from Root in that it is computed first and then is available for all the fields.

Now with all this context, to answer your question, the resolvers for the top level fields have the root value available for them. This root value type is defined in the executor itself-

case class Executor[Ctx, Root]

and you pass an instance of Root type(or any other instance that can be proved to be as same as this type) when you call the execute function.

If your use case requires no use of this Root, you can set it as Unit and pass in root = (), when invoking the execute method.