Skip to content

Commit 606b1c8

Browse files
committed
GQL-12: Added examples on how to use the mutation class to run queries
- Added an example file for mutations - Added an example file for raw queries - Added examples for both mutations and raw queries in the README file
1 parent 3b6fe25 commit 606b1c8

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
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']);

0 commit comments

Comments
 (0)