In the previous article, we’ve implemented resolvers and queries. There is quite a common catch with them, though. It is referred to as the N + 1 problem. In this article, we illustrate the issue and provide a few ways to deal with it.
The N + 1 problem
The N + 1 problem can appear when we fetch nested, related data. A good example would be the following query:
Above, we request an author for every post. One way to do so would be to use the @ResolveField() decorator that NestJS gives us. Let’s add it to our resolver:
1import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
2import { Post } from './models/post.model';
3import PostsService from './posts.service';
4import { User } from '../users/models/user.model';
5import { UsersService } from '../users/users.service';
6
7@Resolver(() => Post)
8export class PostsResolver {
9 constructor(
10 private postsService: PostsService,
11 private usersService: UsersService,
12 ) {}
13
14 @Query(() => [Post])
15 async posts() {
16 const posts = await this.postsService.getPosts();
17 return posts.items;
18 }
19
20 @ResolveField('author', () => User)
21 async getAuthor(@Parent() post: Post) {
22 const { authorId } = post;
23
24 return this.usersService.getById(authorId);
25 }
26
27 // ...
28}In this example, we create a field resolver. What we return from it becomes the value of the author. To figure out which author we need to query from the database, we access the parent. In this case, the post is the parent of the author.
In this series, we use the TypeORM library. To have access to the authorId property, we need to use @RelationId() decorator.
1import { Entity, ManyToOne, PrimaryGeneratedColumn, Index, RelationId } from 'typeorm';
2import User from '../users/user.entity';
3
4@Entity()
5class Post {
6 @PrimaryGeneratedColumn()
7 public id: number;
8
9 @Index('post_authorId_index')
10 @ManyToOne(() => User, (author: User) => author.posts)
11 public author: User
12
13 @RelationId((post: Post) => post.author)
14 public authorId: number;
15
16 // ...
17}We also need to add it to our GraphQL model.
1import { Field, Int, ObjectType } from '@nestjs/graphql';
2import { User } from '../../users/models/user.model';
3
4@ObjectType()
5export class Post {
6 @Field(() => Int)
7 id: number;
8
9 @Field(() => Int)
10 authorId: number;
11
12 @Field()
13 author: User;
14
15 // ...
16}Seeing the issue
While the above certainly works, it has a serious flaw. To visualize it, let’s modify our DataBase slightly:
1import { Module } from '@nestjs/common';
2import { TypeOrmModule } from '@nestjs/typeorm';
3import { ConfigModule, ConfigService } from '@nestjs/config';
4
5@Module({
6 imports: [
7 TypeOrmModule.forRootAsync({
8 imports: [ConfigModule],
9 inject: [ConfigService],
10 useFactory: (configService: ConfigService) => ({
11 type: 'postgres',
12 host: configService.get('POSTGRES_HOST'),
13 // ...
14 logging: true,
15 })
16 }),
17 ],
18})
19export class DatabaseModule {}You can find more options connected to the logging functionality in the official TypeORM documentation.
Thanks to logging: true, we can now see what queries we make to the database. It looks a bit like the following:
1SELECT * FROM post;
2SELECT * FROM user WHERE id = 1;
3SELECT * FROM user WHERE id = 2;
4SELECT * FROM user WHERE id = 1;
5SELECT * FROM user WHERE id = 1;
6SELECT * FROM user WHERE id = 2;
7...The core of the issue is that for N posts, we make N + 1 queries to the database. This is why it is called the N + 1 problem.
Solving the N + 1 problem with the DataLoader
One of the ways of solving the above issue is using the DataLoader library. We can use it to batch our requests and load all entities at once.
1const batchAuthorsLoader = new DataLoader(userIds: number[] => {
2 // ... fetch all users
3});1batchAuthorsLoader.load(1);
2batchAuthorsLoader.load(2);
3batchAuthorsLoader.load(1);With the above code, we tell the DataLoader that we will need authors with certain ids. Our goal is to batch all of those and create queries like that:
1SELECT * FROM post; SELECT * FROM user WHERE id IN (1, 2, 3)Some libraries aim to integrate the DataLoader with NestJS, but they are not well maintained.
The crucial thing to understand with the DataLoader is that it should be initialized once per request. Therefore, it is commonly used within a GraphQL context object that can be initialized once per every request. We could achieve it with a code like that:
1GraphQLModule.forRootAsync({
2 imports: [ConfigModule, UsersModule],
3 inject: [ConfigService, UsersService],
4 useFactory: (
5 configService: ConfigService,
6 usersService: UsersService
7 ) => ({
8 playground: Boolean(configService.get('GRAPHQL_PLAYGROUND')),
9 autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
10 context: () => ({
11 batchAuthorsLoader: batchAuthorsLoader(usersService)
12 })
13 })
14}),Unfortunately, this is not very elegant, because we would need to put all of our loaders there in one place. Instead, we can follow a very cool idea by Jeppe Smith.
First, let’s create a method that can query multiple users from the database at once.
1import { Injectable } from '@nestjs/common';
2import { InjectRepository } from '@nestjs/typeorm';
3import { Repository, In } from 'typeorm';
4import User from './user.entity';
5
6@Injectable()
7export class UsersService {
8 constructor(
9 @InjectRepository(User)
10 private usersRepository: Repository<User>,
11 ) {}
12
13 async getByIds(ids: number[]) {
14 return this.usersRepository.find({
15 where: { id: In(ids) },
16 });
17 }
18
19 // ...
20}An important thing to remember that the order of the results in usersRepository.find is not guaranteed. The results that we return from our DataLoader need to be in the same order as the ids. Therefore, we need to map them to ensure that.
Let’s create an Injectable with scope: Scope.REQUEST. This means that NestJS will reinitialize our class for every request.
1import { Injectable, Scope } from '@nestjs/common';
2import { UsersService } from '../../users/users.service';
3import * as DataLoader from 'dataloader';
4
5@Injectable({ scope: Scope.REQUEST })
6export default class PostsLoaders {
7 constructor(
8 private usersService: UsersService,
9 ) {
10 }
11
12 public readonly batchAuthors = new DataLoader(async (authorIds: number[]) => {
13 const users = await this.usersService.getByIds(authorIds);
14 const usersMap = new Map(users.map(user => [user.id, user]));
15 return authorIds.map(authorId => usersMap.get(authorId));
16 })
17}We also need to add PostsLoaders to providers array in the PostModule.
The last thing is to use the above loader in our resolver.
1import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
2import { Post } from './models/post.model';
3import PostsService from './posts.service';
4import { User } from '../users/models/user.model';
5import PostsLoaders from './loaders/posts.loaders';
6
7@Resolver(() => Post)
8export class PostsResolver {
9 constructor(
10 private postsService: PostsService,
11 private postsLoaders: PostsLoaders
12 ) {}
13
14 @Query(() => [Post])
15 async posts() {
16 const posts = await this.postsService.getPosts();
17 return posts.items;
18 }
19
20 @ResolveField('author', () => User)
21 async getAuthor(
22 @Parent() post: Post
23 ) {
24 const { authorId } = post;
25
26 return this.postsLoaders.batchAuthors.load(authorId);
27 }
28
29 // ...
30}With the above solutions, we make just two queries to the database. The first one is for the posts, and the second one is for all of the authors.
Solving the N + 1 issue with JOIN queries
Instead of the above approach, we might create just one database query that joins posts and users. First, let’s look into the implementation of a method that does that in the PostsService.
1import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
2import CreatePostDto from './dto/createPost.dto';
3import Post from './post.entity';
4import { InjectRepository } from '@nestjs/typeorm';
5import { Repository } from 'typeorm';
6import { MoreThan, FindManyOptions } from 'typeorm';
7
8@Injectable()
9export default class PostsService {
10 constructor(
11 @InjectRepository(Post)
12 private postsRepository: Repository<Post>,
13 ) {}
14
15 async getPosts(offset?: number, limit?: number, startId?: number, options?: FindManyOptions<Post>) {
16 const where: FindManyOptions<Post>['where'] = {};
17 let separateCount = 0;
18 if (startId) {
19 where.id = MoreThan(startId);
20 separateCount = await this.postsRepository.count();
21 }
22
23 const [items, count] = await this.postsRepository.findAndCount({
24 where,
25 order: {
26 id: 'ASC'
27 },
28 skip: offset,
29 take: limit,
30 ...options
31 });
32
33 return {
34 items,
35 count: startId ? separateCount : count
36 }
37 }
38
39 async getPostsWithAuthors(offset?: number, limit?: number, startId?: number) {
40 return this.getPosts(offset, limit, startId, {
41 relations: ['author'],
42 })
43 }
44}Above, our getPosts method contains the pagination functionality. If you want to know more about it, check out API with NestJS #17. Offset and keyset pagination with PostgreSQL and TypeORM
Now, let’s use the getPostsWithAuthors method in our resolver:
1import { Query, Resolver } from '@nestjs/graphql';
2import { Post } from './models/post.model';
3import PostsService from './posts.service';
4
5@Resolver(() => Post)
6export class PostsResolver {
7 constructor(
8 private postsService: PostsService
9 ) {}
10
11 @Query(() => [Post])
12 async posts() {
13 const posts = await this.postsService.getPostsWithAuthors();
14 return posts.items;
15 }
16
17 // ...
18}Thanks to that approach, TypeORM generates a single query both for the posts and the authors. There is an issue here, though. With the above code, we always fetch both posts and authors, even if the client does not request it.
Fortunately, we can access the details about the GraphQL query with the @Info() decorator. The most straightforward way to use it is with the graphql-parse-resolve-info library.
1import { Info, Query, Resolver } from '@nestjs/graphql';
2import { Post } from './models/post.model';
3import PostsService from './posts.service';
4import { parseResolveInfo, ResolveTree, simplifyParsedResolveInfoFragmentWithType } from 'graphql-parse-resolve-info';
5import { GraphQLResolveInfo } from 'graphql';
6
7@Resolver(() => Post)
8export class PostsResolver {
9 constructor(
10 private postsService: PostsService
11 ) {}
12
13 @Query(() => [Post])
14 async posts(
15 @Info() info: GraphQLResolveInfo
16 ) {
17 const parsedInfo = parseResolveInfo(info) as ResolveTree;
18 const simplifiedInfo = simplifyParsedResolveInfoFragmentWithType(
19 parsedInfo,
20 info.returnType
21 );
22
23 const posts = 'author' in simplifiedInfo.fields
24 ? await this.postsService.getPostsWithAuthors()
25 : await this.postsService.getPosts();
26
27 return posts.items;
28 }
29}Thanks to the above optimization, we join posts with authors only if the user requests it through the query.
Summary
This article tackled the N + 1 problem, which is a common issue in the GraphQL world. The first approach that we’ve implemented was with the DataLoader library. With it, we’ve limited the number of queries that we perform to two. The second way of solving the problem was with a JOIN query performed through TypeORM.
There are a lot more topics when it comes to GraphQL, so you can expect more articles about it. Stay tuned!