Притульский Анатолий
https://github.com/nepster-web/conf-info/tree/master/2017/CodeID-GraphQL
Мультиканальный, комплексный сервис в индустрии путешествий, который работает с такими сегментами рынка как: отели, авиа и ж/д билеты, круизы, трансферы, а также рестораны, концерты и другие виды досуга.
(Application Programming Interface)
Representational State Transfer
«передача состояния представления»
В двух словах, GraphQL это синтаксис, который описывает как запрашивать данные, и, в основном, используется клиентом для загрузки данных с сервера.
Facebook используют GraphQL с 2012 года. А в 2015 году GraphQL стал доступен всем.
https://github.com/APIs-guru/graphql-apis
https://developer.github.com/v4/explorer/
{
hero {
name
friends {
name
}
}
}
{
"data": {
"hero": {
"name": "R2-D2",
"friends": [
{
"name": "Luke Skywalker"
},
{
"name": "Han Solo"
},
{
"name": "Leia Organa"
}
]
}
}
}
{
human(id: "1000") {
name
height(unit: FOOT)
}
}
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 5.6430448
}
}
}
query($id String!) {
human(id: $id) {
name
height(unit: FOOT)
}
}
{"id": "1000"}
{
"data": {
"human": {
"name": "Luke Skywalker",
"height": 5.6430448
}
}
}
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
{
"ep": "JEDI",
"review": {
"stars": 5,
"commentary": "This is a great movie!"
}
}
{
"data": {
"createReview": {
"stars": 5,
"commentary": "This is a great movie!"
}
}
}
query {
echo(message: "Hello World")
}
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'echo' => [
'type' => Type::string(),
'args' => [
'message' => Type::nonNull(Type::string()),
],
'resolve' => function ($root, $args) {
return $root['prefix'] . $args['message'];
}
],
],
]);
graphql.php
use GraphQL\GraphQL;
use GraphQL\Schema;
// Request data
// $query - GET or file_get_contents('php://input')
// $variables - GET or file_get_contents('php://input')
// ------------
$schema = new Schema(['query' => $queryType]);
try {
$rootValue = ['prefix' => 'You said: '];
$result = GraphQL::executeAndReturnResult($schema, $query, $rootValue, null, $variables);
$output = $result->toArray();
} catch (\Exception $e) {
$output = ['errors' => [['message' => $e->getMessage()]]];
}
header('Content-Type: application/json');
echo json_encode($output);
php -S localhost:8000 simple1.php
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
или
http://localhost/simple1.php?query=query{echo(message:"Hello World")}
{
"data": {
"echo": "You said: Hello World"
}
}
https://github.com/nepster-web/conf-info/tree/master/2017/CodeID-GraphQL/demo
https://github.com/graphql/graphiql
https://github.com/nepster-web/conf-info/tree/master/2017/CodeID-GraphQL