Member-only story
How to Build a GraphQL + MongoDB Series
Part 5: FieldResolver and Data Loader made easy with NestJS
Now that you have your scaffolded GraphQL API with Authentication, made with NestJS… it’s time to talk about data loaders and batching!

Welcome back to my GraphQL with NestJS series! If it’s your first time here, please make sure to read my previous articles:
- https://javascript.plainenglish.io/graphql-nodejs-mongodb-made-easy-with-nestjs-and-mongoose-29f9c0ea7e1d
- https://makinhs.medium.com/unit-testing-made-easy-with-nestjs-part-02-for-graphql-mongodb-backend-article-series-b37f147e2a9e
- https://makinhs.medium.com/e2e-testing-made-easy-with-nestjs-part-3-of-graphql-nodejs-mongodb-series-ba913bad7cf2
- https://makinhs.medium.com/authentication-made-easy-with-nestjs-part-4-of-how-to-build-a-graphql-mongodb-d6057eae3fdf
It is nice to have read the previous articles to follow this one.
We are going to cover some more advanced GraphQL topics, even though it is considered basic to some sort of developers, which are the data loaders
My goal here is not to judge how hard or not this topic is but to show how to use it in NestJS.
Quick review on GraphQL and resolvers
Remember that when we are playing with GraphQL, we can select the fields we want to resolve, and we can also have a way to resolve a field only if it's selected in the query in order to prevent non-necessary database queries.
In our previous articles we managed to create a simple User entity that didn’t have other fields to be resolved:
@Schema()
@ObjectType()
export class User {
@Field(() => String)
_id: MongooseSchema.Types.ObjectId | string;
@Prop()
@Field(() => String, { description: 'User firstName ' })
firstName: string;
@Prop()
@Field(() => String, { description: 'User lastName ' })
lastName: string;
@Prop()
@Field(() => String, { description: 'User email ' })
email: string;
@Prop()
password: string;
@Prop()
@Field(() => String, { description: 'User role' })
role: string;
}