GraphQL Playground in general is quite helpful in grasping the GraphQL concepts, it helps to validate the queries and to see what they return. It also helps to deal with errors on the fly so they can quicky be identified and corrected. Everything about it is excellent, but if we want to integrate it into any type of custom logic there needs to be a simple way of accessing it from the Apex itself. And there is.
After installing the MIP package, you can call the method:
static String runQuery(String query, Map<String, Object> variables)
Once you successfuly called the runQuery method and got a response, you can go in any direction with further writing your code.
Here are some parts of the code snippet that can help you jump-start your development.
String idVariable = ...
//Create Map and put variables inside
Map<String, Object> variablesMap = new Map<String, Object>();
variablesMap.put('var',idVariable);
variablesMap.put('toInclude','true');
//Call the method and save response into String
String JSONresponse = mmint.GraphQL.runQuery(getGQLquery(), variablesMap);
//Deserialize result data, errors and extensions into Map
Map<String, Object> resultMap = (Map<String, Object>)JSON.deserializeUntyped(JSONresponse);
Map<String, Object> dataMap = resultMap.get('data');
List<Map<String, Object>> errorList = resultMap.get('errors');
Map<String, Object> extensionsMap = resultMap.get('extensions');
As query parameter in GraphQL.runQuery method is nothing more than a regular String, here is the one way it could possibly be formated as:
private String getGQLquery() {
return
'{ '
+ 'AliasAcc: Account(Id: $accid) { '
+ 'Id '
+ 'Name '
+ 'CreatedById '
+ 'CreatedDate '
+ 'NumberOfEmployees '
+ 'BillingAddress '
+ 'Contacts(first:2) { '
+ 'FirstName '
+ 'LastName '
+ 'AliasOpp: Opportunities { '
+ 'Id '
+ 'Name '
+ '} '
+ '} '
+ 'AccountNumber '
+ '} '
+ 'Lead(first:5 offset:3) { '
+ 'FirstName '
+ '} '
+ '}'
To get more control over errors, we can construct GraphQLError class:
class GraphQLError {
public String message;
public List<GraphQLErrorLocation> locations;
public String errorType;
public String errorSubType;
class GraphQLErrorLocation{
public String line;
public String column;
}
}
and then instead of putting errors into Map, we can deserialize them:
List<GraphQLError> errorList =
Json.deserialize(resultMap.get('errors'), List<GraphQLError>.class);
for (GraphQLError error : errorList) {
String message = error.message;
String locations = error.locations;
String errorType = error.errorType;
String errorSubType = error.errorSubType;
}
Finally, everything put together into an Apex class should look something like this:
public class GraphQLTest {
public static void queryTest() {
String idVariable = '';
Map<String, Object> variablesMap = new Map<String, Object>();
variablesMap.put('var',idVariable);
variablesMap.put('toInclude','true');
String JSONresponse = mmint.GraphQL.runQuery(getGQLquery(), variablesMap);
Map<String, Object> resultMap = (Map<String, Object>)JSON.deserializeUntyped(JSONresponse);
Map<String, Object> dataMap = resultMap.get('data');
List<GraphQLError> errorList = resultMap.get('errors');
Map<String, Object> extensionsMap = resultMap.get('extensions');
}
class GraphQLError {
public String message;
public List<GraphQLErrorLocation> locations;
public String errorType;
public String errorSubType;
class GraphQLErrorLocation{
public String line;
public String column;
}
}
private String getGQLquery() {
return
'{ '
+ 'AliasAcc: Account(Id: $accid) { '
+ 'Id '
+ 'Name '
+ 'CreatedById '
+ 'CreatedDate '
+ 'NumberOfEmployees '
+ 'BillingAddress '
+ 'Contacts(first:2) { '
+ 'FirstName '
+ 'LastName '
+ 'AliasOpp: Opportunities { '
+ 'Id '
+ 'Name '
+ '} '
+ '} '
+ 'AccountNumber '
+ '} '
+ 'Lead(first:5 offset:3) { '
+ 'FirstName '
+ '} '
+ '}'
}
}