@jenyus-org/nestjs-graphql-utils
TypeScript icon, indicating that this package has built-in type declarations

1.6.4 • Public • Published

nestjs-graphql-utils

License: MIT NPM Release NPM Downloads NPM Type Definitions

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

Package Sidebar

Install

npm i @jenyus-org/nestjs-graphql-utils

Weekly Downloads

901

Version

1.6.4

License

MIT

Unpacked Size

44 kB

Total Files

51

Last publish

Collaborators

  • dan6erbond
  • doemuu