npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

perch-query-builder

v1.3.2

Published

Dynamically build TypeORM queries based on GraphQL queries for NestJS and TypeORM

Downloads

27

Readme

About The Project

In order to harness the true potential of NestJS, GraphQL, and TypeORM, I needed a way to dynamically fetch child relationships based on the GraphQL query coming in, without having to write a resolver specific to each entity and limited in depth. I went searching for a solution and found this thread. A solution had been posted by david-eos, but it wasn't functional enough to integrate easily into a proper NestJS + TypeORM + @nestjs/graphql project. I re-worked his solution into a portable and lightweight package generalizable enough to be used by anyone.

Built With

Getting Started

This plugin is light-weight and easy to install and use.

Installation

npm i --save perch-query-builder@latest

Usage

Use inside your resolver

import {Args, Context, Info, Query, Resolver} from '@nestjs/graphql';
import {Repository} from 'typeorm';
import {InjectRepository} from "@nestjs/typeorm";
import {PerchQueryBuilder} from 'perch-query-builder';
import {BookArgs} from '../args';
import {Book} from '../entities';

@Resolver(of => Book)
export class BookResolver {

    constructor(
        @InjectRepository(Book),
        private bookRepository: Repository<Book>,
    ) {}

    @Query(of => [Book], {
        name: `Book`,
        description: `Generic Collection Query For Books`,
        nullable: true,
    })
    async queryBooks(
        @Context() ctx,
        @Args() args: BookArgs,
        @Info() info: GraphQLResolveInfo,
    ): Promise<Book[]> {
        // Simply pass your entity's repository, and the GraphQLResolve Info
        return await PerchQueryBuilder.find<Book>(this.bookRepository, info);
    }
}

Sorting

In order to add the ability to sort results by a given property, import the OrderByArgs argument class and declare it as an argument in your resolver

| option | type | description | | :---: | :---: | :---: | | _orderAscBy | string | Sorts the results as ascending from the value given | | _orderDescBy | string | Sorts the results as descending from the value given |

// rest of imports...
import {OrderByArgs} from "perch-query-builder";

// rest of resolver class...
    async queryBooks(
        @Context() ctx,
        @Args() args: BookArgs,
        // It is passed along side your other arguments
        @Args() orderByArgs: OrderByArgs,
        @Info() info: GraphQLResolveInfo,
    ): Promise<Book[]> {
        // There is nothing else to do, since OrderByArgs is only present to satisfy strict argument declaration requirements
        return await PerchQueryBuilder.find<Book>(this.bookRepository, info);
    }
// rest of resolver class...

Us in your query

{
    # Return Books sorted by title ascending
    Book(_orderAscBy: "title") {
        id
        title
    }
}

Pagination

Pagination is simple, though in a future feature "cursor" based pagination will be available as described in the GraphQL website

| option | type | description | | :---: | :---: | :---: | | _limit | integer | Sets the max results to return, maps to the TypeORM SelectQueryBuilder<T>.take(number) method | | _offset | integer | Sets the number of entities to skip before returning your query, maps to the TypeORM SelectQueryBuilder<T>.skip(number) method |

In order to add pagination to your resolver, add the PaginationArgs argument class and declare it as an argument in your resolver

// rest of imports...
import {PaginationArgs} from "perch-query-builder";

// rest of resolver class...
    async queryBooks(
        @Context() ctx,
        @Args() args: BookArgs,
        // It is passed along side your other arguments
        @Args() paginationArgs: PaginationArgs,
        @Info() info: GraphQLResolveInfo,
    ): Promise<Book[]> {
        // There is nothing else to do, since PaginationArgs is only present to satisfy strict argument declaration requirements
        return await PerchQueryBuilder.find<Book>(this.bookRepository, info);
    }
// rest of resolver class...

Us in your query

{
    # Return only 5 Books starting from the 10th book
    Book(_limit: 5, _offset: 10) {
        id
        title
    }
}

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request.

Acknowledgements

Credit to david-eos for writing the base functionality.

License

Distributed under the MIT License.

Contact

Wesley Young - @FullstackAttack - [email protected]

Project Link: https://github.com/wesleyyoung/perch-query-builder