mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 04:46:04 +03:00
Merge branch 'master' of https://github.com/webonyx/graphql-php into v0.10
This commit is contained in:
commit
8e3d1eb29b
19
examples/02-shorthand/README.md
Normal file
19
examples/02-shorthand/README.md
Normal 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) }" }'
|
||||
```
|
31
examples/02-shorthand/graphql.php
Normal file
31
examples/02-shorthand/graphql.php
Normal 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);
|
||||
|
35
examples/02-shorthand/rootvalue.php
Normal file
35
examples/02-shorthand/rootvalue.php
Normal 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: ',
|
||||
];
|
13
examples/02-shorthand/schema.graphqls
Normal file
13
examples/02-shorthand/schema.graphqls
Normal file
@ -0,0 +1,13 @@
|
||||
schema {
|
||||
query: Query
|
||||
mutation: Calc
|
||||
}
|
||||
|
||||
type Calc {
|
||||
sum(x: Int, y: Int): Int
|
||||
}
|
||||
|
||||
type Query {
|
||||
echo(message: String): String
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
<?php
|
||||
namespace GraphQL\Type\Definition;
|
||||
|
||||
use GraphQL\Error\UserError;
|
||||
use GraphQL\Language\AST\IntValueNode;
|
||||
use GraphQL\Language\AST\StringValueNode;
|
||||
use GraphQL\Utils;
|
||||
|
||||
/**
|
||||
* Class IDType
|
||||
@ -46,6 +48,12 @@ When expected as an input type, any string (such as `"4"`) or integer
|
||||
if ($value === 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;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace GraphQL\Type\Definition;
|
||||
|
||||
use GraphQL\Error\UserError;
|
||||
use GraphQL\Language\AST\StringValueNode;
|
||||
use GraphQL\Utils;
|
||||
|
||||
/**
|
||||
* Class StringType
|
||||
@ -43,6 +45,12 @@ represent free-form human-readable text.';
|
||||
if ($value === 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;
|
||||
}
|
||||
|
||||
|
@ -144,6 +144,21 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame('-1.1', $stringType->serialize(-1.1));
|
||||
$this->assertSame('true', $stringType->serialize(true));
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user