Skip to content

Commit 8e3d1eb

Browse files
committed
Merge branch 'master' of https://github.com/webonyx/graphql-php into v0.10
2 parents 49208d7 + 8fe26a1 commit 8e3d1eb

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

examples/02-shorthand/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Parsing GraphQL IDL shorthand
2+
3+
Same as the Hello world example but shows how to build GraphQL schema from shorthand
4+
and wire up some resolvers
5+
6+
### Run locally
7+
```
8+
php -S localhost:8080 ./graphql.php
9+
```
10+
11+
### Try query
12+
```
13+
curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
14+
```
15+
16+
### Try mutation
17+
```
18+
curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
19+
```

examples/02-shorthand/graphql.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
8+
use GraphQL\GraphQL;
9+
use GraphQL\Utils\BuildSchema;
10+
11+
try {
12+
13+
$schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
14+
$rootValue = include __DIR__ . '/rootvalue.php';
15+
16+
$rawInput = file_get_contents('php://input');
17+
$input = json_decode($rawInput, true);
18+
$query = $input['query'];
19+
$variableValues = isset($input['variables']) ? $input['variables'] : null;
20+
21+
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
22+
} catch (\Exception $e) {
23+
$result = [
24+
'error' => [
25+
'message' => $e->getMessage()
26+
]
27+
];
28+
}
29+
header('Content-Type: application/json; charset=UTF-8');
30+
echo json_encode($result);
31+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
interface Resolver {
4+
public function resolve($root, $args, $context);
5+
}
6+
7+
class Addition implements Resolver
8+
{
9+
public function resolve($root, $args, $context)
10+
{
11+
return $args['x'] + $args['y'];
12+
}
13+
}
14+
15+
class Echoer implements Resolver
16+
{
17+
public function resolve($root, $args, $context)
18+
{
19+
return $root['prefix'].$args['message'];
20+
}
21+
}
22+
23+
return [
24+
'sum' => function($root, $args, $context) {
25+
$sum = new Addition();
26+
27+
return $sum->resolve($root, $args, $context);
28+
},
29+
'echo' => function($root, $args, $context) {
30+
$echo = new Echoer();
31+
32+
return $echo->resolve($root, $args, $context);
33+
},
34+
'prefix' => 'You said: ',
35+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
schema {
2+
query: Query
3+
mutation: Calc
4+
}
5+
6+
type Calc {
7+
sum(x: Int, y: Int): Int
8+
}
9+
10+
type Query {
11+
echo(message: String): String
12+
}
13+

src/Type/Definition/IDType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\UserError;
45
use GraphQL\Language\AST\IntValueNode;
56
use GraphQL\Language\AST\StringValueNode;
7+
use GraphQL\Utils;
68

79
/**
810
* Class IDType
@@ -46,6 +48,12 @@ public function parseValue($value)
4648
if ($value === false) {
4749
return 'false';
4850
}
51+
if ($value === null) {
52+
return 'null';
53+
}
54+
if (!is_scalar($value)) {
55+
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
56+
}
4957
return (string) $value;
5058
}
5159

src/Type/Definition/StringType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace GraphQL\Type\Definition;
33

4+
use GraphQL\Error\UserError;
45
use GraphQL\Language\AST\StringValueNode;
6+
use GraphQL\Utils;
57

68
/**
79
* Class StringType
@@ -43,6 +45,12 @@ public function parseValue($value)
4345
if ($value === false) {
4446
return 'false';
4547
}
48+
if ($value === null) {
49+
return 'null';
50+
}
51+
if (!is_scalar($value)) {
52+
throw new UserError("String cannot represent non scalar value: " . Utils::printSafe($value));
53+
}
4654
return (string) $value;
4755
}
4856

tests/Type/ScalarSerializationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ public function testSerializesOutputStrings()
144144
$this->assertSame('-1.1', $stringType->serialize(-1.1));
145145
$this->assertSame('true', $stringType->serialize(true));
146146
$this->assertSame('false', $stringType->serialize(false));
147+
$this->assertSame('null', $stringType->serialize(null));
148+
149+
try {
150+
$stringType->serialize([]);
151+
$this->fail('Expected exception was not thrown');
152+
} catch (UserError $e) {
153+
$this->assertEquals('String cannot represent non scalar value: array', $e->getMessage());
154+
}
155+
156+
try {
157+
$stringType->serialize(new \stdClass());
158+
$this->fail('Expected exception was not thrown');
159+
} catch (UserError $e) {
160+
$this->assertEquals('String cannot represent non scalar value: instance of stdClass', $e->getMessage());
161+
}
147162
}
148163

149164
/**

0 commit comments

Comments
 (0)