Merge branch 'master' of https://github.com/webonyx/graphql-php into v0.10

This commit is contained in:
Vladimir Razuvaev 2017-07-18 00:42:52 +07:00
commit 8e3d1eb29b
7 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,19 @@
# Parsing GraphQL IDL shorthand
Same as the Hello world example but shows how to build GraphQL schema from shorthand
and wire up some resolvers
### Run locally
```
php -S localhost:8080 ./graphql.php
```
### Try query
```
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
```
### Try mutation
```
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
```

View File

@ -0,0 +1,31 @@
<?php
// Test this using following command
// php -S localhost:8080 ./graphql.php &
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
require_once __DIR__ . '/../../vendor/autoload.php';
use GraphQL\GraphQL;
use GraphQL\Utils\BuildSchema;
try {
$schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
$rootValue = include __DIR__ . '/rootvalue.php';
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
} catch (\Exception $e) {
$result = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($result);

View File

@ -0,0 +1,35 @@
<?php
interface Resolver {
public function resolve($root, $args, $context);
}
class Addition implements Resolver
{
public function resolve($root, $args, $context)
{
return $args['x'] + $args['y'];
}
}
class Echoer implements Resolver
{
public function resolve($root, $args, $context)
{
return $root['prefix'].$args['message'];
}
}
return [
'sum' => function($root, $args, $context) {
$sum = new Addition();
return $sum->resolve($root, $args, $context);
},
'echo' => function($root, $args, $context) {
$echo = new Echoer();
return $echo->resolve($root, $args, $context);
},
'prefix' => 'You said: ',
];

View File

@ -0,0 +1,13 @@
schema {
query: Query
mutation: Calc
}
type Calc {
sum(x: Int, y: Int): Int
}
type Query {
echo(message: String): String
}

View File

@ -1,8 +1,10 @@
<?php <?php
namespace GraphQL\Type\Definition; namespace GraphQL\Type\Definition;
use GraphQL\Error\UserError;
use GraphQL\Language\AST\IntValueNode; use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\StringValueNode;
use GraphQL\Utils;
/** /**
* Class IDType * Class IDType
@ -46,6 +48,12 @@ When expected as an input type, any string (such as `"4"`) or integer
if ($value === false) { if ($value === false) {
return 'false'; return 'false';
} }
if ($value === null) {
return 'null';
}
if (!is_scalar($value)) {
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
}
return (string) $value; return (string) $value;
} }

View File

@ -1,7 +1,9 @@
<?php <?php
namespace GraphQL\Type\Definition; namespace GraphQL\Type\Definition;
use GraphQL\Error\UserError;
use GraphQL\Language\AST\StringValueNode; use GraphQL\Language\AST\StringValueNode;
use GraphQL\Utils;
/** /**
* Class StringType * Class StringType
@ -43,6 +45,12 @@ represent free-form human-readable text.';
if ($value === false) { if ($value === false) {
return 'false'; return 'false';
} }
if ($value === null) {
return 'null';
}
if (!is_scalar($value)) {
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
}
return (string) $value; return (string) $value;
} }

View File

@ -144,6 +144,21 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
$this->assertSame('-1.1', $stringType->serialize(-1.1)); $this->assertSame('-1.1', $stringType->serialize(-1.1));
$this->assertSame('true', $stringType->serialize(true)); $this->assertSame('true', $stringType->serialize(true));
$this->assertSame('false', $stringType->serialize(false)); $this->assertSame('false', $stringType->serialize(false));
$this->assertSame('null', $stringType->serialize(null));
try {
$stringType->serialize([]);
$this->fail('Expected exception was not thrown');
} catch (UserError $e) {
$this->assertEquals('String cannot represent non scalar value: array', $e->getMessage());
}
try {
$stringType->serialize(new \stdClass());
$this->fail('Expected exception was not thrown');
} catch (UserError $e) {
$this->assertEquals('String cannot represent non scalar value: instance of stdClass', $e->getMessage());
}
} }
/** /**