graphql-php/tests/GraphQLTest.php
2019-06-02 20:49:10 +02:00

48 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace GraphQL\Tests;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use function sprintf;
class GraphQLTest extends TestCase
{
public function testPromiseToExecute() : void
{
$promiseAdapter = new SyncPromiseAdapter();
$schema = new Schema(
[
'query' => new ObjectType(
[
'name' => 'Query',
'fields' => [
'sayHi' => [
'type' => Type::nonNull(Type::string()),
'args' => [
'name' => [
'type' => Type::nonNull(Type::string()),
],
],
'resolve' => static function ($value, $args) use ($promiseAdapter) {
return $promiseAdapter->createFulfilled(sprintf('Hi %s!', $args['name']));
},
],
],
]
),
]
);
$promise = GraphQL::promiseToExecute($promiseAdapter, $schema, '{ sayHi(name: "John") }');
$result = $promiseAdapter->wait($promise);
$this->assertSame(['data' => ['sayHi' => 'Hi John!']], $result->toArray());
}
}