Nested resolvers and relational data in GraphQL

Nested resolvers and relational data in GraphQL

In this post we will give you information about Nested resolvers and relational data in GraphQL. Hear we will give you detail about Nested resolvers and relational data in GraphQLAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to create relational data and nested resolvers in graphql.

Note: If you don’t know about GraphQL then refer to my previous tutorial GraphQL intro for the beginners

Relational data means consider we have a ‘todo app’ with users and todos where each todo iscreated by the particular user.

let todos = [    {        id: 1,        title: "This is my todo",        body: "sample content in id 1",        userId: 1    },    {        id: 2,        title: "This is my second todo",        body: "sample content in id 2",        userId: 1    },    {        id: 3,        title: "This is my third todo",        body: "sample content in id 3",        userId: 2    }]let users = [    {        id: 1,        name: "sai"    },    {        id: 2,        name: "gowtham"    },]

if any user asks for their todos we only show the todos created by them using some type of relationship between the user and todos.

Let’s start writing code.

Type definitions

type Todo{    id: ID!    title: String!    body: String!}type User{    id:ID!    name:String!}type Query{     user(id:ID!): User!}

We created two object types which are Todo & User and One Query Type.

Resolvers

Next, we need to tell the graphql how to resolve the user field in the query.

const resolvers = {    Query: {        user(parent, args, ctx, info) {            if (!args.id) {                throw new Error('id is required')            }            return users.find(user => user.id === +args.id)        }    }}

Let’s test it now by using the graphql play-ground.

Relational data

In this step, we are creating a relationship between the user and todos for this we need to adda new field called todos to the User type.

type User{    id:ID!    name:String!    todos:[Todo!]!}

if you ask for a todos you will get an array of todos where it’s shape should look like Todotype but we don’t have any resolver for the todos field.

Nested Resolvers

Let’s create a nested resolver to handle the todos field.

const resolvers = {    Query: {        user(parent<span>, args, ctx, info) {            if (!args.id) {                throw new Error('id is required')            }            return users.find(user =&gt; user.id === +args.id)        }    },    User: {        todos(parent, args, ctx, info) {              //parent is the root object (User is the parent here)            return todos.filter(todo =&gt; todo.userId === parent.id)        }    }}

In the above code, we have added Nested todos resolver on the User object so that if any user asks for there todos we need to take the id from it’s parent and return their todos.

Note: Nested resolvers only run when we query for that nested field otherwise it doesn’t run.

Code repository

Hope this code and post will helped you for implement Nested resolvers and relational data in GraphQL. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US