Type vs Field
Approach
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
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
Type Resolvers return response for node compliant to its type data shape.
- e.g.
Usernode would have atype resolverwhich returns whole integrity ofUserfields wherever it may be used (viatype 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.