graphql.php 999 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. // Test this using following command
  3. // php -S localhost:8080 ./graphql.php &
  4. // curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
  5. // curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
  6. require_once __DIR__ . '/../../vendor/autoload.php';
  7. use GraphQL\GraphQL;
  8. use GraphQL\Utils\BuildSchema;
  9. try {
  10. $schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
  11. $rootValue = include __DIR__ . '/rootvalue.php';
  12. $rawInput = file_get_contents('php://input');
  13. $input = json_decode($rawInput, true);
  14. $query = $input['query'];
  15. $variableValues = isset($input['variables']) ? $input['variables'] : null;
  16. $result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
  17. } catch (\Exception $e) {
  18. $result = [
  19. 'error' => [
  20. 'message' => $e->getMessage()
  21. ]
  22. ];
  23. }
  24. header('Content-Type: application/json; charset=UTF-8');
  25. echo json_encode($result);