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-sequelize-query-loader

v1.0.6

Published

Convert GraphQL Query to query options for Sequelize models facilitating eagerloading of associated resources.

Downloads

11

Readme

graphql-sequelize-query-loader

Convert GraphQL Query to query options for Sequelize models facilitating eagerloading of associated resources.

codecov Build Status codebeat badge License

Overview

Essentially, the tool expects that all sequelize models have set on them the appropriate associations to all related resources. Then, when given a GraphQL query, it parses the info object made available as a parameter in GraphQL Query Resolvers, producing an object which includes ONLY the selected resources and with ONLY the specified attributes. This solves for the N + 1 problem with database querying, as well as the issue of over-fetching of table attributes in result sets.

Installation

$ npm install --save graphql-sequelize-query-loader

Pre-requisites

In order to use the helper to eagerload related entities, you need to have setup the associations on the Sequelize Models.

Features

  • maps the selected fields (in all models found in a GraphQL query) to the attributes option for Sequelize `Model.Find
  • maps included models found in the GraphQL query to include option properties
  • converts scope argument in GraphQL query and turns them to where option properties

Usage

import queryLoader from 'graphql-sequelize-query-loader';
import models from 'path/to/sequelize/models';

/**
 * dictionary of what sequelize models respectively match the named resources
 * captured on the graphql schema
 */
const includeModels = {
  articles: models.Article,
  article: models.Article,
  owner: models.User,
  category: models.category,
  comments: models.Comment,
};

/* 
 * Initiliase the loader with "includeModels", 
 * a map of all models referenced in GraphQL with their respective Sequelize Model
 */
queryLoader.init({ includeModels });

/**
 * GraphQL
 */
Query: {
  articles: async (parent, args, { models }, info) => {
    const { Article } = models;
    const queryOptions = queryLoader.getFindOptions({ model: Article, info });
    const articles = await Article.findAll(queryOptions);
    return articles;
  },
},

Examples

You can find examples in the demo directory. It contains migrations, models and seeds setup for testing out the app. It also contains graphql schemas and resolvers with examples of how the queryLoader utility is used

Testing the Demo

On the git repo You can quickly test the demo and play with the code using Gitpod. Gitpod is a full blown IDE created on the fly from a git repository. It allows us to test a github project with just the browser. Learn more about gitpod OR install the chrome extension

  • Environment should be set to development. This is already setup in the package.json scripts
  • Setup DATABASE_URL: You can use this database created with elephantsql. postgres://cigjzbzt:[email protected]:5432/cigjzbzt
  • Run migrations and seeds - npm run db:seed (optional): This will create tables and seed them with dummy data. If you are testing the project locally, you will definitely need to this the first time, but if you are testing it online with gitpod, the database has already been migrated and seeded, so you don't have to do this step.
  • Start the development server: npm start

Scope Argument

This allows us to query models and return records matching certain conditions. Currently, we support a small set of Sequelize operators with the scope argument

Usage

In a GraphQL query, provide scope argument with the following pattern field|operator|value

Example

articles(scope: 'title|like|%graphql%') {
  id
  title
}

Supported Scope Operators

eq, gt, gte, like, lt, lte, ne

Author

Samuel Osuh @jsamchineme