andrew72ru / gql-query-builder
GraphGL query builder
Installs: 3 565
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
Requires (Dev)
- codeception/codeception: ^2.3
- friendsofphp/php-cs-fixer: ^2.10
This package is auto-updated.
Last update: 2024-10-23 02:53:08 UTC
README
Attention!
The library does not include any http-clients implementations and intended only for build valid queries for some GraphQL servers. The correctness of requests depends of specific GraphQL server implementation.
Installation
composer require andrew72ru/gql-query-builder
Usage
Example query with parameters and variables:
query MyAwesomeScheme($pools: [String!], $objects: [String!], $yesterday: DateTime) {
today: values(pools: $pools, objects: $objects) {
object {
object
expirationDate
}
time
myParam
myOtherParam
}
yesterday: values(pools: $pools, objects: $objects, time: $yesterday) {
object {
object
expirationDate
}
myThirdParam
}
}
The code above returns this query as string
public function createMyQuery()
{
$builder = new Builder();
$builder->setName('MyAwesomeScheme');
$builder->addQueryParam('pools', Builder::TYPE_STRING, true, true)
->addQueryParam('objects', Builder::TYPE_STRING, true, true)
->addQueryParam('yesterday', Builder::TYPE_DATE_TIME, false, false);
$bodyToday = new QueryBody($builder);
$bodyToday->setName('values')
->setVariableName('today')
->addBodyPart(['object' => ['object', 'expirationDate']])
->addBodyPart('time')
->addBodyPart('myParam')
->addBodyPart('myOtherParam');
$bodyToday->addNameParam('pools', 'pools')
->addNameParam('bjects', 'bjects');
$bodyYesterday = new QueryBody($builder);
$bodyYesterday->setName('values')
->setVariableName('yesterday');
$body = [
'object' => [
'object',
'expirationDate',
],
'myThirdParam',
];
$bodyYesterday->setBody($body);
$params = [
[
'name' => 'pools',
'type' => 'pools',
],
[
'name' => 'values',
'type' => 'values',
],
[
'name' => 'time',
'type' => 'yesterday',
]
];
$bodyYesterday->setNameParams($params);
$builder->setBody($bodyToday)->addBodyPart($bodyYesterday);
return $builder->build();
// Or You may use __toString() implementation
// return (string) $builder;
}
Simply query without parameters and variables
query MyAwesomeScheme {
values {
object {
expirationDate
}
time
}
}
Code:
public function createSimpleQuery()
{
$builder = new Builder();
$body = new QueryBody($builder);
$body->setName('quotes')
->setBody([
'symbol' => ['expirationDate'],
'time',
]);
$builder->setBody($body)
->setName('TradingSchema');
return (string) $builder;
}
Contributing
You are welcome! :)
Testing
Tests are implemented with Codeception framework.
- Clone this repository;
- install requirements (
composer install --dev
); - run tests:
vendor/bin/codecept run --coverage-html