If I have 2 types: User
and Note
with the following schema:
query {
getUser(userId: ID!): User
}
type User {
userId: ID
email: String
notes: [Note]
}
type Note {
noteId: ID
text: String
}
I am writing a resolver for User#notes
. Now say notes need to be retrieved by email address, so I actually need the root object passed to the resolver to contain the email
field, is there anyway I can force GraphQL to query the email
field in the User
object even if the user has not requested it?
In terms of code, from what I see, this is how I can write a resolver. How can I ensure obj.email
is requested whenever the user requests the note
field?
User: {
notes(obj, args, context, info) {
// How can I ensure obj.email is requested?
return NoteRetriever.getNotesByEmail(obj.email);
}
}
Edit
I am wondering about the case where the parent resolver doesn't resolve the email
field unless explicitly requested. What if we need to make an API call to get the email for the user? So by default we don't request it. However, when the notes is requested, it makes sense to request the email too.
Is there a way for the resolver to specify dependency on parent fields - to ensure that gets requested?