2016-05-02 00:42:05 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Executor;
|
|
|
|
|
|
|
|
use GraphQL\GraphQL;
|
2018-05-27 15:13:32 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2016-05-02 00:42:05 +03:00
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
|
2016-10-18 18:20:51 +03:00
|
|
|
require_once __DIR__ . '/TestClasses.php';
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-10-18 18:20:51 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ResolveTest extends TestCase
|
2016-05-02 00:42:05 +03:00
|
|
|
{
|
|
|
|
// Execute: resolve function
|
|
|
|
|
|
|
|
private function buildSchema($testField)
|
|
|
|
{
|
|
|
|
return new Schema([
|
|
|
|
'query' => new ObjectType([
|
|
|
|
'name' => 'Query',
|
|
|
|
'fields' => [
|
|
|
|
'test' => $testField
|
|
|
|
]
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('default function accesses properties')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
|
|
|
public function testDefaultFunctionAccessesProperties()
|
|
|
|
{
|
|
|
|
$schema = $this->buildSchema(['type' => Type::string()]);
|
|
|
|
|
|
|
|
$source = [
|
|
|
|
'test' => 'testValue'
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => 'testValue']],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test }', $source)->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('default function calls methods')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
2017-07-08 10:56:15 +03:00
|
|
|
public function testDefaultFunctionCallsClosures()
|
2016-05-02 00:42:05 +03:00
|
|
|
{
|
|
|
|
$schema = $this->buildSchema(['type' => Type::string()]);
|
|
|
|
$_secret = 'secretValue' . uniqid();
|
|
|
|
|
|
|
|
$source = [
|
2017-07-08 10:56:15 +03:00
|
|
|
'test' => function() use ($_secret) {
|
2016-05-02 00:42:05 +03:00
|
|
|
return $_secret;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => $_secret]],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test }', $source)->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-18 18:20:51 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('default function passes args and context')
|
2016-10-18 18:20:51 +03:00
|
|
|
*/
|
|
|
|
public function testDefaultFunctionPassesArgsAndContext()
|
|
|
|
{
|
|
|
|
$schema = $this->buildSchema([
|
|
|
|
'type' => Type::int(),
|
|
|
|
'args' => [
|
|
|
|
'addend1' => [ 'type' => Type::int() ],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$source = new Adder(700);
|
|
|
|
|
2018-05-27 15:13:32 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, '{ test(addend1: 80) }', $source, ['addend2' => 9])->toArray();
|
2016-10-18 18:20:51 +03:00
|
|
|
$this->assertEquals(['data' => ['test' => 789]], $result);
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('uses provided resolve function')
|
2016-05-02 00:42:05 +03:00
|
|
|
*/
|
|
|
|
public function testUsesProvidedResolveFunction()
|
|
|
|
{
|
|
|
|
$schema = $this->buildSchema([
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
|
|
|
'aStr' => ['type' => Type::string()],
|
|
|
|
'aInt' => ['type' => Type::int()],
|
|
|
|
],
|
|
|
|
'resolve' => function ($source, $args) {
|
|
|
|
return json_encode([$source, $args]);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => '[null,[]]']],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test }')->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => '["Source!",[]]']],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test }', 'Source!')->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => '["Source!",{"aStr":"String!"}]']],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test(aStr: "String!") }', 'Source!')->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
['data' => ['test' => '["Source!",{"aStr":"String!","aInt":-123}]']],
|
2018-05-27 15:13:32 +03:00
|
|
|
GraphQL::executeQuery($schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!')->toArray()
|
2016-05-02 00:42:05 +03:00
|
|
|
);
|
|
|
|
}
|
2017-05-08 01:21:20 +03:00
|
|
|
}
|