From c3483e30efe9aafaa3707fd0b9f6108e73da6af8 Mon Sep 17 00:00:00 2001 From: Christopher Moore Date: Tue, 31 May 2016 08:03:07 -0700 Subject: [PATCH] added test to query node type w/o id attribute --- tests/Queries/QueryTest.php | 33 ++++++++++ .../Database/Factories/ModelFactory.php | 7 +++ tests/Support/GraphQL/Types/PostType.php | 62 +++++++++++++++++++ tests/Support/Models/Post.php | 27 ++++++++ 4 files changed, 129 insertions(+) create mode 100644 tests/Support/GraphQL/Types/PostType.php create mode 100644 tests/Support/Models/Post.php diff --git a/tests/Queries/QueryTest.php b/tests/Queries/QueryTest.php index aabf5e61ee..3849663694 100644 --- a/tests/Queries/QueryTest.php +++ b/tests/Queries/QueryTest.php @@ -6,6 +6,7 @@ use Nuwave\Lighthouse\Support\Definition\GraphQLQuery; use Nuwave\Lighthouse\Tests\Support\Models\User; use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\UserType; +use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\PostType; use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\TaskType; use Nuwave\Lighthouse\Tests\Support\GraphQL\Queries\UserQuery; use Nuwave\Lighthouse\Tests\TestCase; @@ -75,6 +76,7 @@ public function itCanExecuteConnectionQuery() public function itCanResolveNodeInterface() { $id = $this->encodeGlobalId(UserType::class, 1); + $query = '{ node(id:"'.$id.'") { id @@ -99,4 +101,35 @@ public function itCanResolveNodeInterface() $data = $this->executeQuery($query)['data']; $this->assertEquals($expected, $data); } + + /** + * @test + * @group failing + */ + public function itCanResolveNodeWithoutRegularIdAttribute() + { + $id = $this->encodeGlobalId(PostType::class, 1); + + $query = '{ + node(id:"'.$id.'") { + id + ... on Post { + title + } + } + }'; + + $expected = [ + 'node' => [ + 'id' => $id, + 'title' => 'Foobar', + ], + ]; + + $graphql = app('graphql'); + $graphql->schema()->type('post', PostType::class); + + $data = $this->executeQuery($query)['data']; + $this->assertEquals($expected, $data); + } } diff --git a/tests/Support/Database/Factories/ModelFactory.php b/tests/Support/Database/Factories/ModelFactory.php index cae5f67ba8..ac572ce4b0 100644 --- a/tests/Support/Database/Factories/ModelFactory.php +++ b/tests/Support/Database/Factories/ModelFactory.php @@ -16,3 +16,10 @@ 'completed' => 'false' ]; }); + +$factory->define($namespace.'Post', function (Faker\Generator $faker) { + return [ + 'title' => $faker->sentence, + 'content' => $faker->paragraph + ]; +}); diff --git a/tests/Support/GraphQL/Types/PostType.php b/tests/Support/GraphQL/Types/PostType.php new file mode 100644 index 0000000000..c9d1335959 --- /dev/null +++ b/tests/Support/GraphQL/Types/PostType.php @@ -0,0 +1,62 @@ + 'Post', + 'description' => 'A post that does not have a regular id field.' + ]; + + /** + * Get model by id. + * + * Note: When the root 'node' query is called, this method + * will be used to resolve the type by providing the id. + * + * @param mixed $id + * @return mixed + */ + public function resolveById($id) + { + return factory(Post::class)->make([ + 'post_id' => $id, + 'title' => 'Foobar', + ]); + } + + /** + * Type fields. + * + * @return array + */ + public function fields() + { + return [ + 'title' => [ + 'type' => Type::string(), + 'description' => 'Title of the post.' + ], + 'content' => [ + 'type' => Type::string(), + 'description' => 'Content of the post.' + ], + 'user_id' => [ + 'type' => Type::int(), + 'description' => 'ID of user who wrote the post.' + ] + ]; + } +} diff --git a/tests/Support/Models/Post.php b/tests/Support/Models/Post.php new file mode 100644 index 0000000000..a114e5286a --- /dev/null +++ b/tests/Support/Models/Post.php @@ -0,0 +1,27 @@ +