JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

How to Build a GraphQL + MongoDB Series

Part 5: FieldResolver and Data Loader made easy with NestJS

Marcos Henrique da Silva
JavaScript in Plain English
10 min readAug 30, 2022

--

Now that you have your scaffolded GraphQL API with Authentication, made with NestJS… it’s time to talk about data loaders and batching!

Photo by ThisIsEngineering: https://www.pexels.com/photo/female-software-engineer-coding-on-computer-3861972/

Welcome back to my GraphQL with NestJS series! If it’s your first time here, please make sure to read my previous articles:

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;
}

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

No responses yet

Write a response