Skip to content

Commit

Permalink
GQL-12: Added examples on how to use the mutation class to run queries
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mghoneimy committed Apr 26, 2019
1 parent 3b6fe25 commit 606b1c8
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ $results = $client->runQuery($gql, true);
$results->getData()['Company'][1]['branches']['address']
```

# Mutations
Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
can have sub-fields.

Here's a sample example on how to construct and run mutations:
```
$mutation = (new Mutation('createCompany'))
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
->setSelectionSet(
[
'_id',
'name',
'serialNumber',
]
);
$results = $client->runQuery($mutation);
```
Mutations can be run by the client the same way queries are run.

# Live API Example
GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
the web, we'll use it to demo the capabilities of this client.
Expand Down Expand Up @@ -265,3 +284,27 @@ catch (QueryError $exception) {
}
print_r($results->getData()['pokemon']);
```

# Running Raw Queries
Although not the primary goal of this package, but it supports running raw string queries, just like any other client
using the `runRawQuery` method in the `Client` class. Here's an example on how to use it:
```
$gql = <<<QUERY
query {
pokemon(name: "Pikachu") {
id
number
name
attacks {
special {
name
type
damage
}
}
}
}
QUERY;
$results = $client->runQuery($gql);
```
47 changes: 47 additions & 0 deletions examples/mutation_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use GraphQL\Client;
use GraphQL\Exception\QueryError;
use GraphQL\Mutation;
use GraphQL\RawObject;

// Create Client object to contact the GraphQL endpoint
$client = new Client(
'api_url',
[] // Replace with array of extra headers to be sent with request for auth or other purposes
);


// Create the GraphQL mutation
$gql = (new Mutation('createCompany'))
->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
->setSelectionSet(
[
'_id',
'name',
'serialNumber',
]
);

// Run query to get results
try {
$results = $client->runQuery($gql);
}
catch (QueryError $exception) {

// Catch query error and desplay error details
print_r($exception->getErrorDetails());
exit;
}

// Display original response from endpoint
var_dump($results->getResponseObject());

// Display part of the returned results of the object
var_dump($results->getData()->pokemon);

// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['pokemon']);
50 changes: 50 additions & 0 deletions examples/raw_query_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use GraphQL\Client;
use GraphQL\Exception\QueryError;

// Create Client object to contact the GraphQL endpoint
$client = new Client(
'https://graphql-pokemon.now.sh/',
[] // Replace with array of extra headers to be sent with request for auth or other purposes
);

$gql = <<<QUERY
query {
pokemon(name: "Pikachu") {
id
number
name
attacks {
special {
name
type
damage
}
}
}
}
QUERY;

// Run query to get results
try {
$results = $client->runRawQuery($gql);
}
catch (QueryError $exception) {

// Catch query error and desplay error details
print_r($exception->getErrorDetails());
exit;
}

// Display original response from endpoint
var_dump($results->getResponseObject());

// Display part of the returned results of the object
var_dump($results->getData()->pokemon);

// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['pokemon']);

0 comments on commit 606b1c8

Please sign in to comment.