Skip to content

Commit 4165c4d

Browse files
committed
added support for operation
1 parent 19f840d commit 4165c4d

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
public class GraphQLNode {
22
public String id;
3+
public String operation; //optional query, mutation, subscription
34
public Boolean typeFrament; // transforms to "...on {id} {"
45
public String alias;
56
public GraphQLArgument[] args; //not sure this supports all use cases... might need to be GraphQLArgument | GraphQLArgument[]
67
public Object[] children; //object or other nodes
8+
79

810
public GraphQLNode(String id){
911
this.typeFrament = false;
@@ -18,6 +20,12 @@ public class GraphQLNode {
1820
return this;
1921
}
2022

23+
//returns the current node
24+
public GraphQLNode setOperation(String operation){
25+
this.operation = operation;
26+
return this;
27+
}
28+
2129
//returns the current node
2230
public GraphQLNode setAlias(String alias){
2331
this.alias = alias;
@@ -52,7 +60,12 @@ public class GraphQLNode {
5260

5361
public string build(){
5462
String qry = this.id;
55-
if(this.typeFrament){
63+
64+
//we should probably refactor to different classes
65+
//which all extend GraphQlNode (OperationNode, FragmentNode, etc)
66+
if(operation != null){
67+
qry = operation + ' ' + qry;
68+
}else if(this.typeFrament){
5669
qry = '... on ' + this.id;
5770
}else if(this.alias != null){
5871
qry = this.alias + ': ' + qry;

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
public class GraphQLQuery {
22
public string query;
3-
public string variables;
4-
public GraphQLQuery(string query, string variables){
3+
public Object variables;
4+
public GraphQLQuery(string query, Object variables){
55
this.query = query;
66
this.variables = variables;
77
}
88

99
public GraphQLQuery(GraphQLNode node, Object variables){
1010
this.query = buildQuery(node);
11-
if(variables != null){
12-
this.variables = JSON.serialize(variables);
13-
}
11+
this.variables = variables;
1412
}
1513

1614
public GraphQLQuery(GraphQLNode[] nodes, Object variables){
1715
this.query = buildQuery(nodes);
18-
if(variables != null){
19-
this.variables = JSON.serialize(variables);
20-
}
16+
this.variables = variables;
2117
}
2218

2319
private static string buildQuery(GraphQLNode node){
20+
if(node.operation != null){
21+
return node.build();
22+
}
2423
return '{\n' + node.build() + '\n}';
2524
}
2625

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ public class GraphQLQueryTests {
9090
System.assertEquals('{empireHero: hero(episode:EMPIRE){name}jediHero: hero(episode:JEDI){name}}', qry.query.remove('\n'));
9191
}
9292

93+
94+
95+
// mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
96+
// createReview(episode: $ep, review: $review) {
97+
// stars
98+
// commentary
99+
// }
100+
// }
101+
@isTest
102+
private static void testOperation(){
103+
GraphQLNode node = new GraphQLNode('CreateReviewForEpisode')
104+
.setOperation('mutation')
105+
.addArguments(new GraphQLArgument('$ep', 'Episode!', true))
106+
.addArguments(new GraphQLArgument('$review', 'ReviewInput!', true))
107+
.add(
108+
new GraphQLNode('createReview')
109+
.addArguments(new GraphQLArgument[]{
110+
new GraphQLArgument('episode', '$ep', true),
111+
new GraphQLArgument('review', '$review', true)
112+
})
113+
.add(new Object[]{'stars', 'commentary'})
114+
);
115+
116+
TestVariables test = new TestVariables();
117+
GraphQLQuery qry = new GraphQLQuery(node, test);
118+
System.assertEquals(
119+
'mutation CreateReviewForEpisode($ep:Episode!, $review:ReviewInput!){ createReview(episode:$ep, review:$review){ stars commentary } }',
120+
qry.query.replace('\n', ' ').trim()
121+
);
122+
}
123+
124+
93125
private class TestVariables {
94126
public String foo;
95127
public Decimal bar;

0 commit comments

Comments
 (0)