Skip to content

Conversation

@roelofjan-elsinga
Copy link
Contributor

@roelofjan-elsinga roelofjan-elsinga commented Oct 8, 2018

This extra configuration field takes care of querying nested JSON database columns.
What it does is effectively requesting to query a specific nested object, but not query any of the underlying columns, because it's not a "relationship", but an attribute of the parent.

The problem I had, that this implementation solves, was that when specifying a model on a type, any nested arrays are seen as a database relationship, even when one of the properties of the type is a nested JSON object, and not actually a relationship. This implementation makes sure to add the key of the nested object to the select fields but ignores any of the underlying columns, practically making SelectFields view the column as a single valued field.

If I missed a feature that already takes care of this, I'd love to hear how it's done, but this solved the problem for me, so perhaps it'll help others.

My solution

OLD

'persons' => [
    'type' => Type::listOf(GraphQL::type('crew_list_member')),
    'description' => 'The members of the party of the booking',
    'resolve' => function($root) {
         // I have to get the full model a second time here
         $list = \App\Models\CrewList::find($root->id);
         return $list->persons;
    },
    'selectable' => false
]

NEW

'persons' => [
    'type' => Type::listOf(GraphQL::type('crew_list_member')),
    'description' => 'The members of the party of the booking',
    'is_relation' => false
]

docs/advanced.md Outdated
'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
Copy link
Owner

Choose a reason for hiding this comment

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

Use it the other way - 'is_relation' => true

Copy link
Contributor Author

@roelofjan-elsinga roelofjan-elsinga Oct 9, 2018

Choose a reason for hiding this comment

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

Would it default to true and then you set it to false in the case I've described above? Otherwise, this would result in a lot of added configuration in most of the cases.

Whether the nested object is a relation is now an opt-out instead of an opt-in configuration.
@crissi
Copy link
Contributor

crissi commented Oct 9, 2018

isn't this the same as not adding a model to the type referrered, crew_list_member?

@roelofjan-elsinga
Copy link
Contributor Author

roelofjan-elsinga commented Oct 10, 2018

isn't this the same as not adding a model to the type referrered, crew_list_member?

@crissi I don't believe so. My "crew_list_member" already has no model attribute, but my crew_list type does, because it is a model. Since the "parentType" in the method, crew_list in this case, is a model, it'll automatically look for a relationship within the specified model, even though this relationship doesn't exist. However "persons", the fieldname for the crew_list_member type, does exist, as a column. I've tried to play with the model attribute on the crew_list type, but no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants