This library can build a ready to use query/mutation string from php array
composer require adeey/graphql-php
<?php
require 'vendor/autoload.php';
use MaxGraphQL\Types\Query; // For Query
use MaxGraphQL\Types\Mutation; // For Mutation
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
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 that can be called from object
- To return current selected fields
$object->getSelect()
- To return current arguments
$object->getArguments()
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
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}}}
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
mutation{updateUser(id:"321",data:{name:"Test",age:32,admin:false}){id,name,code,password,channels{id,titles{id}},... on UserAdmin{userAdminLevel}}}
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',
...
]
];