Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow alias to be a callback #452

Merged
merged 7 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CHANGELOG

[Next release](https://github.com/rebing/graphql-laravel/compare/2.0.0...master)
--------------
### Added
- Allow `'alias'` to be a callback [\#452 / crissi](https://github.com/rebing/graphql-laravel/pull/452)

2019-08-05, 2.0.0
-----------------
Expand Down
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class UserType extends GraphQLType
'description' => 'The id of the user',
// Use 'alias', if the database column is different from the type name.
// This is supported for discrete values as well as relations.
// You can also use `DB::raw()` to solve more complex issues
// - you can also use `DB::raw()` to solve more complex issues
// - or a callback returning the value (string or `DB::raw()` result)
'alias' => 'user_id',
],
'email' => [
Expand Down
1 change: 1 addition & 0 deletions src/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ protected static function handleFields(array $queryArgs, array $requestedFields,
$key = isset($fieldObject->config['alias'])
? $fieldObject->config['alias']
: $key;
$key = $key instanceof Closure ? $key() : $key;

self::addFieldToSelect($key, $select, $parentTable, false);

Expand Down
24 changes: 18 additions & 6 deletions tests/Database/SelectFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rebing\GraphQL\Tests\Database;

use Illuminate\Support\Carbon;
use Rebing\GraphQL\Tests\TestCaseDatabase;
use Rebing\GraphQL\Tests\Support\Models\Post;
use Rebing\GraphQL\Tests\Support\Models\Comment;
Expand All @@ -18,6 +19,7 @@
use Rebing\GraphQL\Tests\Support\Queries\PostsListOfWithSelectFieldsAndModelQuery;
use Rebing\GraphQL\Tests\Support\Types\PostWithModelAndAliasAndCustomResolverType;
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsAndModelAndAliasQuery;
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsAndModelAndAliasCallbackQuery;
use Rebing\GraphQL\Tests\Support\Queries\PostsNonNullAndListOfWithSelectFieldsAndModelQuery;
use Rebing\GraphQL\Tests\Support\Queries\PostWithSelectFieldsAndModelAndAliasAndCustomResolverQuery;
use Rebing\GraphQL\Tests\Support\Queries\PostsNonNullAndListAndNonNullOfWithSelectFieldsAndModelQuery;
Expand Down Expand Up @@ -295,17 +297,26 @@ public function testWithSelectFieldsAndModelAndCallbackSqlAlias(): void
'title' => 'Description of the post',
]);

factory(Comment::class, 2)
Carbon::setTestNow('2018-01-01');

factory(Comment::class)
->create([
'post_id' => $post->id,
'created_at' => new Carbon('2000-01-01'),
]);

factory(Comment::class)
->create([
'post_id' => $post->id,
'created_at' => new Carbon('2018-05-05'),
]);

$graphql = <<<GRAQPHQL
{
postWithSelectFieldsAndModelAndAlias(id: $post->id) {
postWithSelectFieldsAndModelAndAliasCallback(id: $post->id) {
id
description
commentCount
commentsLastMonth
}
}
GRAQPHQL;
Expand All @@ -318,18 +329,18 @@ public function testWithSelectFieldsAndModelAndCallbackSqlAlias(): void

$this->assertSqlQueries(
<<<'SQL'
select "posts"."id", "posts"."title", (SELECT count(*) FROM comments WHERE posts.id = comments.post_id) AS commentCount from "posts" where "posts"."id" = ? limit 1;
select "posts"."id", "posts"."title", (SELECT count(*) FROM comments WHERE posts.id = comments.post_id AND DATE(created_at) > '2018-01-01 00:00:00') AS commentsLastMonth from "posts" where "posts"."id" = ? limit 1;
SQL
);

$this->assertEquals($response->getStatusCode(), 200);

$expectedResult = [
'data' => [
'postWithSelectFieldsAndModelAndAlias' => [
'postWithSelectFieldsAndModelAndAliasCallback' => [
'id' => '1',
'description' => 'Description of the post',
'commentCount' => 2,
'commentsLastMonth' => 1,
],
],
];
Expand Down Expand Up @@ -433,6 +444,7 @@ protected function getEnvironmentSetUp($app)
PostWithSelectFieldsAndModelAndAliasQuery::class,
PostWithSelectFieldsAndModelQuery::class,
PostWithSelectFieldsNoModelQuery::class,
PostWithSelectFieldsAndModelAndAliasCallbackQuery::class,
],
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Support\Queries;

use Closure;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;
use GraphQL\Type\Definition\ResolveInfo;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Tests\Support\Models\Post;

class PostWithSelectFieldsAndModelAndAliasCallbackQuery extends Query
{
protected $attributes = [
'name' => 'postWithSelectFieldsAndModelAndAliasCallback',
];

public function type(): Type
{
return GraphQL::type('PostWithModelAndAlias');
}

public function args(): array
{
return [
'id' => [
'type' => Type::nonNull(Type::id()),
],
];
}

public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closure $getSelectFields)
{
return Post
::select($getSelectFields()->getSelect())
->findOrFail($args['id']);
}
}
13 changes: 13 additions & 0 deletions tests/Support/Types/PostWithModelAndAliasType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rebing\GraphQL\Tests\Support\Types;

use DB;
use Illuminate\Support\Carbon;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Tests\Support\Models\Post;
use Rebing\GraphQL\Support\Type as GraphQLType;
Expand All @@ -30,6 +31,18 @@ public function fields(): array
'type' => Type::nonNull(Type::int()),
'alias' => DB::raw('(SELECT count(*) FROM comments WHERE posts.id = comments.post_id) AS commentCount'),
],

'commentsLastMonth' => [
'type' => Type::nonNull(Type::int()),
'alias' => function () {
$day = Carbon::now()
->startOfMonth()
->format('Y-m-d H:i:s');

return DB::raw("(SELECT count(*) FROM comments WHERE posts.id = comments.post_id AND DATE(created_at) > '$day') AS commentsLastMonth");
},
],

];
}
}