As mentioned before, resolver can return whatever value is defined (as long as it complies to the return type defined).
Type resolver is NOT a native mechanism provided by Graphql, rather it's a concept/pattern that can be enforced in the code base.
Field Resolvers
represent specific data returned to fulfill the field value requested.
export const someField: SomeFieldType = async(parent, args, context, info) => { const someResult = await someService.fetchValue(parent.id); return { ...someResult } }
Type Resolvers
return response for node
compliant to its type
data shape.
User
node would have a type resolver
which returns whole integrity of User
fields wherever it may be used (via type User
).// calling the type resolver at query resolver layer export const someField: SomeFieldType = async(parent, args, context, info) =>{ return { ...new NodeName(parent, args) } } // implementation is uniform, common, and shared class NodeName{ constructor(parent, args){ this.id = parent.id } async someDetail() { const fetchedDetail = someService.findSomeDetail(this.id); return fetchedDetail; } async complexResponse() { // ... return { // ... } } }
Caveat that when using the class type resolver approach, avoid bloating it with too many functionalities attached to it.