GraphQL + BookshelfJS
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
...
Simply wrap with model.attr()
...
id: model
Or wrap with model.belongsTo()...
subject: model
Use model.hasMany()...
students: model
Need to do more on your associated collection?
homeworks: model
Or just leave it alone...
size: type: GraphQLInt description: 'How many students there are in the class.' { // And do it the old fashioned way return model; } })}));
Are you using graphql-relay-js? Define some connection associations.
At the top:
;
And in your schema...
homeworks: model
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
- Install sqlite3, clone repo, npm install
- Create database in project root by running
sqlite3 graphql_bookshelf.sqlite
- Run migrations,
knex migrate:latest
- Run the tests with
npm test
- When they pass, submit a PR