Skip to content

Commit 91ae6a8

Browse files
committed
added alias and support for unescaped string (variables)
1 parent a0cd417 commit 91ae6a8

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

force-app/main/default/classes/GraphQLArgument.cls

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
public class GraphQLArgument{
22
public String name;
33
public Object value; //string | GraphQLArgument | GraphQLArgument[]
4+
public Boolean isVariable;
45

56
public GraphQLArgument(String name, Object value){
67
this.name = name;
78
this.value = value;
9+
this.isVariable = false;
10+
}
11+
12+
public GraphQLArgument(String name, Object value, Boolean isVariable){
13+
this.name = name;
14+
this.value = value;
15+
this.isVariable = isVariable;
816
}
917

1018
public string build(){
1119
String qry = this.name + ':';
12-
if(this.value instanceOf Integer || this.value instanceOf Decimal){
20+
if(isVariable || this.value instanceOf Integer || this.value instanceOf Decimal){
1321
qry += String.valueOf(this.value);
1422
}else if(this.value instanceOf DateTime){
1523
qry += '\"' + ((DateTime) this.value).format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'') + '\"';
@@ -21,7 +29,7 @@ public class GraphQLArgument{
2129
qry += childParam.build();
2230
}
2331
qry += '}';
24-
}else{ // default to string
32+
}else { // default to string
2533
qry += '\"' + String.valueOf(this.value) + '\"';
2634
}
2735
return qry;

force-app/main/default/classes/GraphQLNode.cls

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
public class GraphQLNode {
22
public String id;
33
public Boolean typeFrament; // transforms to "...on {id} {"
4+
public String alias;
45
public GraphQLArgument arg; //not sure this supports all use cases... might need to be GraphQLArgument | GraphQLArgument[]
56
public Object[] children; //object or other nodes
67

@@ -16,6 +17,12 @@ public class GraphQLNode {
1617
return this;
1718
}
1819

20+
//returns the current node
21+
public GraphQLNode setAlias(String alias){
22+
this.alias = alias;
23+
return this;
24+
}
25+
1926
//returns the current node
2027
public GraphQLNode setArguments(GraphQLArgument arg){
2128
this.arg = arg;
@@ -37,10 +44,12 @@ public class GraphQLNode {
3744
return this;
3845
}
3946

40-
private string buildInner(){
47+
public string build(){
4148
String qry = this.id;
4249
if(this.typeFrament){
4350
qry = '... on ' + this.id;
51+
}else if(this.alias != null){
52+
qry = this.alias + ': ' + qry;
4453
}
4554

4655
if(this.arg != null){
@@ -49,7 +58,7 @@ public class GraphQLNode {
4958
qry += '{\n';
5059
for(Object child : this.children){
5160
if(child instanceOf GraphQLNode){
52-
qry += ((GraphQLNode) child).buildInner();
61+
qry += ((GraphQLNode) child).build();
5362
}else{
5463
qry += ((String) child) + '\n';
5564
}
@@ -58,9 +67,4 @@ public class GraphQLNode {
5867
return qry;
5968
}
6069

61-
62-
public string build(){
63-
return '{\n' + this.buildInner() + '\n}';
64-
}
65-
6670
}

force-app/main/default/classes/GraphQLQuery.cls

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@ public class GraphQLQuery {
77
}
88

99
public GraphQLQuery(GraphQLNode node, Object variables){
10-
this.query = node.build();
10+
this.query = buildQuery(node);
1111
if(variables != null){
1212
this.variables = JSON.serialize(variables);
1313
}
1414
}
15+
16+
public GraphQLQuery(GraphQLNode[] nodes, Object variables){
17+
this.query = buildQuery(nodes);
18+
if(variables != null){
19+
this.variables = JSON.serialize(variables);
20+
}
21+
}
22+
23+
private static string buildQuery(GraphQLNode node){
24+
return '{\n' + node.build() + '\n}';
25+
}
26+
27+
private static string buildQuery(GraphQLNode[] nodes){
28+
String[] nodeStrings = new String[]{};
29+
for(GraphQLNode node : nodes){
30+
nodeStrings.add(node.build());
31+
}
32+
return '{\n' + String.join(nodeStrings, '\n') + '\n}';
33+
}
1534
}

force-app/main/default/classes/GraphQLQueryTests.cls

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class GraphQLQueryTests {
1919
private static void simpleNode(){
2020
GraphQLNode n = new GraphQLNode('hello')
2121
.add('world');
22-
System.assertEquals('{hello{world}}', n.build().deleteWhitespace());
22+
System.assertEquals('hello{world}', n.build().deleteWhitespace());
2323
}
2424

2525
@isTest
2626
private static void testSimpleNodeInput(){
2727
GraphQLNode n = new GraphQLNode('hello')
2828
.setArguments(new GraphQLArgument('key', 'value'))
2929
.add('world');
30-
System.assertEquals('{hello(key:\"value\"){world}}', n.build().deleteWhitespace());
30+
System.assertEquals('hello(key:\"value\"){world}', n.build().deleteWhitespace());
3131
}
3232

3333
@isTest
@@ -36,7 +36,7 @@ public class GraphQLQueryTests {
3636
.add(
3737
new GraphQLNode('hello').add('world')
3838
);
39-
System.assertEquals('{message{hello{world}}}', n.build().deleteWhitespace());
39+
System.assertEquals('message{hello{world}}', n.build().deleteWhitespace());
4040
}
4141

4242
@isTest
@@ -47,11 +47,28 @@ public class GraphQLQueryTests {
4747
.setTypeFragment(true)
4848
.add('x')
4949
});
50-
System.assertEquals('{foo{... on bar{x}}}', n.build().remove('\n'));
50+
System.assertEquals('foo{... on bar{x}}', n.build().remove('\n'));
51+
}
52+
53+
@isTest
54+
private static void testAlias(){
55+
GraphQLNode[] nodes = new GraphQLNode[]{
56+
new GraphQLNode('hero')
57+
.setAlias('empireHero')
58+
.setArguments(new GraphQLArgument('episode', 'EMPIRE', true))
59+
.add('name'),
60+
new GraphQLNode('hero')
61+
.setAlias('jediHero')
62+
.setArguments(new GraphQLArgument('episode', 'JEDI', true))
63+
.add('name')
64+
};
65+
66+
GraphQLQuery qry = new GraphQLQuery(nodes, null);
67+
System.assertEquals('{empireHero: hero(episode:EMPIRE){name}jediHero: hero(episode:JEDI){name}}', qry.query.remove('\n'));
5168
}
5269

5370
private class TestVariables {
5471
public String foo;
5572
public Decimal bar;
5673
}
57-
}
74+
}

0 commit comments

Comments
 (0)