0
0
GraphqlHow-ToBeginner · 3 min read

How to Write Resolver for Relationship in GraphQL

To write a resolver for a relationship in GraphQL, define a resolver function for the related field that fetches the connected data, often by querying a database or another data source. Use the parent argument to access the current object's data and return the related objects accordingly.
📐

Syntax

A resolver for a relationship is a function defined on a field in your GraphQL type. It receives parent (the current object), args (arguments), context (shared info like database), and info (query details). The resolver returns the related data, often by querying a database using parent data.

  • parent: The object that contains the field.
  • args: Arguments passed to the field.
  • context: Shared resources like database connections.
  • info: Query info (rarely used for simple cases).
javascript
const resolvers = {
  ParentType: {
    relatedField: (parent, args, context, info) => {
      // Use parent.id or other fields to fetch related data
      return context.db.getRelatedData(parent.id);
    }
  }
};
💻

Example

This example shows a User type with a posts field that returns all posts written by the user. The resolver uses the parent.id to fetch posts from a mock database.

javascript
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' }
];

const posts = [
  { id: 1, title: 'Hello World', authorId: 1 },
  { id: 2, title: 'GraphQL Basics', authorId: 1 },
  { id: 3, title: 'Advanced GraphQL', authorId: 2 }
];

const resolvers = {
  User: {
    posts: (parent) => {
      return posts.filter(post => post.authorId === parent.id);
    }
  }
};

// Example usage:
const user = users[0];
const userPosts = resolvers.User.posts(user);
console.log(userPosts);
Output
[ { id: 1, title: 'Hello World', authorId: 1 }, { id: 2, title: 'GraphQL Basics', authorId: 1 } ]
⚠️

Common Pitfalls

Common mistakes when writing relationship resolvers include:

  • Not using the parent argument to access the current object's data.
  • Returning incorrect or incomplete related data.
  • Forgetting to handle asynchronous data fetching with async functions or promises.
  • Not using the context to access shared resources like databases.

Always ensure your resolver returns the correct related data and handles async operations properly.

javascript
/* Wrong: ignoring parent and returning all posts */
const resolversWrong = {
  User: {
    posts: () => {
      return posts; // returns all posts, not just user's
    }
  }
};

/* Right: use parent to filter posts by authorId */
const resolversRight = {
  User: {
    posts: (parent) => {
      return posts.filter(post => post.authorId === parent.id);
    }
  }
};
📊

Quick Reference

ConceptDescription
parentCurrent object containing the field
argsArguments passed to the field
contextShared resources like database connections
resolver functionReturns related data using parent and context
async handlingUse async/await if fetching data asynchronously

Key Takeaways

Use the parent argument in the resolver to access the current object's data for relationships.
Fetch related data by filtering or querying using parent fields like IDs.
Always handle asynchronous data fetching with async functions or promises.
Use context to access shared resources such as databases inside resolvers.
Test resolvers to ensure they return the correct related data for each parent object.