JSPM

@jenyus-org/nestjs-graphql-utils

1.6.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4634
  • Score
    100M100P100Q150283F
  • License MIT

NestJS utilities and decorators built around @jenyus-org/graphql-utils.

Package Exports

  • @jenyus-org/nestjs-graphql-utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@jenyus-org/nestjs-graphql-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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