adeey / graphql-php
GraphQL php library
Installs: 13 904
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 4
Open Issues: 0
Requires
- php: >=5.5
Requires (Dev)
- phpunit/phpunit: ^9.6
README
This library can build a ready to use query/mutation string from php array
Installation
composer require adeey/graphql-php
Classes
<?php require 'vendor/autoload.php'; use MaxGraphQL\Types\Query; // For Query use MaxGraphQL\Types\Mutation; // For Mutation
Steps to use
Steps to use:
-
Create new class object
$mutation = new Mutation('name');
with mutation/query name -
Add what you want to select
$mutation->addSelect(['test', 'name']);
2.1 Or you can pass a one name of field
$mutation->addSelect('name'); $mutation->addSelect('test'); $mutation->getSelect(); // ['name', 'test']
-
Add arguments(filters) to your query
$mutation->addArguments(['test' => 123]);
-
Get the builded query
$mutation->getPreparedQuery();
-
Use the string in your request
OR
You can build string by calling static method:
// $arguments is optional Mutation::getPreparedQueryFrom('nameOfYourMutation', $selected, $arguments); Query::getPreparedQueryFrom('nameOfYourQuery', $selected, $arguments);
both of these methods will return string
Methods
Methods that can be called from object
- To return current selected fields
$object->getSelect()
- To return current arguments
$object->getArguments()
Where is arguments/select?
query { HEREYOURNAME( argument: "ITS MY ARGUMENT" ) { HERE SELECT } }
php code of example:
$query = new Query('HEREYOURNAME'); $arguments = [ 'argument' => 'ITS MY ARGUMENT' ]; $select = [ 'HERE SELECT' ]; $query->addSelect($select); $query->addArguments($arguments); $query->getPreparedQuery(); // and here ours query
How to build a query like this? Its pretty easy
query { users( format: ALL, filter: { activeUsers: true, userIds: [1,2] } ) { id name code password channels { id titles { id } } ... on UserAdmin { userAdminLevel } } }
PHP code:
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
The result of PHP code is string of query that equals my GraphQL query and its generated from PHP arrays:
query{users(format:ALL,filter:{activeUsers:true,userIds:[1,2]}){id,name,code,password,channels{id,titles{id}},... on UserAdmin{userAdminLevel}}}
How to build a mutation like this?
mutation { updateUser( id: "321", data: { name: "Test", age: 32, admin: false } ) { id name code password channels { id titles { id } } ... on UserAdmin { userAdminLevel } } }
PHP code:
$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
Result mutation in string
mutation{updateUser(id:"321",data:{name:"Test",age:32,admin:false}){id,name,code,password,channels{id,titles{id}},... on UserAdmin{userAdminLevel}}}
Additional cases:
Sometimes we want to add extra filter to query like this:
query { users { all(pageSize: 25) { name ... } } }
We need to write like that:
$whatWeNeedToSelect = [ 'all(pageSize: 25)' => [ 'name', ... ] ];