• 1Installation
  • 2Preliminary Knowledge
  • 3G.apex Demo
  • 4G.apex Query
  • 5Create Object Types
  • 6Create Schema
  • 7Resolver Functions
  • 8Serve Query Request
  • 9Mutation
  • 10Parameters
  • 11Default Value
  • 12Aliases
  • 13Fragments
  • 14Variables
  • 15Directives

G.apex

  • Docs
  • Tutorials
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 resolve(List parents, Map args, G.ResolvingContext context) {
        List result = new List();

        for(Object parentObj : parents) {
            Map parent = (Map)parentObj;
            List found = R.of(books).filter(R.propEq.apply('authorId', parent.get('id'))).toList();
            result.add(found);
        }

        return result;
    }
}

In this resolver, we get the list of books related to the author based on the passed in list of parents.

Basically batch resolvers should be used if there is such operation like doing DML operations or querying or making http requests.

Done