What is G.apex?
G.apex is a library that helps you adopt GraphQL features in Apex.
G.apex is a library that helps you adopt GraphQL features in Apex.
G.apex Query is a JSON query that very much resembles GraphQL queries.
Sadly no. G.apex adopts similar concepts from GraphQL, but is tuned for Salesforce Apex and is therefore not compatible with GraphQL specification.
Influenced by GraphQL, G.apex query can help you build flexible service end points.
For example, normal RESTful end point like below can get you a book:
https:///api/v2/book/{id}
However, you have to code every single end point like this, and they are just not as flexible and composable.
Say what if I want to get some extra fields of the book? what if I also want to get the related author of the book?
G.apex can help you build a more flexible and composable end point like this:
{
"query": {
"book": {
"@id": "2",
"name": ""
}
}
}
You can control the fields of the result by adding or removing extra fields. Or you can get the related author easily like this:
{
"query": {
"book": {
"@id": "2",
"name": "",
"author": {
"name": ""
}
}
}
}
And you can nest as you like:
{
"query": {
"book": {
"@id": "2",
"name": "",
"author": {
"name": "",
"books": {
"name": ""
}
}
}
}
}
Below are the most important concepts in G.apex.
A schema is the whole collection of the DataTypes defined. There is usually only one instance of Schema, registered globally to G.apex. The schema defines the graph.
Various data types construct the query system. Simple data types can help build the basic data, while more complicated object types are used everywhere to define objects. Object types are more important in that they offer the main functionality to encapsulate the data and relate with each other. Object types are just like the vertexes in the graph.
Resolving is the process when an object type tries to find its related object types. Basically, object types are supposed to be self-independent and should not concern other object type data. Only when a relationship is required do the object types start resolving the related object types. The resolving function provides the ability to navigate between vertexes in the graph.
Here is an example of the schema and object types:
private static 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());
private static G.ObjectType authorType = new G.ObjectType('Author')
.addField('id', G.StringType)
.addField('name', G.StringType)
.addField('books', new G.ListType(new G.ReferenceType('Book')), new AuthorBooksResolver());
private static G.Schema schema = new G.Schema()
.add(
new G.ObjectType('query')
.addField('books', new G.ListType(bookType), R.constant.apply(new List