graphql-bookshelf

Some help defining GraphQL schema around BookshelfJS models

  • 所有者: brysgo/graphql-bookshelf
  • 平台:
  • 许可证:
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

GraphQL + BookshelfJS

npm package
Greenkeeper badge

peerDependency Status

This is an early version of the BookshelfType I wrote to help me link up my Bookshelf models (built on top of Postgres) to my GraphQL schema.

Install

npm install --save graphql-bookshelf

...

var BookshelfType = require('graphql-bookshelf');

Example use...

Use BookshelfType inside of GraphQLObjectType...

export default new GraphQLObjectType(BookshelfType({
  name: 'Classroom',
  description: 'Need I say more?',
  // ...and an object gets passed into the fields to help with your model.
  fields: (model) => ({

Simply wrap with model.attr()...

    id: model.attr({
      type: new GraphQLNonNull(GraphQLInt),
      description: 'The id of the classroom.',
      // ...and you don't need to resolve table attributes.
    }),

Or wrap with model.belongsTo()...

    subject: model.belongsTo({
      type: SubjectType, // And use the right association type...
      description: 'The subject of the classroom.',
      // And you get one-to-one relationships for free
    }),

Use model.hasMany()...

    students: model.hasMany({
      type: new GraphQLList(StudentType), // And make sure you use `GraphQLList`
      description: 'Students in the classroom.',
      // Now you have associated collections for free
    }),

Need to do more on your associated collection?

    homeworks: model.hasMany({
      type: new GraphQLList(HomeworkType),
      description: 'Homework submitted to the classroom (latest to oldest).',
      // Define a resolve function...
      resolve: (qb) => {
        // And get a sweet KnexJS query builder
        qb.orderBy('created_at', 'DESC');
      }
    }),

Or just leave it alone...

    size: {
      type: GraphQLInt,
      description: 'How many students there are in the class.',
      resolve: (model) => {
        // And do it the old fashioned way
        return model.studentCount();
      },
    }
  }),
}));

Are you using graphql-relay-js? Define some connection associations.

At the top:

import { connectionDefinitions, connectionArgs } from "graphql-relay";

And in your schema...

    homeworks: model.hasMany({
      type: connectionDefinitions({nodeType: HomeworkType}).connectionType,
      args: connectionArgs,
      description: 'Homework submitted to the classroom.'
    }),

Goals & Philosophy

This library is intended to keep it simple. Automatic generation of schema can leave use-cases out, while using bookshelf in every resolve calls for large amounts of repetitive boilerplate.

Another thing this library could help with is optimization. Turing graphql queries into database calls can be expensive, but using a layer in between can help make those optimizations that would get ugly and repetitive in every resolve.

See this example in action via the tests...

Contributing

  1. Install sqlite3, clone repo, npm install
  2. Create database in project root by running sqlite3 graphql_bookshelf.sqlite
  3. Run migrations, knex migrate:latest
  4. Run the tests with npm test
  5. When they pass, submit a PR

主要指标

概览
名称与所有者brysgo/graphql-bookshelf
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证
所有者活动
创建于2015-08-13 03:38:10
推送于2023-01-11 01:43:33
最后一次提交2019-09-13 10:53:09
发布数2
最新版本名称v1.0.1 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数184
关注者数4
派生数12
提交数88
已启用问题?
问题数14
打开的问题数8
拉请求数24
打开的拉请求数30
关闭的拉请求数99
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?