Skip to content

Commit d8dace9

Browse files
roelofjan-elsingaMikk Mihkel Nurges
authored andcommitted
Added "non_relation_field" to types with documentation (#140)
* Added "non_relation_field" to types with documentation * 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. * Update SelectFields.php
1 parent 061e7ca commit d8dace9

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

docs/advanced.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Unions](#unions)
1313
- [Interfaces](#interfaces)
1414
- [Input Object](#input-object)
15+
- [JSON Columns](#json-columns)
1516

1617
### Authorization
1718

@@ -881,3 +882,35 @@ class TestMutation extends GraphQLType {
881882

882883
}
883884
```
885+
886+
### JSON Columns
887+
888+
When using JSON columns in your database, the field won't be defined as a "relationship",
889+
but rather a simple column with nested data. To get a nested object that's not a database relationship,
890+
use the `non_relation_field` attribute in your Type:
891+
892+
```php
893+
class UserType extends GraphQLType {
894+
895+
...
896+
897+
public function fields()
898+
{
899+
return [
900+
901+
...
902+
903+
// JSON column containing all posts made by this user
904+
'posts' => [
905+
'type' => Type::listOf(GraphQL::type('post')),
906+
'description' => 'A list of posts written by the user',
907+
// Now this will simply request the "posts" column, and it won't
908+
// query for all the underlying columns in the "post" object
909+
// The value defaults to true
910+
'is_relation' => false
911+
]
912+
];
913+
}
914+
915+
}
916+
```

src/Rebing/GraphQL/Support/SelectFields.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ protected static function handleFields(array $requestedFields, $parentType, arra
140140
// Add a query, if it exists
141141
$customQuery = array_get($fieldObject->config, 'query');
142142

143+
// Check if the field is a relation that needs to be requested from the DB
144+
$queryable = self::isQueryable($fieldObject);
145+
143146
// Pagination
144147
if(is_a($parentType, PaginationType::class))
145148
{
146149
self::handleFields($field, $fieldObject->config['type']->getWrappedType(), $select, $with);
147150
}
148151
// With
149-
elseif(is_array($field))
152+
elseif(is_array($field) && $queryable)
150153
{
151154
if (isset($parentType->config['model']))
152155
{
@@ -297,6 +300,16 @@ protected static function validateField($fieldObject)
297300
return $selectable;
298301
}
299302

303+
/**
304+
* Determines whether the fieldObject is queryable.
305+
*
306+
* @param $fieldObject
307+
* @return bool
308+
*/
309+
private static function isQueryable($fieldObject) {
310+
return array_get($fieldObject, 'is_relation', true) === true;
311+
}
312+
300313
/**
301314
* Add selects that are given by the 'always' attribute
302315
*/

0 commit comments

Comments
 (0)