-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |