2016-12-17 01:14:51 +03:00
|
|
|
<?php
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace GraphQL\Tests\Executor;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
|
|
|
use GraphQL\Deferred;
|
|
|
|
use GraphQL\Executor\Executor;
|
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
2018-09-01 18:07:06 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2017-07-10 15:50:26 +03:00
|
|
|
use GraphQL\Utils\Utils;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-07-25 17:51:01 +03:00
|
|
|
use function count;
|
2018-09-01 18:07:06 +03:00
|
|
|
use function in_array;
|
2018-07-25 17:51:01 +03:00
|
|
|
use function json_encode;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class DeferredFieldsTest extends TestCase
|
2016-12-17 01:14:51 +03:00
|
|
|
{
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var ObjectType */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $userType;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var ObjectType */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $storyType;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var ObjectType */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $categoryType;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var */
|
2018-07-25 17:51:01 +03:00
|
|
|
private $paths;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var mixed[][] */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $storyDataSource;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var mixed[][] */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $userDataSource;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var mixed[][] */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $categoryDataSource;
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
/** @var ObjectType */
|
2016-12-17 01:14:51 +03:00
|
|
|
private $queryType;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->storyDataSource = [
|
|
|
|
['id' => 1, 'authorId' => 1, 'title' => 'Story #1', 'categoryIds' => [2, 3]],
|
|
|
|
['id' => 2, 'authorId' => 2, 'title' => 'Story #2', 'categoryIds' => [1, 2]],
|
|
|
|
['id' => 3, 'authorId' => 3, 'title' => 'Story #3', 'categoryIds' => [2]],
|
|
|
|
['id' => 4, 'authorId' => 3, 'title' => 'Story #4', 'categoryIds' => [1]],
|
|
|
|
['id' => 5, 'authorId' => 1, 'title' => 'Story #5', 'categoryIds' => [3]],
|
|
|
|
['id' => 6, 'authorId' => 2, 'title' => 'Story #6', 'categoryIds' => [1]],
|
|
|
|
['id' => 7, 'authorId' => 3, 'title' => 'Story #7', 'categoryIds' => [2]],
|
|
|
|
['id' => 8, 'authorId' => 1, 'title' => 'Story #8', 'categoryIds' => [1, 2, 3]],
|
|
|
|
['id' => 9, 'authorId' => 2, 'title' => 'Story #9', 'categoryIds' => [2, 3]],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->userDataSource = [
|
|
|
|
['id' => 1, 'name' => 'John', 'bestFriendId' => 4],
|
|
|
|
['id' => 2, 'name' => 'Jane', 'bestFriendId' => 3],
|
|
|
|
['id' => 3, 'name' => 'Joe', 'bestFriendId' => 2],
|
|
|
|
['id' => 4, 'name' => 'Dirk', 'bestFriend' => 1],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->categoryDataSource = [
|
|
|
|
['id' => 1, 'name' => 'Category #1', 'topStoryId' => 8],
|
|
|
|
['id' => 2, 'name' => 'Category #2', 'topStoryId' => 3],
|
2018-09-01 18:07:06 +03:00
|
|
|
['id' => 3, 'name' => 'Category #3', 'topStoryId' => 9],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
|
|
|
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths = [];
|
2016-12-17 01:14:51 +03:00
|
|
|
$this->userType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'User',
|
|
|
|
'fields' => function () {
|
2016-12-17 01:14:51 +03:00
|
|
|
return [
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
2016-12-17 01:14:51 +03:00
|
|
|
'resolve' => function ($user, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return $user['name'];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'bestFriend' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => $this->userType,
|
|
|
|
'resolve' => function ($user, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
return new Deferred(function () use ($user) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = 'deferred-for-best-friend-of-' . $user['id'];
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
return Utils::find(
|
|
|
|
$this->userDataSource,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function ($entry) use ($user) {
|
2018-09-01 18:07:06 +03:00
|
|
|
return $entry['id'] === $user['bestFriendId'];
|
|
|
|
}
|
|
|
|
);
|
2016-12-17 01:14:51 +03:00
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->storyType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Story',
|
2016-12-17 01:14:51 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'title' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($entry, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return $entry['title'];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'author' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => $this->userType,
|
|
|
|
'resolve' => function ($story, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
return new Deferred(function () use ($story) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = 'deferred-for-story-' . $story['id'] . '-author';
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
return Utils::find(
|
|
|
|
$this->userDataSource,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function ($entry) use ($story) {
|
2018-09-01 18:07:06 +03:00
|
|
|
return $entry['id'] === $story['authorId'];
|
|
|
|
}
|
|
|
|
);
|
2016-12-17 01:14:51 +03:00
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->categoryType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Category',
|
2016-12-17 01:14:51 +03:00
|
|
|
'fields' => [
|
|
|
|
'name' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($category, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return $category['name'];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
'stories' => [
|
|
|
|
'type' => Type::listOf($this->storyType),
|
|
|
|
'resolve' => function ($category, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
return Utils::filter(
|
|
|
|
$this->storyDataSource,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function ($story) use ($category) {
|
2018-10-09 17:51:23 +03:00
|
|
|
return in_array($category['id'], $story['categoryIds'], true);
|
2018-09-01 18:07:06 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'topStory' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => $this->storyType,
|
|
|
|
'resolve' => function ($category, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
|
|
|
return new Deferred(function () use ($category) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = 'deferred-for-category-' . $category['id'] . '-topStory';
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
return Utils::find(
|
|
|
|
$this->storyDataSource,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function ($story) use ($category) {
|
2018-09-01 18:07:06 +03:00
|
|
|
return $story['id'] === $category['topStoryId'];
|
|
|
|
}
|
|
|
|
);
|
2016-12-17 01:14:51 +03:00
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->queryType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'Query',
|
2016-12-17 01:14:51 +03:00
|
|
|
'fields' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'topStories' => [
|
|
|
|
'type' => Type::listOf($this->storyType),
|
|
|
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
|
|
|
return Utils::filter(
|
|
|
|
$this->storyDataSource,
|
2018-09-26 12:07:23 +03:00
|
|
|
static function ($story) {
|
2018-09-01 18:07:06 +03:00
|
|
|
return $story['id'] % 2 === 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'featuredCategory' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => $this->categoryType,
|
|
|
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return $this->categoryDataSource[0];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
2018-09-01 18:07:06 +03:00
|
|
|
'categories' => [
|
|
|
|
'type' => Type::listOf($this->categoryType),
|
|
|
|
'resolve' => function ($val, $args, $context, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return $this->categoryDataSource;
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDeferredFields() : void
|
2016-12-17 01:14:51 +03:00
|
|
|
{
|
|
|
|
$query = Parser::parse('
|
|
|
|
{
|
|
|
|
topStories {
|
|
|
|
title
|
|
|
|
author {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
featuredCategory {
|
|
|
|
stories {
|
|
|
|
title
|
|
|
|
author {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$schema = new Schema([
|
2018-09-01 18:07:06 +03:00
|
|
|
'query' => $this->queryType,
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'topStories' => [
|
2016-12-17 01:14:51 +03:00
|
|
|
['title' => 'Story #1', 'author' => ['name' => 'John']],
|
|
|
|
['title' => 'Story #3', 'author' => ['name' => 'Joe']],
|
|
|
|
['title' => 'Story #5', 'author' => ['name' => 'John']],
|
|
|
|
['title' => 'Story #7', 'author' => ['name' => 'Joe']],
|
|
|
|
['title' => 'Story #9', 'author' => ['name' => 'Jane']],
|
|
|
|
],
|
|
|
|
'featuredCategory' => [
|
|
|
|
'stories' => [
|
|
|
|
['title' => 'Story #2', 'author' => ['name' => 'Jane']],
|
|
|
|
['title' => 'Story #4', 'author' => ['name' => 'Joe']],
|
|
|
|
['title' => 'Story #6', 'author' => ['name' => 'Jane']],
|
|
|
|
['title' => 'Story #8', 'author' => ['name' => 'John']],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$result = Executor::execute($schema, $query);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result->toArray());
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-07-25 17:51:01 +03:00
|
|
|
$expectedPaths = [
|
2016-12-17 01:14:51 +03:00
|
|
|
['topStories'],
|
|
|
|
['topStories', 0, 'title'],
|
|
|
|
['topStories', 0, 'author'],
|
|
|
|
['topStories', 1, 'title'],
|
|
|
|
['topStories', 1, 'author'],
|
|
|
|
['topStories', 2, 'title'],
|
|
|
|
['topStories', 2, 'author'],
|
|
|
|
['topStories', 3, 'title'],
|
|
|
|
['topStories', 3, 'author'],
|
|
|
|
['topStories', 4, 'title'],
|
|
|
|
['topStories', 4, 'author'],
|
|
|
|
['featuredCategory'],
|
|
|
|
['featuredCategory', 'stories'],
|
|
|
|
['featuredCategory', 'stories', 0, 'title'],
|
|
|
|
['featuredCategory', 'stories', 0, 'author'],
|
|
|
|
['featuredCategory', 'stories', 1, 'title'],
|
|
|
|
['featuredCategory', 'stories', 1, 'author'],
|
|
|
|
['featuredCategory', 'stories', 2, 'title'],
|
|
|
|
['featuredCategory', 'stories', 2, 'author'],
|
|
|
|
['featuredCategory', 'stories', 3, 'title'],
|
|
|
|
['featuredCategory', 'stories', 3, 'author'],
|
|
|
|
'deferred-for-story-1-author',
|
|
|
|
'deferred-for-story-3-author',
|
|
|
|
'deferred-for-story-5-author',
|
|
|
|
'deferred-for-story-7-author',
|
|
|
|
'deferred-for-story-9-author',
|
|
|
|
'deferred-for-story-2-author',
|
|
|
|
'deferred-for-story-4-author',
|
|
|
|
'deferred-for-story-6-author',
|
|
|
|
'deferred-for-story-8-author',
|
|
|
|
['topStories', 0, 'author', 'name'],
|
|
|
|
['topStories', 1, 'author', 'name'],
|
|
|
|
['topStories', 2, 'author', 'name'],
|
|
|
|
['topStories', 3, 'author', 'name'],
|
|
|
|
['topStories', 4, 'author', 'name'],
|
|
|
|
['featuredCategory', 'stories', 0, 'author', 'name'],
|
|
|
|
['featuredCategory', 'stories', 1, 'author', 'name'],
|
|
|
|
['featuredCategory', 'stories', 2, 'author', 'name'],
|
|
|
|
['featuredCategory', 'stories', 3, 'author', 'name'],
|
|
|
|
];
|
2018-07-25 17:51:01 +03:00
|
|
|
self::assertCount(count($expectedPaths), $this->paths);
|
|
|
|
foreach ($expectedPaths as $expectedPath) {
|
|
|
|
self::assertTrue(in_array($expectedPath, $this->paths, true), 'Missing path: ' . json_encode($expectedPath));
|
|
|
|
}
|
2016-12-17 01:14:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testNestedDeferredFields() : void
|
2016-12-17 01:14:51 +03:00
|
|
|
{
|
|
|
|
$query = Parser::parse('
|
|
|
|
{
|
|
|
|
categories {
|
|
|
|
name
|
|
|
|
topStory {
|
|
|
|
title
|
|
|
|
author {
|
|
|
|
name
|
|
|
|
bestFriend {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$schema = new Schema([
|
2018-09-01 18:07:06 +03:00
|
|
|
'query' => $this->queryType,
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$author1 = ['name' => 'John', 'bestFriend' => ['name' => 'Dirk']];
|
|
|
|
$author2 = ['name' => 'Jane', 'bestFriend' => ['name' => 'Joe']];
|
2018-09-01 18:07:06 +03:00
|
|
|
$author3 = ['name' => 'Joe', 'bestFriend' => ['name' => 'Jane']];
|
2016-12-17 01:14:51 +03:00
|
|
|
$author4 = ['name' => 'Dirk', 'bestFriend' => ['name' => 'John']];
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'data' => [
|
|
|
|
'categories' => [
|
|
|
|
['name' => 'Category #1', 'topStory' => ['title' => 'Story #8', 'author' => $author1]],
|
|
|
|
['name' => 'Category #2', 'topStory' => ['title' => 'Story #3', 'author' => $author3]],
|
|
|
|
['name' => 'Category #3', 'topStory' => ['title' => 'Story #9', 'author' => $author2]],
|
2018-09-01 18:07:06 +03:00
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$result = Executor::execute($schema, $query);
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result->toArray());
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-07-25 17:51:01 +03:00
|
|
|
$expectedPaths = [
|
2016-12-17 01:14:51 +03:00
|
|
|
['categories'],
|
|
|
|
['categories', 0, 'name'],
|
|
|
|
['categories', 0, 'topStory'],
|
|
|
|
['categories', 1, 'name'],
|
|
|
|
['categories', 1, 'topStory'],
|
|
|
|
['categories', 2, 'name'],
|
|
|
|
['categories', 2, 'topStory'],
|
|
|
|
'deferred-for-category-1-topStory',
|
|
|
|
'deferred-for-category-2-topStory',
|
|
|
|
'deferred-for-category-3-topStory',
|
|
|
|
['categories', 0, 'topStory', 'title'],
|
|
|
|
['categories', 0, 'topStory', 'author'],
|
|
|
|
['categories', 1, 'topStory', 'title'],
|
|
|
|
['categories', 1, 'topStory', 'author'],
|
|
|
|
['categories', 2, 'topStory', 'title'],
|
|
|
|
['categories', 2, 'topStory', 'author'],
|
|
|
|
'deferred-for-story-8-author',
|
|
|
|
'deferred-for-story-3-author',
|
|
|
|
'deferred-for-story-9-author',
|
|
|
|
['categories', 0, 'topStory', 'author', 'name'],
|
|
|
|
['categories', 0, 'topStory', 'author', 'bestFriend'],
|
|
|
|
['categories', 1, 'topStory', 'author', 'name'],
|
|
|
|
['categories', 1, 'topStory', 'author', 'bestFriend'],
|
|
|
|
['categories', 2, 'topStory', 'author', 'name'],
|
|
|
|
['categories', 2, 'topStory', 'author', 'bestFriend'],
|
|
|
|
'deferred-for-best-friend-of-1',
|
|
|
|
'deferred-for-best-friend-of-3',
|
|
|
|
'deferred-for-best-friend-of-2',
|
|
|
|
['categories', 0, 'topStory', 'author', 'bestFriend', 'name'],
|
|
|
|
['categories', 1, 'topStory', 'author', 'bestFriend', 'name'],
|
|
|
|
['categories', 2, 'topStory', 'author', 'bestFriend', 'name'],
|
|
|
|
];
|
2018-07-25 17:51:01 +03:00
|
|
|
self::assertCount(count($expectedPaths), $this->paths);
|
|
|
|
foreach ($expectedPaths as $expectedPath) {
|
|
|
|
self::assertTrue(in_array($expectedPath, $this->paths, true), 'Missing path: ' . json_encode($expectedPath));
|
|
|
|
}
|
2016-12-17 01:14:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testComplexRecursiveDeferredFields() : void
|
2016-12-17 01:14:51 +03:00
|
|
|
{
|
|
|
|
$complexType = new ObjectType([
|
2018-09-01 18:07:06 +03:00
|
|
|
'name' => 'ComplexType',
|
|
|
|
'fields' => function () use (&$complexType) {
|
2016-12-17 01:14:51 +03:00
|
|
|
return [
|
2018-09-01 18:07:06 +03:00
|
|
|
'sync' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return 'sync';
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
2018-09-01 18:07:06 +03:00
|
|
|
'deferred' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
return new Deferred(function () use ($info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = ['!dfd for: ', $info->path];
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return 'deferred';
|
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
2018-09-01 18:07:06 +03:00
|
|
|
'nest' => [
|
|
|
|
'type' => $complexType,
|
|
|
|
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return [];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'deferredNest' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'type' => $complexType,
|
|
|
|
'resolve' => function ($v, $a, $c, ResolveInfo $info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = $info->path;
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
return new Deferred(function () use ($info) {
|
2018-07-25 17:51:01 +03:00
|
|
|
$this->paths[] = ['!dfd nest for: ', $info->path];
|
2018-09-01 18:07:06 +03:00
|
|
|
|
2016-12-17 01:14:51 +03:00
|
|
|
return [];
|
|
|
|
});
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
2018-09-01 18:07:06 +03:00
|
|
|
},
|
2016-12-17 01:14:51 +03:00
|
|
|
]);
|
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$schema = new Schema(['query' => $complexType]);
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-09-01 18:07:06 +03:00
|
|
|
$query = Parser::parse('
|
2016-12-17 01:14:51 +03:00
|
|
|
{
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
}
|
|
|
|
deferredNest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deferredNest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
nest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
}
|
|
|
|
deferredNest {
|
|
|
|
sync
|
|
|
|
deferred
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
2018-09-01 18:07:06 +03:00
|
|
|
$result = Executor::execute($schema, $query);
|
2016-12-17 01:14:51 +03:00
|
|
|
$expected = [
|
|
|
|
'data' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'nest' => [
|
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
|
|
|
'nest' => [
|
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'deferredNest' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'deferredNest' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
|
|
|
'nest' => [
|
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
2016-12-17 01:14:51 +03:00
|
|
|
],
|
|
|
|
'deferredNest' => [
|
2018-09-01 18:07:06 +03:00
|
|
|
'sync' => 'sync',
|
|
|
|
'deferred' => 'deferred',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2016-12-17 01:14:51 +03:00
|
|
|
];
|
|
|
|
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals($expected, $result->toArray());
|
2016-12-17 01:14:51 +03:00
|
|
|
|
2018-07-25 17:51:01 +03:00
|
|
|
$expectedPaths = [
|
2016-12-17 01:14:51 +03:00
|
|
|
['nest'],
|
|
|
|
['nest', 'sync'],
|
|
|
|
['nest', 'deferred'],
|
|
|
|
['nest', 'nest'],
|
|
|
|
['nest', 'nest', 'sync'],
|
|
|
|
['nest', 'nest', 'deferred'],
|
|
|
|
['nest', 'deferredNest'],
|
|
|
|
['deferredNest'],
|
|
|
|
|
|
|
|
['!dfd for: ', ['nest', 'deferred']],
|
|
|
|
['!dfd for: ', ['nest', 'nest', 'deferred']],
|
|
|
|
['!dfd nest for: ', ['nest', 'deferredNest']],
|
|
|
|
['!dfd nest for: ', ['deferredNest']],
|
|
|
|
|
|
|
|
['nest', 'deferredNest', 'sync'],
|
|
|
|
['nest', 'deferredNest', 'deferred'],
|
|
|
|
['deferredNest', 'sync'],
|
|
|
|
['deferredNest', 'deferred'],
|
|
|
|
['deferredNest', 'nest'],
|
|
|
|
['deferredNest', 'nest', 'sync'],
|
|
|
|
['deferredNest', 'nest', 'deferred'],
|
|
|
|
['deferredNest', 'deferredNest'],
|
|
|
|
|
|
|
|
['!dfd for: ', ['nest', 'deferredNest', 'deferred']],
|
|
|
|
['!dfd for: ', ['deferredNest', 'deferred']],
|
|
|
|
['!dfd for: ', ['deferredNest', 'nest', 'deferred']],
|
|
|
|
['!dfd nest for: ', ['deferredNest', 'deferredNest']],
|
|
|
|
|
|
|
|
['deferredNest', 'deferredNest', 'sync'],
|
|
|
|
['deferredNest', 'deferredNest', 'deferred'],
|
|
|
|
['!dfd for: ', ['deferredNest', 'deferredNest', 'deferred']],
|
|
|
|
];
|
|
|
|
|
2018-07-25 17:51:01 +03:00
|
|
|
self::assertCount(count($expectedPaths), $this->paths);
|
|
|
|
foreach ($expectedPaths as $expectedPath) {
|
|
|
|
self::assertTrue(in_array($expectedPath, $this->paths, true), 'Missing path: ' . json_encode($expectedPath));
|
|
|
|
}
|
2016-12-17 01:14:51 +03:00
|
|
|
}
|
|
|
|
}
|