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-server-crud

v1.0.5

Published

Create GraphQL API server CRUD without boilerplate code

Downloads

56

Readme

npm version CircleCI codecov

Please go to https://nostalgic-perlman-fe9f48.netlify.com for the complete documentation.

Introduction

This is a lightweight javascript framework/library to help you build a GraphQL server efficiently. It aims to reduce your code size, save you time and gives you the flexibilities you need.

Philosophy

  • Reduce the CRUD related code you need to write.
  • It is code, not black box service, you have full control about logic, deployment, etc.
  • You don't need to learn new random definitions: syntax, directive, magic, etc.
  • It coexists with your existing project.
  • Support multiple databases: it should support what knex supports.
  • It is not discouraged to build your own complicate queries to support complicate use case.

Getting Started

The following is to give you a quick idea of how to get started. You may want to refer to a full example here, which contains test data.

Install package

npm install graphql-server-crud

Define the "model"

const { ModelBase } = require('graphql-server-crud')
const { knex } = require('../db') // this is your typical knex db

class Company extends ModelBase {
  knex = knex
  table = 'companies'
  fields = {
    id: 'Int',
    domain: 'String',
    public: 'Boolean',
    phone: 'String',
    sales: 'Float',
    customers: 'Int'
  }
}

Add auto-generated typeDefs and resolvers to your root

Add these followinng lines to your root typeDefs and resolvers variables.

// Add default typeDefs and resolvers
// You only need to do this once regardless of the number of models you have
const { addTypeDefs, addResolvers } = require('graphql-server-crud')
const models = require('./models')
addTypeDefs(typeDefs, models)
addResolvers(resolvers, models)

You can check a full example file here.

That is it!

You have a basic CRUD endpoint for Company now.

Run a simple query:

Query

The schema:

Schema

A more complicate query example

query {
  # queryAuthorJoinPost is a derived table(runtime view)
  queryAuthorJoinPost(
    # complicate filter condition
    where: {
      _and: [
        { _or: [{ id: { gt: 10 }, email: { gt: "k" } }, { id: { lt: 5 } }] }
        { first_name: { gt: "OK" } }
      ]
    }
    # top level orderBy
    orderBy: [{ column: "score__max", order: "desc" }]
    limit: 10
    offset: 0
    # support aggregation
    groupBy: ["id", "email"]
    having: {
      age__avg: { gt: 30 }
      id: { nin: [1, 2, 6] }
      email: { null: false }
    }
  ) {
    id
    email
    # aggregations
    age__avg
    age__count
    age__count_distinct
    score__max
    # nested field in group by
    review(
      on: { star: { gt: 2 } }
      limit: 5
      offset: 1
      orderBy: [{ column: "star", order: "desc" }]
    ) {
      id
      star
    }
  }
}

How it works

You define a model. The library generates common GraphQL schema, resolvers logic for you. To support complicate search queries(filter, groupBy, join, nested fields, etc), it also has a built-in compiler to compile the filter input to knex code. A ModelBase class is provided to you so that you can build your custom logic on top of it. You can also use the model as your database client in any place of your logic.

Features

  • logic generation for schema
  • logic generation for common resolvers
  • support queries: get, list(search by filters)
  • support mutation: insert, delete, update, bulk insert, bulk delete, bulk update
  • support common where filters: =, <>, >, <, between, in, or, and, etc.
  • support common aggregations: groupBy, having, sum, avg, min, max, count, distinct, etc.
  • support common components in a query: offset, limit, order by, etc.
  • support nested objects queries
  • no N+1 problem for common queries
  • coexists with your current code, custom schema, custom resolver.
  • it does not care if you are using graphql-server, graphql-lambda, or graphql-express, etc.

Please go to https://nostalgic-perlman-fe9f48.netlify.com for the complete documentation.

Contributing

You are always welcome to contribute to the project.

Thanks for all your wonderful PRs, issues and ideas.