From 6601e5c471f9f0389777f547d842ef9e002eef8f Mon Sep 17 00:00:00 2001 From: Roelof Jan Elsinga Date: Mon, 8 Oct 2018 12:11:15 +0200 Subject: [PATCH 1/4] Added "non_relation_field" to types with documentation --- docs/advanced.md | 32 +++++++++++++++++++++ src/Rebing/GraphQL/Support/SelectFields.php | 5 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/advanced.md b/docs/advanced.md index 8a05977c..280c1e52 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -12,6 +12,7 @@ - [Unions](#unions) - [Interfaces](#interfaces) - [Input Object](#input-object) +- [JSON Columns](#json-columns) ### Authorization @@ -881,3 +882,34 @@ class TestMutation extends GraphQLType { } ``` + +### JSON Columns + +When using JSON columns in your database, the field won't be defined as a "relationship", +but rather a simple column with nested data. To get a nested object that's not a database relationship, +use the `non_relation_field` attribute in your Type: + +```php +class UserType extends GraphQLType { + + ... + + public function fields() + { + return [ + + ... + + // JSON column containing all posts made by this user + 'posts' => [ + 'type' => Type::listOf(GraphQL::type('post')), + 'description' => 'A list of posts written by the user', + // Now this will simply request the "posts" column, and it won't + // query for all the underlying columns in the "post" object + 'non_relation_field' => true + ] + ]; + } + +} +``` diff --git a/src/Rebing/GraphQL/Support/SelectFields.php b/src/Rebing/GraphQL/Support/SelectFields.php index 3f43c058..a4624356 100644 --- a/src/Rebing/GraphQL/Support/SelectFields.php +++ b/src/Rebing/GraphQL/Support/SelectFields.php @@ -140,13 +140,16 @@ protected static function handleFields(array $requestedFields, $parentType, arra // Add a query, if it exists $customQuery = array_get($fieldObject->config, 'query'); + // Check if the field is a relation that needs to be requested from the DB + $queryable = !isset($fieldObject->config['non_relation_field']) || $fieldObject->config['non_relation_field'] === false; + // Pagination if(is_a($parentType, PaginationType::class)) { self::handleFields($field, $fieldObject->config['type']->getWrappedType(), $select, $with); } // With - elseif(is_array($field)) + elseif(is_array($field) && $queryable) { if (isset($parentType->config['model'])) { From 0345f86979a5fd2262ae2d99df19f881f597701a Mon Sep 17 00:00:00 2001 From: Roelof Jan Elsinga Date: Tue, 9 Oct 2018 14:47:58 +0200 Subject: [PATCH 2/4] Changed "non_relation_field" to "is_relation" Whether the nested object is a relation is now an opt-out instead of an opt-in configuration. --- docs/advanced.md | 3 ++- src/Rebing/GraphQL/Support/SelectFields.php | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/advanced.md b/docs/advanced.md index 280c1e52..20978dbf 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -906,7 +906,8 @@ class UserType extends GraphQLType { 'description' => 'A list of posts written by the user', // Now this will simply request the "posts" column, and it won't // query for all the underlying columns in the "post" object - 'non_relation_field' => true + // The value defaults to true + 'is_relation' => false ] ]; } diff --git a/src/Rebing/GraphQL/Support/SelectFields.php b/src/Rebing/GraphQL/Support/SelectFields.php index a4624356..9d3053aa 100644 --- a/src/Rebing/GraphQL/Support/SelectFields.php +++ b/src/Rebing/GraphQL/Support/SelectFields.php @@ -97,6 +97,19 @@ public static function getSelectableFieldsAndRelations(array $requestedFields, $ } } + /** + * Determines whether the fieldObject is queryable. + * + * @param $fieldObject + * @return bool + */ + private static function isQueryable($fieldObject) { + $is_specified_relation = isset($fieldObject->config['is_relation']) && $fieldObject->config['is_relation'] === true; + $is_default_relation = !isset($fieldObject->config['is_relation']); + + return $is_specified_relation || $is_default_relation; + } + /** * Get the selects and withs from the given fields * and recurse if necessary @@ -141,7 +154,7 @@ protected static function handleFields(array $requestedFields, $parentType, arra $customQuery = array_get($fieldObject->config, 'query'); // Check if the field is a relation that needs to be requested from the DB - $queryable = !isset($fieldObject->config['non_relation_field']) || $fieldObject->config['non_relation_field'] === false; + $queryable = self::isQueryable($fieldObject); // Pagination if(is_a($parentType, PaginationType::class)) From 3f88d11aef4173be998aae3cb77b1a09e25fa4eb Mon Sep 17 00:00:00 2001 From: Mikk Mihkel Nurges Date: Wed, 10 Oct 2018 09:41:19 +0300 Subject: [PATCH 3/4] Update SelectFields.php --- src/Rebing/GraphQL/Support/SelectFields.php | 23 +++++++++------------ 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Rebing/GraphQL/Support/SelectFields.php b/src/Rebing/GraphQL/Support/SelectFields.php index 9d3053aa..6779c114 100644 --- a/src/Rebing/GraphQL/Support/SelectFields.php +++ b/src/Rebing/GraphQL/Support/SelectFields.php @@ -97,19 +97,6 @@ public static function getSelectableFieldsAndRelations(array $requestedFields, $ } } - /** - * Determines whether the fieldObject is queryable. - * - * @param $fieldObject - * @return bool - */ - private static function isQueryable($fieldObject) { - $is_specified_relation = isset($fieldObject->config['is_relation']) && $fieldObject->config['is_relation'] === true; - $is_default_relation = !isset($fieldObject->config['is_relation']); - - return $is_specified_relation || $is_default_relation; - } - /** * Get the selects and withs from the given fields * and recurse if necessary @@ -313,6 +300,16 @@ protected static function validateField($fieldObject) return $selectable; } + /** + * Determines whether the fieldObject is queryable. + * + * @param $fieldObject + * @return bool + */ + private static function isQueryable($fieldObject) { + return array_get($fieldObject, 'is_relation', true) === true; + } + /** * Add selects that are given by the 'always' attribute */ From a74d74947c3617bf09c3ed7c6c6a4ca7759724bc Mon Sep 17 00:00:00 2001 From: Roelof Jan Elsinga Date: Thu, 1 Nov 2018 16:12:59 +0100 Subject: [PATCH 4/4] Fixed #145, changed to ->config --- src/Rebing/GraphQL/Support/SelectFields.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rebing/GraphQL/Support/SelectFields.php b/src/Rebing/GraphQL/Support/SelectFields.php index dea88958..f1c480fc 100644 --- a/src/Rebing/GraphQL/Support/SelectFields.php +++ b/src/Rebing/GraphQL/Support/SelectFields.php @@ -139,7 +139,7 @@ protected static function handleFields(array $requestedFields, $parentType, arra $customQuery = array_get($fieldObject->config, 'query'); // Check if the field is a relation that needs to be requested from the DB - $queryable = self::isQueryable($fieldObject); + $queryable = self::isQueryable($fieldObject->config); // Pagination if(is_a($parentType, PaginationType::class))