Getting started with G.apex
Resolver Functions
In G.apex, we fetch information based on each nodes. When it comes to the relationship between different nodes, we use resolver functions to handle.
Consider the query below:
{
"query": {
"book": {
"@id": "2",
"name": "",
"author": {
"name": ""
}
}
}
}
We want to further get the author information related to the book. In this case, we define our book type as below.
G.ObjectType bookType = new G.ObjectType('Book', 'Book__c')
.addField('id', G.StringType, 'Id')
.addField('name', G.StringType, 'Name')
.addField('author', new G.ReferenceType('Author'), new BookAuthorResolver());
BookAuthorResolver
is provided to author
field, so that whenever the relationship is required, the resolver function will be invoked.
Here is what BookAuthorResolver
looks like:
private class BookAuthorResolver implements G.Resolver {
public Object resolve(Map parent, Map args, G.ResolvingContext context) {
return R.of(authors).find(R.propEq.apply('id', parent.get('authorId'))).toMap();
}
}
In the resolver, we find the author that matches the authorId
of the book from the list of authors.
Notice here that we are using a resolver that does not take a batch. If we want to use a batch resolver, we do it like this:
private class AuthorBooksResolver implements G.BatchResolver {
public List