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

Add GraphQL context as 3rd argument to custom relation 'query' #464

Merged
merged 1 commit into from
Aug 27, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
[Next release](https://github.com/rebing/graphql-laravel/compare/2.0.1...master)
--------------
### Added
- The custom `'query'` now receives the GraphQL context as the 3rd arg (same as any resolver) [\#464 / mfn](https://github.com/rebing/graphql-laravel/pull/464)
- Allow to load deeper nested queries by allowing to change the depth when calling `$getSelectFields(int $depth)` [\#472 / mfn](https://github.com/rebing/graphql-laravel/pull/472)

2019-08-18, 2.0.1
Expand Down
11 changes: 9 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,15 @@ class UserType extends GraphQLType
'posts' => [
'type' => Type::listOf(GraphQL::type('post')),
'description' => 'A list of posts written by the user',
// The first args are the parameters passed to the query
'query' => function(array $args, $query) {
'args' => [
'date_from' => [
'type' => Type::string(),
],
],
// $args are the local arguments passed to the relation
// $query is the relation builder object
// $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`
'query' => function(array $args, $query, $ctx) {
return $query->where('posts.created_at', '>', $args['date_from']);
}
]
Expand Down
4 changes: 3 additions & 1 deletion src/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ protected function getResolver(): ?Closure
// Add the 'selects and relations' feature as 5th arg
if (isset($arguments[3])) {
$arguments[] = function (int $depth = null) use ($arguments): SelectFields {
return new SelectFields($arguments[3], $this->type(), $arguments[1], $depth ?? 5);
$ctx = $arguments[2] ?? null;

return new SelectFields($arguments[3], $this->type(), $arguments[1], $depth ?? 5, $ctx);
};
}

Expand Down
50 changes: 35 additions & 15 deletions src/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ class SelectFields
* @param GraphqlType $parentType
* @param array $queryArgs Arguments given with the query/mutation
* @param int $depth The depth to walk the AST and introspect for nested relations
* @param mixed $ctx The GraphQL context; can be anything and is only passed through
* Can be created/overridden by \Rebing\GraphQL\GraphQLController::queryContext
*/
public function __construct(ResolveInfo $info, GraphqlType $parentType, array $queryArgs, int $depth)
public function __construct(ResolveInfo $info, GraphqlType $parentType, array $queryArgs, int $depth, $ctx)
{
if ($parentType instanceof WrappingType) {
$parentType = $parentType->getWrappedType(true);
}

$requestedFields = $this->getFieldSelection($info, $queryArgs, $depth);
$fields = self::getSelectableFieldsAndRelations($queryArgs, $requestedFields, $parentType);
$fields = self::getSelectableFieldsAndRelations($queryArgs, $requestedFields, $parentType, null, true, $ctx);
$this->select = $fields[0];
$this->relations = $fields[1];
}
Expand All @@ -62,17 +64,24 @@ private function getFieldSelection(ResolveInfo $resolveInfo, array $args, int $d
* Retrieve the fields (top level) and relations that
* will be selected with the query.
*
* @param array $queryArgs Arguments given with the query/mutation
* @param array $queryArgs Arguments given with the query/mutation
* @param array $requestedFields
* @param GraphqlType $parentType
* @param Closure|null $customQuery
* @param bool $topLevel
* @param mixed $ctx The GraphQL context; can be anything and is only passed through
* @return array|Closure - if first recursion, return an array,
* where the first key is 'select' array and second is 'with' array.
* On other recursions return a closure that will be used in with
*/
public static function getSelectableFieldsAndRelations(array $queryArgs, array $requestedFields, GraphqlType $parentType, ?Closure $customQuery = null, bool $topLevel = true)
{
public static function getSelectableFieldsAndRelations(
array $queryArgs,
array $requestedFields,
GraphqlType $parentType,
?Closure $customQuery = null,
bool $topLevel = true,
$ctx = null
) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided it was time to stop this "long line" madness and finally break them; did this a way times in this PR

$select = [];
$with = [];

Expand All @@ -82,7 +91,7 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
$parentTable = self::getTableNameFromParentType($parentType);
$primaryKey = self::getPrimaryKeyFromParentType($parentType);

self::handleFields($queryArgs, $requestedFields, $parentType, $select, $with);
self::handleFields($queryArgs, $requestedFields, $parentType, $select, $with, $ctx);

// If a primary key is given, but not in the selects, add it
if (null !== $primaryKey) {
Expand All @@ -95,9 +104,9 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
if ($topLevel) {
return [$select, $with];
} else {
return function ($query) use ($with, $select, $customQuery, $requestedFields) {
return function ($query) use ($with, $select, $customQuery, $requestedFields, $ctx) {
if ($customQuery) {
$query = $customQuery($requestedFields['args'], $query);
$query = $customQuery($requestedFields['args'], $query, $ctx);
}

$query->select($select);
Expand All @@ -115,9 +124,16 @@ public static function getSelectableFieldsAndRelations(array $queryArgs, array $
* @param GraphqlType $parentType
* @param array $select Passed by reference, adds further fields to select
* @param array $with Passed by reference, adds further relations
* @param mixed $ctx The GraphQL context; can be anything and is only passed through
*/
protected static function handleFields(array $queryArgs, array $requestedFields, GraphqlType $parentType, array &$select, array &$with): void
{
protected static function handleFields(
array $queryArgs,
array $requestedFields,
GraphqlType $parentType,
array &$select,
array &$with,
$ctx
): void {
$parentTable = self::isMongodbInstance($parentType) ? null : self::getTableNameFromParentType($parentType);

foreach ($requestedFields['fields'] as $key => $field) {
Expand Down Expand Up @@ -154,7 +170,7 @@ protected static function handleFields(array $queryArgs, array $requestedFields,

// Pagination
if (is_a($parentType, config('graphql.pagination_type', PaginationType::class))) {
self::handleFields($queryArgs, $field, $fieldObject->config['type']->getWrappedType(), $select, $with);
self::handleFields($queryArgs, $field, $fieldObject->config['type']->getWrappedType(), $select, $with, $ctx);
}
// With
elseif (is_array($field['fields']) && $queryable) {
Expand Down Expand Up @@ -206,9 +222,9 @@ protected static function handleFields(array $queryArgs, array $requestedFields,

self::addAlwaysFields($fieldObject, $field, $parentTable, true);

$with[$relationsKey] = self::getSelectableFieldsAndRelations($queryArgs, $field, $newParentType, $customQuery, false);
$with[$relationsKey] = self::getSelectableFieldsAndRelations($queryArgs, $field, $newParentType, $customQuery, false, $ctx);
} else {
self::handleFields($queryArgs, $field, $fieldObject->config['type'], $select, $with);
self::handleFields($queryArgs, $field, $fieldObject->config['type'], $select, $with, $ctx);
}
}
// Select
Expand Down Expand Up @@ -310,8 +326,12 @@ private static function isQueryable(array $fieldObject): bool
* @param string|null $parentTable
* @param bool $forRelation
*/
protected static function addAlwaysFields(FieldDefinition $fieldObject, array &$select, ?string $parentTable, bool $forRelation = false): void
{
protected static function addAlwaysFields(
FieldDefinition $fieldObject,
array &$select,
?string $parentTable,
bool $forRelation = false
): void {
if (isset($fieldObject->config['always'])) {
$always = $fieldObject->config['always'];

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

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Database\SelectFields\QueryArgsAndContextTests;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;
use Rebing\GraphQL\Tests\Support\Models\Comment;

class CommentType extends GraphQLType
{
protected $attributes = [
'name' => 'Comment',
'model' => Comment::class,
];

public function fields(): array
{
return [
'id' => [
'type' => Type::nonNull(Type::ID()),
],
'body' => [
'type' => Type::string(),
],
'title' => [
'type' => Type::nonNull(Type::string()),
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Database\SelectFields\QueryArgsAndContextTests;

class GraphQLContext
{
public $data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Database\SelectFields\QueryArgsAndContextTests;

use Rebing\GraphQL\GraphQLController as BaseGraphQLController;

class GraphQLController extends BaseGraphQLController
{
protected function queryContext(string $query, ?array $params, string $schema)
{
return new GraphQLContext();
}
}
Loading