mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 06:16:05 +03:00
Added hello world example
This commit is contained in:
parent
96e4f01ee6
commit
1787c1377d
18
examples/00-hello-world/README.md
Normal file
18
examples/00-hello-world/README.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Hello world
|
||||||
|
Clean and simple single-file example of main GraphQL concepts originally proposed and
|
||||||
|
implemented by [Leo Cavalcante](https://github.com/leocavalcante)
|
||||||
|
|
||||||
|
### Run locally
|
||||||
|
```
|
||||||
|
php -S localhost:8080 ./graphql.php
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try query
|
||||||
|
```
|
||||||
|
curl http://localhost:8080 -d "query { echo(message: \"Hello World\") }"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try mutation
|
||||||
|
```
|
||||||
|
curl http://localhost:8080 -d "mutation { sum(x: 2, y: 2) }"
|
||||||
|
```
|
61
examples/00-hello-world/graphql.php
Normal file
61
examples/00-hello-world/graphql.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
// Test this using following command
|
||||||
|
// php -S localhost:8080 ./graphql.php
|
||||||
|
require_once '../../vendor/autoload.php';
|
||||||
|
|
||||||
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
|
use GraphQL\Type\Definition\Type;
|
||||||
|
use GraphQL\Schema;
|
||||||
|
use GraphQL\GraphQL;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$queryType = new ObjectType([
|
||||||
|
'name' => 'Query',
|
||||||
|
'fields' => [
|
||||||
|
'echo' => [
|
||||||
|
'type' => Type::string(),
|
||||||
|
'args' => [
|
||||||
|
'message' => ['type' => Type::string()],
|
||||||
|
],
|
||||||
|
'resolve' => function ($root, $args) {
|
||||||
|
return $root['prefix'].$args['message'];
|
||||||
|
}
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$mutationType = new ObjectType([
|
||||||
|
'name' => 'Calc',
|
||||||
|
'fields' => [
|
||||||
|
'sum' => [
|
||||||
|
'type' => Type::int(),
|
||||||
|
'args' => [
|
||||||
|
'x' => ['type' => Type::int()],
|
||||||
|
'y' => ['type' => Type::int()],
|
||||||
|
],
|
||||||
|
'resolve' => function ($root, $args) {
|
||||||
|
return $args['x'] + $args['y'];
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$schema = new Schema([
|
||||||
|
'query' => $queryType,
|
||||||
|
'mutation' => $mutationType,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$rawInput = file_get_contents('php://input');
|
||||||
|
|
||||||
|
$rootValue = ['prefix' => 'You said: '];
|
||||||
|
$result = GraphQL::execute($schema, $rawInput, $rootValue);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$result = [
|
||||||
|
'error' => [
|
||||||
|
'message' => $e->getMessage()
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
header('Content-Type: application/json; charset=UTF-8');
|
||||||
|
echo json_encode($result);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user