0
0
GraphQLquery~30 mins

Bidirectional relationships in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Bidirectional Relationships in GraphQL
📖 Scenario: You are building a simple social network database where users can follow each other. Each user should know who they follow and who follows them.
🎯 Goal: Create a GraphQL schema that models users with bidirectional relationships: each user has a list of users they follow and a list of users who follow them.
📋 What You'll Learn
Define a User type with id and name fields
Add a follows field to User that lists users this user follows
Add a followers field to User that lists users who follow this user
Create a root Query type to fetch all users
💡 Why This Matters
🌍 Real World
Social networks and apps often need to model relationships where users follow each other, requiring bidirectional links.
💼 Career
Understanding bidirectional relationships in GraphQL is important for backend developers building APIs for social platforms and connected data.
Progress0 / 4 steps
1
Define the User type with basic fields
Define a GraphQL User type with fields id of type ID! and name of type String!.
GraphQL
Need a hint?

Start by defining a type User with id and name fields.

2
Add the follows field to User
Add a field called follows to the User type. It should be a list of User objects, non-nullable, and the list itself should be non-nullable (use [User!]!).
GraphQL
Need a hint?

The follows field should be a list of users this user follows.

3
Add the followers field to User
Add a field called followers to the User type. It should be a list of User objects, non-nullable, and the list itself should be non-nullable (use [User!]!).
GraphQL
Need a hint?

The followers field should be a list of users who follow this user.

4
Create the Query type to fetch all users
Define a root Query type with a field called users that returns a non-nullable list of non-nullable User objects (use [User!]!).
GraphQL
Need a hint?

The Query type should allow fetching all users with the users field.