Use case for resolve and resolveField outside of root query? #913
Unanswered
abdullahseba
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What is the use case for having
resolveandresolveFieldat the type level? If the rootquerydoes not have a resolver, none of the resolvers further down get executed. I learned from #721 (comment), that if the root query resolver returns anything other thannull, the resolvers further down get executed. However, for a multi-level query, resolvers that returntrue, cause an error. Take the following query:{ objectRecords { edges { node { objBriefDesc } } } }Root query field:
ObjectRecordConnection type:
[ 'name' => 'ObjectRecordConnection', 'description' => 'ObjectRecordConnection', 'fields' => [ 'totalCount' => [ 'type' => TR::int(), 'description' => 'The total number of object records in the database.' ], 'edges' => [ 'type' => TR::ListOf(TR::objectRecordEdge()), 'description' => 'The total number of object records in the database.', 'resolve' => function ($root, $args) { return ['edges' => []]; } ], ... ], ];ObjectRecord Edge type:
[ 'name' => 'ObjectRecordEdge', 'description' => 'ObjectRecordEdge', 'fields' => [ 'node' => [ 'type' => TR::objectRecord(), 'description' => 'The item at the end of the edge.', 'resolve' => function ($root, $args) { return ['node' => []]; } ], ... ]ObjectRecord type:
[ 'name' => 'ObjectRecord', 'description' => 'ObjectRecord', 'fields' => [ 'id' => TR::id(), 'objBriefDesc' => [ 'type' => TR::string(), 'resolve' => function () { return 'description'; } ], ... ] ];Result:
{ "data": { "objectRecords": { "edges": [ { "node": { "objBriefDesc": "description" } } ] } } }Without
ObjectRecordConnectionreturningedgesandObjectRecordEdgereturningnodearrays, the description resolver does not run. With the code above, the description resolver overwrites anything returned by the previous resolver in thenodearray.Having to return part of the query in each resolver seems long-winded for multi-level queries, and I was curious what use cases there would be for doing this. I may well be missing something obvious, and it's a case 'just because you can, doesn't mean you should'. To be clear, I'm just experimenting and trying to find the best way to structure my project.
Thank you for the great library!
Beta Was this translation helpful? Give feedback.
All reactions