Home

Awesome

trailpack-graphql

NPM version Build status Dependency Status Code Climate

This is a draft proposal

This trailpack provides a Relay-compatible GraphQL implementation for Trails.

Install

$ npm install --save trailpack-graphql

Configure

// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-graphql')
  ]
}

Usage

Define Models

User model

class User extends Model {
  static schema () {
    return graphql`

      type User {
        id: ID!
        email: String!
        age: Int
        role: Role!
      }

      type Query {
        user (email: String): User
        allUsers (): [User]
      }
    `
  }

  static resolver () {
    return {
      user ({ email }) {
        // find user by email
      }
    }
  }
}

Role model

class Role extends Model {
  static schema () {
    return graphql`

      type Role {
        name: String!
        users: [User]
      }

      type Query {
        userRoles (id: ID!): [Role]
      }
    `
  }

  static resolver () {
    userRoles ({ userId }) {
      // select roles from role where role.user_id = userId
    }
  }
}

References