diff --git a/docs/advanced.md b/docs/advanced.md index 8a05977c..20978dbf 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,35 @@ 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 + // 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 3f43c058..6779c114 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 = self::isQueryable($fieldObject); + // 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'])) { @@ -297,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 */