Resolvers
Resolvers are the main building blocks when it comes to fetching data. Every field in our GraphQL schema is backed by such a resolver function, responsible for returning the field's value. Since a resolver is just a function, we can use it to retrieve data from a database, a REST service, or any other data source as needed.
Resolver Tree
A resolver tree is a projection of a GraphQL operation that is prepared for execution. For better understanding, let's imagine we have a simple GraphQL query like the following, where we select some fields of the currently logged-in user.
query {
me {
name
company {
id
name
}
}
}
In Hot Chocolate, this query results in the following resolver tree.
This tree will be traversed by the execution engine, starting with one or more root resolvers. In the above example the me
field represents the only root resolver.
Field resolvers that are sub-selections of a field, can only be executed after a value has been resolved for their parent field. In the case of the above example this means that the name
and company
resolvers can only run, after the me
resolver has finished.
Resolvers of field sub-selections can and will be executed in parallel. Because of this it is important that resolvers, with the exception of top level mutation field resolvers, do not contain side-effects, since their execution order may vary.
The execution of a request finishes, once each resolver of the selected fields has produced a result. This is of course an oversimplification that differs from the actual implementation.
Defining a Resolver
Resolvers can be defined in a way that should feel very familiar to C# developers, especially in the Annotation-based approach.
Properties
Hot Chocolate automatically converts properties with a public get accessor to a resolver that simply returns its value.