In a sub-resolver: Can i get the `id` of my parent...
# prisma-whats-new
n
In a sub-resolver: Can i get the
id
of my parent, even if
info
did not contain an
id
. How can i determine the identity of the father? For example if info is
{ profile { posts { title,  publicComments } } }
, how can i use the missing
id
of the given
post
to resolve
publicComments
?
n
if I understand correctly, the
id
you’re looking for is in the first resolver argument (usually called
root
or
parent
) and not in in the
info
boject
l
That should be available as the first argument (parent)
Oops I got beat to it 🙂
😅 1
n
This should help understanding what’s going on: https://blog.graph.cool/ac5e2950214e#9d03
n
It is in the first resolver argument,(called
parent
), if the
info
object passed in the fathers resolver did request an
id
. I can run a query like
{ father { id { children}}
or i can run a query
{father {children}}
(omitting
id
). Now when the child-resolver is invoked, ther is no id in the
parent
.
m
@Nick Luger I think you need to use fragment in your resolver, but last time I checked fragments didn’t work - there’s an ongoing issue in apollo & prisma-binding. RN afaik you can only get it working by including it in
info
.
n
Ok thank you @matic! Is there a way i can force-include it in
info
, by passing the original
info
query and always add the
id
field to the
info
object? (The info object is a bit opaque to me, until now i used strings to describe it)
m
@Nick Luger afaik it would be a major difficulty
n
@matic , you saved me! 😃 Fragments did the job. (
fragment: 'fragment PublicPosts on User { id }', resolve: (...) => ...)
. Now the id is always in the parent, no matter the info object provided by the client.
Is there a good place to document this? There is no documentation on fragments, and almost no docs on sub-resolvers in general. However the fragments solution could be found in the graphq-server-example.
m
@Nick Luger that’s awesome, which version of binding and yoga are you using?
I think it might be well documented in Apollo, not sure whether it is in Prisma...
n
I'm using
"prisma-binding": "1.5.12"
and
"graphql-yoga": "1.3.3"
which uses
"graphql-binding": "1.2.2",
for what i found.
👍 1
The queries then contain typed inline fragments (http://graphql.org/learn/queries/#inline-fragments)
Copy code
... on User {
      id
    }
m
Thanks! Fragment replacements are latter on passed to Apollo if you dug a bit deeper. There was a problem recently which prevented correct functionality of fragments, but I am happy to hear that they work now! 🙂