Skip to content

Commit 3b6fe25

Browse files
committed
GQL-12: Added support for generating mutation operations
- Added the Mutation class - Added tests for the mutation class - Modified the query class to accomodate the introduction of the Mutation
1 parent e9c0980 commit 3b6fe25

File tree

4 files changed

+123
-4
lines changed

4 files changed

+123
-4
lines changed

src/Mutation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

src/Query.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ class Query
1818
*
1919
* @var string
2020
*/
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';
2229

2330
/**
2431
* Stores the object being queried for
@@ -181,8 +188,8 @@ protected function constructSelectionSet(): string
181188
public function __toString()
182189
{
183190
$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}";
186193
}
187194
$argumentsString = $this->constructArguments();
188195
$selectionSetString = $this->constructSelectionSet();

tests/MutationTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

tests/QueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testEmptyArguments(Query $query)
4646
/**
4747
* @covers \GraphQL\Query::__toString
4848
*/
49-
public function testExplicitQueryObject()
49+
public function testQueryWithOperationType()
5050
{
5151
$query = new Query('query');
5252

0 commit comments

Comments
 (0)