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-bookshelf

v1.1.0

Published

Some help defining schema around bookshelfjs models.

Downloads

650

Readme

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