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

graphql-fields-collector

v1.2.1

Published

This package helps to bind client requested grapqhl fields with database queries.

Downloads

205

Readme

Graphql fields collector

Installation:

npm install graphql-fields-collector

Why was it made?

Typically, when creating a GraphQL resolver, we need to run a query against the database and return the data. However, sometimes the client may only require a subset of the data, which means unnecessary fields are being retrieved and transferred, leading to slower query times.

To address this issue, this package has been developed that can improve query performance. By reducing the number of fields retrieved from the database, the query can be processed more quickly.

Basic Usage

To begin, let's create the collector:

import { GraphQLCollector } from "graphql-fields-collector";

type User = {
  id: string;
  name: string;
};

type Post = {
  id: string;
  title: string;
  content: string;
  author: User;
  info: {
    createdAt: Date;
  };
};

type Context = {
  select: string[];
  leftJoin: string[][];
};

const postCollector = new GraphQLCollector<Context, Post>({
  id: (ctx) => ctx.select.push("post.id"),
  title: (ctx) => ctx.select.push("post.title"),
  content: (ctx) => ctx.select.push("post.content"),
  author: (ctx) => {
    ctx.jeftJoin.push(["user", "user.id", "post.author_id"]);

    return {
      id: (ctx) => ctx.select.push("user.id as authorId"),
      name: (ctx) => ctx.select.push("user.name as authorName"),
    };
  },
  info: {
    createdAt: (ctx) => ctx.select.push("post.createdAt"),
  },
});

Next, we can utilize it within the GraphQL resolver:

const resolvers = {
  Query: {
    // ...
    posts(_parent, _args, _context, info: GraphQLResolveInfo): Post[] {
      const { select, leftJoin } = postCollector.collect(
        { select: [], leftJoin: [] },
        info
      );

      const query = leftJoin.reduce(
        (query, joinArgs) => query.leftJoin(...joinArgs),
        orm.select(select).from("post")
      );

      // execute query, prepare dto and return
    },
    // ...
  },
};

Let's take the following GraphQL query

query {
    posts {
        id
        title
        author {
            name
        }
    }
}

Than we get the following from the context:

const select = ["post.id", "post.title", "user.name"];
const leftJoin = [["user", "user.id", "post.author_id"]];

Context and all operations are up to you. Change it freely!

Default tools

- Collect

Collect works with array-like fields in context

const postCollector = new GraphQLCollector<Context, Post>({
  id: Collect("select", "post.id"),
  title: Collect("select", "post.title"),
  content: Collect("select", "post.content"),
  author: (ctx) => {
    Collect("jeftJoin", ["user", "user.id", "post.author_id"])(ctx);

    return {
      id: Collect("select", "user.id as authorId"),
      name: Collect("select", "user.name as authorName"),
    };
  },
  info: {
    createdAt: Collect("select", "post.createdAt"),
  },
});

Also there are few wrappers to make code readable

- Select

Wrap Collect("select", ...selects) into Select(...selects)

const postCollector = new GraphQLCollector<Context, Post>({
  id: Select("post.id"),
  title: Select("post.title"),
  content: Select("post.content"),
  author: (ctx) => {
    Collect("jeftJoin", ["user", "user.id", "post.author_id"])(ctx);

    return {
      id: Select("user.id as authorId"),
      name: Select("user.name as authorName"),
    };
  },
  info: {
    createdAt: Select("post.createdAt"),
  },
});

- LeftJoin

Wrap Collect("jeftJoin", [...leftJoinArgs]) into LeftJoin(...leftJoinArgs)

const postCollector = new GraphQLCollector<Context, Post>({
  id: Select("post.id"),
  title: Select("post.title"),
  content: Select("post.content"),
  author: (ctx) => {
    LeftJoin("user", "user.id", "post.author_id")(ctx);

    return {
      id: Select("user.id as authorId"),
      name: Select("user.name as authorName"),
    };
  },
  info: {
    createdAt: Select("post.createdAt"),
  },
});

- Compose

Compose allows us to call field handlers one by one:

const postCollector = new GraphQLCollector<Context, Post>({
  id: Select("post.id"),
  title: Select("post.title"),
  content: Select("post.content"),
  author: Compose(LeftJoin("user", "user.id", "post.author_id"), () => ({
    id: Select("user.id as authorId"),
    name: Select("user.name as authorName"),
  })),
  info: {
    createdAt: Select("post.createdAt"),
  },
});

- GraphQLCollectContext

In addition, if you don't require auxiliary functionality, you can simply use the GraphQLCollectContext with the tools mentioned above.

Remember, you have the option to develop your own tools with customized context. Here's a quick example:

type Ctx = {
  query: Knex.QueryBuilder;
};

const Select =
  (...selects: string[]) =>
  (ctx: Ctx) => {
    ctx.query = ctx.query.select(...selects);
  };