PHP code example of adeey / graphql-php
1. Go to this page and download the library: Download adeey/graphql-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
adeey / graphql-php example snippets
axGraphQL\Types\Query; // For Query
use MaxGraphQL\Types\Mutation; // For Mutation
$mutation->addSelect('name');
$mutation->addSelect('test');
$mutation->getSelect(); // ['name', 'test']
// $arguments is optional
Mutation::getPreparedQueryFrom('nameOfYourMutation', $selected, $arguments);
Query::getPreparedQueryFrom('nameOfYourQuery', $selected, $arguments);
$query = new Query('HEREYOURNAME');
$arguments = [
'argument' => 'ITS MY ARGUMENT'
];
$select = [
'HERE SELECT'
];
$query->addSelect($select);
$query->addArguments($arguments);
$query->getPreparedQuery(); // and here ours query
use MaxGraphQL\FieldTypes\Enum;
$whatIWantToSelect = [
'id',
'name',
'code',
'password',
'channels' => [
'id',
'titles' => [
'id'
]
],
'... on UserAdmin' => [
'userAdminLevel'
]
];
$filteringArguments = [
'format' => new Enum('ALL'), // if you want write enum values you need to use Enum class
'filter' => [
'activeUsers' => true,
'userIds' => [1,2]
]
];
$query = new Query('users');
$query->addSelect($whatIWantToSelect);
$query->addArguments($filteringArguments);
echo $query->getPreparedQuery(); // returns query string
$whatIWantToSelect = [
'id',
'name',
'code',
'password',
'channels' => [
'id',
'titles' => [
'id'
]
],
'... on UserAdmin' => [
'userAdminLevel'
]
];
$mutationArguments = [
'id' => '321', // look that id is in string format
'data' => [
'name' => 'Test',
'age' => 32, // and the age is int
'admin' => false
]
];
$mutation = new Mutation('updateUser'); // updateUser - name of mutation
$mutation->addSelect($whatIWantToSelect);
$mutation->addArguments($mutationArguments);
echo $mutation->getPreparedQuery(); // returns mutation string
$whatWeNeedToSelect = [
'all(pageSize: 25)' => [
'name',
...
]
];
composer
$mutation->addArguments(['test' => 123]);