GithubHelp home page GithubHelp logo

diegofleitas / graphql-server-crud Goto Github PK

View Code? Open in Web Editor NEW

This project forked from charlie0077/graphql-server-crud

0.0 0.0 0.0 2.31 MB

Build GraphQL CRUD api server without boilerplate code

Home Page: https://nostalgic-perlman-fe9f48.netlify.com/guide/

License: MIT License

JavaScript 99.87% Shell 0.13%

graphql-server-crud's Introduction

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.

graphql-server-crud's People

Contributors

braincoke avatar charlie0077 avatar dependabot[bot] avatar sonnypgs avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.