Skip to content

Commit

Permalink
added test to query node type w/o id attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissm79 committed May 31, 2016
1 parent 19f1fc4 commit c3483e3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/Queries/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,7 @@ public function itCanExecuteConnectionQuery()
public function itCanResolveNodeInterface()
{
$id = $this->encodeGlobalId(UserType::class, 1);

$query = '{
node(id:"'.$id.'") {
id
Expand All @@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/Support/Database/Factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@
'completed' => 'false'
];
});

$factory->define($namespace.'Post', function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->paragraph
];
});
62 changes: 62 additions & 0 deletions tests/Support/GraphQL/Types/PostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Nuwave\Lighthouse\Tests\Support\GraphQL\Types;

use GraphQL;
use GraphQL\Type\Definition\Type;
use Nuwave\Lighthouse\Support\Definition\GraphQLType;
use Nuwave\Lighthouse\Support\Interfaces\RelayType;
use Nuwave\Lighthouse\Tests\Support\Models\Post;

class PostType extends GraphQLType implements RelayType
{
/**
* Attributes of type.
*
* @var array
*/
protected $attributes = [
'name' => '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.'
]
];
}
}
27 changes: 27 additions & 0 deletions tests/Support/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Nuwave\Lighthouse\Tests\Support\Models;

use Illuminate\Database\Eloquent\Model;
use Nuwave\Lighthouse\Support\Traits\RelayModel;

class Post extends Model
{
use RelayModel;

/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'post_id';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'content', 'user_id',
];
}

0 comments on commit c3483e3

Please sign in to comment.