Skip to content

allow adding columns in query field #806

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

Merged
merged 5 commits into from
May 4, 2022
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
--------------
### Fixed
- Allow 'always' to work on object types [\#473 / tinyoverflow \#369 / zjbarg](https://github.com/rebing/graphql-laravel/pull/892)

- Allow using addSelect() in relationship query scopes [\#875 / codercms](https://github.com/rebing/graphql-laravel/pull/806)
### Removed
- Support for PHP 7.2, PHP 7.3 and Laravel 7.0 (all EOL) [\#914 / mfn](https://github.com/rebing/graphql-laravel/pull/914)

Expand Down
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,32 @@ The default GraphiQL view makes use of the global `csrf_token()` helper function

- [Laravel GraphQL](#laravel-graphql)
- [Installation](#installation)
- [Dependencies:](#dependencies)
- [Installation:](#installation)
- [Laravel 6.0+](#laravel)
- [Dependencies:](#dependencies)
- [Installation:](#installation-1)
- [Laravel](#laravel)
- [Usage](#usage)
- [Concepts](#concepts)
- [A word on declaring a field `nonNull`](#a-word-on-declaring-a-field-nonnull)
- [Data loading](#data-loading)
- [GraphiQL](#graphiql)
- [Middleware Overview](#middleware-overview)
- [HTTP middleware](#http-middleware)
- [GraphQL execution middleware](#graphql-execution-middleware)
- [GraphQL resolver middleware](#graphql-resolver-middleware)
- [Schemas](#schemas)
- [Schema classes](#schema-classes)
- [Creating a query](#creating-a-query)
- [Creating a mutation](#creating-a-mutation)
- [Adding validation to a mutation](#adding-validation-to-a-mutation)
- [File uploads](#file-uploads)
- [Validation](#validation)
- [Vue.js and Axios example](#vuejs-and-axios-example)
- [jQuery or vanilla javascript](#jquery-or-vanilla-javascript)
- [Validation](#validation)
- [Example defining rules in each argument](#example-defining-rules-in-each-argument)
- [Example using the `rules()` method](#example-using-the-rules-method)
- [Example using Laravel's validator directly](#example-using-laravels-validator-directly)
- [Handling validation errors](#handling-validation-errors)
- [Customizing error messages](#customizing-error-messages)
- [Misc notes](#misc-notes)
- [Resolve method](#resolve-method)
- [Resolver middleware](#resolver-middleware)
- [Defining middleware](#defining-middleware)
Expand All @@ -97,6 +106,7 @@ The default GraphiQL view makes use of the global `csrf_token()` helper function
- [Enums](#enums)
- [Unions](#unions)
- [Interfaces](#interfaces)
- [Supporting custom queries on interface relations](#supporting-custom-queries-on-interface-relations)
- [Sharing interface fields](#sharing-interface-fields)
- [Input Object](#input-object)
- [Type modifiers](#type-modifiers)
Expand All @@ -105,8 +115,11 @@ The default GraphiQL view makes use of the global `csrf_token()` helper function
- [Field deprecation](#field-deprecation)
- [Default field resolver](#default-field-resolver)
- [Macros](#macros)
- [Automatic Persisted Queries support](#automatic-persisted-queries-support)
- [Misc features](#misc-features)
- [Automatic Persisted Queries support](#automatic-persisted-queries-support)
- [Notes](#notes)
- [Client example](#client-example)
- [Misc features](#misc-features)
- [Detecting unused variables](#detecting-unused-variables)
- [Configuration options](#configuration-options)
- [Guides](#guides)
- [Upgrading from v1 to v2](#upgrading-from-v1-to-v2)
Expand Down Expand Up @@ -1789,7 +1802,8 @@ class UserType extends GraphQLType
// $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`
// The return value should be the query builder or void
'query' => function (array $args, $query, $ctx): void {
$query->where('posts.created_at', '>', $args['date_from']);
$query->addSelect('some_column')
->where('posts.created_at', '>', $args['date_from']);
}
]
];
Expand Down
2 changes: 1 addition & 1 deletion src/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function getSelectableFieldsAndRelations(
$query = $customQuery($requestedFields['args'], $query, $ctx) ?? $query;
}

$query->select($select);
$query->addSelect($select);
$query->with($with);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,74 @@ public function testRelationshipAlias(): void

self::assertSame($expectedResult, $result);
}

public function testCustomQueryAllowColumnToBeAdded(): void
{
/** @var User[] $users */
$users = factory(User::class, 2)
->create()
->each(function (User $user): void {
/** @var Post $post */
$post = factory(Post::class)
->create([
'flag' => true,
'user_id' => $user->id,
]);
});

$graphql = <<<'GRAQPHQL'
{
users(select: true, with: true) {
id
name
postsWithExtraField {
body
id
title
}
}
}
GRAQPHQL;

$this->sqlCounterReset();

$result = $this->httpGraphql($graphql);

$this->assertSqlQueries(
<<<'SQL'
select "users"."id", "users"."name" from "users" order by "users"."id" asc;
select 42 as meaning_of_life, "posts"."body", "posts"."id", "posts"."title", "posts"."user_id" from "posts" where "posts"."user_id" in (?, ?) order by "posts"."id" asc;
SQL
);

$expectedResult = [
'data' => [
'users' => [
[
'id' => (string) $users[0]->id,
'name' => $users[0]->name,
'postsWithExtraField' => [
[
'body' => $users[0]->posts[0]->body,
'id' => (string) $users[0]->posts[0]->id,
'title' => $users[0]->posts[0]->title,
],
],
],
[
'id' => (string) $users[1]->id,
'name' => $users[1]->name,
'postsWithExtraField' => [
[
'body' => $users[1]->posts[0]->body,
'id' => (string) $users[1]->posts[0]->id,
'title' => $users[1]->posts[0]->title,
],
],
],
],
],
];
self::assertSame($expectedResult, $result);
}
}
10 changes: 10 additions & 0 deletions tests/Database/SelectFields/QueryArgsAndContextTests/UserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use GraphQL\Type\Definition\Type;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\DB;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Type as GraphQLType;
use Rebing\GraphQL\Tests\Support\Models\User;
Expand Down Expand Up @@ -48,6 +49,15 @@ public function fields(): array
'query' => function (array $args, HasMany $query): HasMany {
$query->where('posts.flag', '=', 1);

return $query;
},
],
'postsWithExtraField' => [
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('Post')))),
'alias' => 'posts',
'query' => function (array $args, HasMany $query): HasMany {
$query->addSelect(DB::raw('42 as meaning_of_life'));

return $query;
},
],
Expand Down