Created
October 14, 2019 12:38
-
-
Save jkentjnr/66ae34089883145eb28f719a79ed2e92 to your computer and use it in GitHub Desktop.
Graphql Builder for Salesforce Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GraphBuilder { | |
/* | |
EXAMPLE | |
------- | |
String q = new GraphBuilder.QueryOperation( | |
new GraphBuilder.QueryResolver( | |
'notification', | |
new List<GraphBuilder.QueryResolverParameter> { | |
new GraphBuilder.QueryResolverParameter('limit', 5), | |
new GraphBuilder.QueryResolverParameter('offset', '25'), | |
new GraphBuilder.QueryResolverParameter('filterValue', 'SUCCESS', true) | |
}, | |
new List<GraphBuilder.QueryResolverAttribute> { | |
new GraphBuilder.QueryResolverAttribute('key'), | |
new GraphBuilder.QueryResolverAttribute('message'), | |
new GraphBuilder.QueryResolverAttribute( | |
'executions', | |
new List<GraphBuilder.QueryResolverAttribute> { | |
new GraphBuilder.QueryResolverAttribute('executionKey'), | |
new GraphBuilder.QueryResolverAttribute('description'), | |
new GraphBuilder.QueryResolverAttribute( | |
'owner', | |
new List<GraphBuilder.QueryResolverAttribute> { | |
new GraphBuilder.QueryResolverAttribute('ownerName'), | |
new GraphBuilder.QueryResolverAttribute('email') | |
} | |
) | |
} | |
), | |
new GraphBuilder.QueryResolverAttribute('createdAt') | |
} | |
) | |
).toString(); | |
---- | |
query { | |
notification( | |
limit: 5, | |
offset: 25, | |
filterValue: \"SUCCESS\" | |
){ | |
key | |
message | |
executions { | |
executionKey | |
description | |
owner { | |
ownerName | |
} | |
} | |
createdAt | |
} | |
} | |
*/ | |
public class QueryOperation { | |
private List<QueryResolver> resolvers { get; set; } | |
public QueryOperation(QueryResolver resolver) { | |
this(new List<QueryResolver> { resolver }); | |
} | |
public QueryOperation(List<QueryResolver> resolverList) { | |
this.resolvers = resolverList; | |
} | |
public override String toString() { | |
String response = 'query {\n'; | |
if (this.resolvers != null) { | |
for (QueryResolver resolver : this.resolvers) { | |
response += resolver.toString(); | |
} | |
} | |
response += '}\n'; | |
return response; | |
} | |
} | |
public class QueryResolver { | |
private String name { get; set; } | |
private List<QueryResolverParameter> parameters { get; set; } | |
private List<QueryResolverAttribute> attributes { get; set; } | |
public QueryResolver(String resolverName, List<QueryResolverAttribute> attributeList) { | |
this(resolverName, null, attributeList); | |
} | |
public QueryResolver(String resolverName, List<QueryResolverParameter> parameterList, List<QueryResolverAttribute> attributeList) { | |
this.name = resolverName; | |
this.parameters = parameterList; | |
this.attributes = attributeList; | |
} | |
public override String toString() { | |
String response = ' ' + this.name; | |
if (this.parameters != null) { | |
response += '(\n'; | |
for (Integer i = 0; i < this.parameters.size(); i++) { | |
response += ' ' + this.parameters[i].toString(); | |
if (i != this.parameters.size()-1) response += ','; | |
response += '\n'; | |
} | |
response += ' ){\n'; | |
} | |
else | |
response += ' {\n'; | |
if (this.attributes != null && this.attributes.size() > 0) { | |
for (QueryResolverAttribute attribute : this.attributes) { | |
response += attribute.toString(); | |
} | |
} | |
response += ' }\n'; | |
return response; | |
} | |
} | |
public class QueryResolverParameter { | |
private String name { get; set; } | |
private Object value { get; set; } | |
private Boolean escapeValue { get; set; } | |
public QueryResolverParameter(String parameterName, Object parameterValue) { | |
this(parameterName, parameterValue, false); | |
} | |
public QueryResolverParameter(String parameterName, Object parameterValue, Boolean escapeParameterValue) { | |
this.name = parameterName; | |
this.value = parameterValue; | |
this.escapeValue = escapeParameterValue; | |
} | |
public override String toString() { | |
String responseValue = (this.escapeValue == true) ? '"' + String.valueOf(this.value) + '"' : String.valueOf(this.value); | |
return this.name + ': ' + ((this.escapeValue == true) ? responseValue.escapeEcmaScript() : responseValue); | |
} | |
} | |
public class QueryResolverAttribute { | |
private String name { get; set; } | |
private List<QueryResolverAttribute> children { get; set; } | |
public QueryResolverAttribute(String attributeName) { | |
this(attributeName, null); | |
} | |
public QueryResolverAttribute(String attributeName, List<QueryResolverAttribute> childrenList) { | |
this.name = attributeName; | |
this.children = childrenList; | |
} | |
public override String toString() { | |
return this.toString(1); | |
} | |
public String toString(Integer indent) { | |
String indentation = ''; | |
for (Integer i = 0; i <= indent; i++) { | |
indentation += ' '; | |
} | |
String response = indentation + this.name; | |
if (this.children != null && this.children.size() > 0) { | |
response += ' {\n'; | |
for (QueryResolverAttribute attribute : this.children) { | |
response += attribute.toString(indent + 1); | |
} | |
response += indentation + '}'; | |
} | |
response += '\n'; | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment