File tree Expand file tree Collapse file tree 4 files changed +123
-4
lines changed Expand file tree Collapse file tree 4 files changed +123
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace GraphQL ;
4
+
5
+ /**
6
+ * Class Mutation
7
+ *
8
+ * @package GraphQL
9
+ */
10
+ class Mutation extends Query
11
+ {
12
+ /**
13
+ * Stores the name of the type of the operation to be executed on the GraphQL server
14
+ *
15
+ * @var string
16
+ */
17
+ protected const OPERATION_TYPE = 'mutation ' ;
18
+ }
Original file line number Diff line number Diff line change @@ -18,7 +18,14 @@ class Query
18
18
*
19
19
* @var string
20
20
*/
21
- private const QUERY_FORMAT = "%s%s { \n%s \n} " ;
21
+ protected const QUERY_FORMAT = "%s%s { \n%s \n} " ;
22
+
23
+ /**
24
+ * Stores the name of the type of the operation to be executed on the GraphQL server
25
+ *
26
+ * @var string
27
+ */
28
+ protected const OPERATION_TYPE = 'query ' ;
22
29
23
30
/**
24
31
* Stores the object being queried for
@@ -181,8 +188,8 @@ protected function constructSelectionSet(): string
181
188
public function __toString ()
182
189
{
183
190
$ queryFormat = static ::QUERY_FORMAT ;
184
- if (!$ this ->isNested && $ this ->object !== ' query ' ) {
185
- $ queryFormat = " query { \n" . static ::QUERY_FORMAT . "\n} " ;
191
+ if (!$ this ->isNested && $ this ->object !== static :: OPERATION_TYPE ) {
192
+ $ queryFormat = static :: OPERATION_TYPE . " { \n" . static ::QUERY_FORMAT . "\n} " ;
186
193
}
187
194
$ argumentsString = $ this ->constructArguments ();
188
195
$ selectionSetString = $ this ->constructSelectionSet ();
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace GraphQL \Tests ;
4
+
5
+ use GraphQL \Mutation ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ class MutationTest extends TestCase
9
+ {
10
+ /**
11
+ *
12
+ */
13
+ public function testMutationWithoutOperationType ()
14
+ {
15
+ $ mutation = new Mutation ('createObject ' );
16
+
17
+ $ this ->assertEquals (
18
+ 'mutation {
19
+ createObject {
20
+
21
+ }
22
+ } ' ,
23
+ (string ) $ mutation
24
+ );
25
+ }
26
+
27
+ /**
28
+ *
29
+ */
30
+ public function testMutationWithOperationType ()
31
+ {
32
+ $ mutation = new Mutation ('mutation ' );
33
+
34
+ $ this ->assertEquals (
35
+ 'mutation {
36
+
37
+ } ' ,
38
+ (string ) $ mutation
39
+ );
40
+ }
41
+
42
+ /**
43
+ *
44
+ */
45
+ public function testMutationWithFields ()
46
+ {
47
+ $ mutation = (new Mutation ('createObject ' ))
48
+ ->setSelectionSet (
49
+ [
50
+ 'fieldOne ' ,
51
+ 'fieldTwo ' ,
52
+ ]
53
+ );
54
+
55
+ $ this ->assertEquals (
56
+ 'mutation {
57
+ createObject {
58
+ fieldOne
59
+ fieldTwo
60
+ }
61
+ } ' ,
62
+ (string ) $ mutation
63
+ );
64
+ }
65
+
66
+ /**
67
+ *
68
+ */
69
+ public function testMutationWithArgumentsAndFields ()
70
+ {
71
+ $ mutation = (new Mutation ('createObject ' ))
72
+ ->setSelectionSet (
73
+ [
74
+ 'fieldOne ' ,
75
+ 'fieldTwo ' ,
76
+ ]
77
+ )->setArguments (
78
+ [
79
+ 'argOne ' => 1 ,
80
+ 'argTwo ' => 'val '
81
+ ]
82
+ );
83
+
84
+ $ this ->assertEquals (
85
+ 'mutation {
86
+ createObject(argOne: 1 argTwo: "val") {
87
+ fieldOne
88
+ fieldTwo
89
+ }
90
+ } ' ,
91
+ (string ) $ mutation
92
+ );
93
+ }
94
+ }
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ public function testEmptyArguments(Query $query)
46
46
/**
47
47
* @covers \GraphQL\Query::__toString
48
48
*/
49
- public function testExplicitQueryObject ()
49
+ public function testQueryWithOperationType ()
50
50
{
51
51
$ query = new Query ('query ' );
52
52
You can’t perform that action at this time.
0 commit comments