Skip to content

Commit e4a27f2

Browse files
committed
Merge branch 'feature/GQL-12' into develop
2 parents e9c0980 + 606b1c8 commit e4a27f2

File tree

7 files changed

+263
-4
lines changed

7 files changed

+263
-4
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ $results = $client->runQuery($gql, true);
151151
$results->getData()['Company'][1]['branches']['address']
152152
```
153153

154+
# Mutations
155+
Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
156+
can have sub-fields.
157+
158+
Here's a sample example on how to construct and run mutations:
159+
```
160+
$mutation = (new Mutation('createCompany'))
161+
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
162+
->setSelectionSet(
163+
[
164+
'_id',
165+
'name',
166+
'serialNumber',
167+
]
168+
);
169+
$results = $client->runQuery($mutation);
170+
```
171+
Mutations can be run by the client the same way queries are run.
172+
154173
# Live API Example
155174
GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
156175
the web, we'll use it to demo the capabilities of this client.
@@ -265,3 +284,27 @@ catch (QueryError $exception) {
265284
}
266285
print_r($results->getData()['pokemon']);
267286
```
287+
288+
# Running Raw Queries
289+
Although not the primary goal of this package, but it supports running raw string queries, just like any other client
290+
using the `runRawQuery` method in the `Client` class. Here's an example on how to use it:
291+
```
292+
$gql = <<<QUERY
293+
query {
294+
pokemon(name: "Pikachu") {
295+
id
296+
number
297+
name
298+
attacks {
299+
special {
300+
name
301+
type
302+
damage
303+
}
304+
}
305+
}
306+
}
307+
QUERY;
308+
309+
$results = $client->runQuery($gql);
310+
```

examples/mutation_example.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use GraphQL\Client;
6+
use GraphQL\Exception\QueryError;
7+
use GraphQL\Mutation;
8+
use GraphQL\RawObject;
9+
10+
// Create Client object to contact the GraphQL endpoint
11+
$client = new Client(
12+
'api_url',
13+
[] // Replace with array of extra headers to be sent with request for auth or other purposes
14+
);
15+
16+
17+
// Create the GraphQL mutation
18+
$gql = (new Mutation('createCompany'))
19+
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
20+
->setSelectionSet(
21+
[
22+
'_id',
23+
'name',
24+
'serialNumber',
25+
]
26+
);
27+
28+
// Run query to get results
29+
try {
30+
$results = $client->runQuery($gql);
31+
}
32+
catch (QueryError $exception) {
33+
34+
// Catch query error and desplay error details
35+
print_r($exception->getErrorDetails());
36+
exit;
37+
}
38+
39+
// Display original response from endpoint
40+
var_dump($results->getResponseObject());
41+
42+
// Display part of the returned results of the object
43+
var_dump($results->getData()->pokemon);
44+
45+
// Reformat the results to an array and get the results of part of the array
46+
$results->reformatResults(true);
47+
print_r($results->getData()['pokemon']);

examples/raw_query_example.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use GraphQL\Client;
6+
use GraphQL\Exception\QueryError;
7+
8+
// Create Client object to contact the GraphQL endpoint
9+
$client = new Client(
10+
'https://graphql-pokemon.now.sh/',
11+
[] // Replace with array of extra headers to be sent with request for auth or other purposes
12+
);
13+
14+
$gql = <<<QUERY
15+
query {
16+
pokemon(name: "Pikachu") {
17+
id
18+
number
19+
name
20+
attacks {
21+
special {
22+
name
23+
type
24+
damage
25+
}
26+
}
27+
}
28+
}
29+
QUERY;
30+
31+
// Run query to get results
32+
try {
33+
$results = $client->runRawQuery($gql);
34+
}
35+
catch (QueryError $exception) {
36+
37+
// Catch query error and desplay error details
38+
print_r($exception->getErrorDetails());
39+
exit;
40+
}
41+
42+
// Display original response from endpoint
43+
var_dump($results->getResponseObject());
44+
45+
// Display part of the returned results of the object
46+
var_dump($results->getData()->pokemon);
47+
48+
// Reformat the results to an array and get the results of part of the array
49+
$results->reformatResults(true);
50+
print_r($results->getData()['pokemon']);

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)