Skip to content

Commit 19f840d

Browse files
committed
#1 sibling arg support
1 parent 91ae6a8 commit 19f840d

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public class GraphQLArgument{
1717

1818
public string build(){
1919
String qry = this.name + ':';
20-
if(isVariable || this.value instanceOf Integer || this.value instanceOf Decimal){
20+
if(isVariable || this.value instanceOf Integer || this.value instanceOf Decimal || this.value instanceOf Boolean){
2121
qry += String.valueOf(this.value);
2222
}else if(this.value instanceOf DateTime){
2323
qry += '\"' + ((DateTime) this.value).format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'') + '\"';
2424
}else if(this.value instanceOf GraphQLArgument){
2525
qry += '{' + ((GraphQLArgument) this.value).build() + '}';
2626
}else if(this.value instanceOf GraphQLArgument[]){
27-
qry += '{';
28-
for(GraphQLArgument childParam : (GraphQLArgument[]) this.value){
29-
qry += childParam.build();
27+
String[] argsStrings = new String[]{};
28+
for(GraphQLArgument arg : (GraphQLArgument[]) this.value){
29+
argsStrings.add(arg.build());
3030
}
31-
qry += '}';
31+
qry += '{' + String.join(argsStrings, ', ') + '}';
3232
}else { // default to string
3333
qry += '\"' + String.valueOf(this.value) + '\"';
3434
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ public class GraphQLNode {
22
public String id;
33
public Boolean typeFrament; // transforms to "...on {id} {"
44
public String alias;
5-
public GraphQLArgument arg; //not sure this supports all use cases... might need to be GraphQLArgument | GraphQLArgument[]
5+
public GraphQLArgument[] args; //not sure this supports all use cases... might need to be GraphQLArgument | GraphQLArgument[]
66
public Object[] children; //object or other nodes
77

88
public GraphQLNode(String id){
99
this.typeFrament = false;
10+
this.args = new GraphQLArgument[]{};
1011
this.children = new Object[]{};
1112
this.id = id;
1213
}
@@ -24,8 +25,13 @@ public class GraphQLNode {
2425
}
2526

2627
//returns the current node
27-
public GraphQLNode setArguments(GraphQLArgument arg){
28-
this.arg = arg;
28+
public GraphQLNode addArguments(GraphQLArgument arg){
29+
this.args.add(arg);
30+
return this;
31+
}
32+
33+
public GraphQLNode addArguments(GraphQLArgument[] args){
34+
this.args.addAll(args);
2935
return this;
3036
}
3137

@@ -52,9 +58,14 @@ public class GraphQLNode {
5258
qry = this.alias + ': ' + qry;
5359
}
5460

55-
if(this.arg != null){
56-
qry += '(' + this.arg.build() + ')';
61+
if(this.args.size() > 0){
62+
String[] argsStrings = new String[]{};
63+
for(GraphQLArgument arg : this.args){
64+
argsStrings.add(arg.build());
65+
}
66+
qry += '(' + String.join(argsStrings, ', ') + ')';
5767
}
68+
5869
qry += '{\n';
5970
for(Object child : this.children){
6071
if(child instanceOf GraphQLNode){

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ public class GraphQLQueryTests {
44
@isTest
55
private static void readmeExample(){
66
GraphQLNode human = new GraphQLNode('human')
7-
.setArguments(new GraphQLArgument('id', '1000'))
7+
.addArguments(new GraphQLArgument('id', '1000'))
88
.add(new Object[]{
99
'name',
1010
'height',
1111
new GraphQLNode('address')
1212
.add(new Object[]{ 'city', 'country' })
1313
});
14-
String qry = human.build();
14+
GraphQLQuery qry = new GraphQLQuery(human, null);
1515
}
1616

1717

@@ -23,13 +23,36 @@ public class GraphQLQueryTests {
2323
}
2424

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

33+
@isTest
34+
private static void testChildArgs(){
35+
GraphQLNode n = new GraphQLNode('hello')
36+
.addArguments(new GraphQLArgument('input', new GraphQLArgument[]{
37+
new GraphQLArgument('key1', 'value1'),
38+
new GraphQLArgument('key2', 'value2')
39+
}))
40+
.add('world');
41+
System.assertEquals('hello(input:{key1:\"value1\", key2:\"value2\"}){world}', n.build().remove('\n'));
42+
}
43+
44+
@isTest
45+
private static void testMultipleTopLevelArgs(){
46+
GraphQLNode n = new GraphQLNode('hero')
47+
.addArguments(new GraphQLArgument[]{
48+
new GraphQLArgument('episode', 'EMPIRE', true),
49+
new GraphQLArgument('robot', true)
50+
})
51+
.add('name');
52+
53+
System.assertEquals('hero(episode:EMPIRE, robot:true){name}', n.build().remove('\n'));
54+
}
55+
3356
@isTest
3457
private static void testChildNode(){
3558
GraphQLNode n = new GraphQLNode('message')
@@ -55,11 +78,11 @@ public class GraphQLQueryTests {
5578
GraphQLNode[] nodes = new GraphQLNode[]{
5679
new GraphQLNode('hero')
5780
.setAlias('empireHero')
58-
.setArguments(new GraphQLArgument('episode', 'EMPIRE', true))
81+
.addArguments(new GraphQLArgument('episode', 'EMPIRE', true))
5982
.add('name'),
6083
new GraphQLNode('hero')
6184
.setAlias('jediHero')
62-
.setArguments(new GraphQLArgument('episode', 'JEDI', true))
85+
.addArguments(new GraphQLArgument('episode', 'JEDI', true))
6386
.add('name')
6487
};
6588

0 commit comments

Comments
 (0)