@jenyus-org/nestjs-graphql-utils
v1.6.4
Published
NestJS utilities and decorators built around @jenyus-org/graphql-utils.
Downloads
19,587
Readme
nestjs-graphql-utils
@jenyus-org/nestjs-graphql-utils
is a collection of utilities and decorators built on top of @jenyus-org/graphql-utils
to encourage the stateless nature of NestJS GraphQL resolvers and simplify the usage of helpers.
Documentation
The full documentation with live code sandboxes can be found here.
Installation
@jenyus-org/nestjs-graphql-utils
can be installed from NPM by running one of the following commands:
NPM:
npm i --save @jenyus-org/nestjs-graphql-utils
Yarn:
yarn add @jenyus-org/nestjs-graphql-utils
This will install @jenyus-org/nestjs-graphql-utils
and all its dependencies.
Getting Started
Typically, you will want to use the NestJS GraphQL-Utils package and its decorators to optimize your SQL SELECT
and JOIN
statements. You can use the @Selections()
decorator and wildcards to get the precise fields and relations to populate:
import { Selections } from "@jenyus-org/nestjs-graphql-utils";
import { Parent, Query, ResolveField, Resolver } from "@nestjs/graphql";
import { UserObject } from "../users/dto/user.object";
import { UsersService } from "../users/users.service";
import { PostObject } from "./dto/post.object";
import { Post } from "./entities/post.entity";
import { PostsService } from "./posts.service";
@Resolver(() => PostObject)
class PostsResolver {
constructor(
private postsService: PostsService,
private usersService: UsersService
) {}
@Query(() => [PostObject])
posts(
@Selections("posts", ["**.**"])
relations: string[],
@Selections("posts", ["*."]) fields: string[]
) {
return await this.postsService.findAll({ relations, fields });
}
@ResolveField(() => UserObject)
async author(@Parent() post: Post) {
if (post.author) {
return post.author;
}
return await this.usersService.findOne({ postId: post.id });
}
}