GithubHelp home page GithubHelp logo

paul-schwendenman / graphql-knex-resolver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tjwebb/graphql-knex-resolver

0.0 2.0 0.0 29 KB

Knex Resolver for GraphQL. Allows you to define GraphQL schemas and run queries in a relational database

License: MIT License

JavaScript 100.00%

graphql-knex-resolver's Introduction

graphql-knex-resolver

NPM version Build status Dependency Status Code Climate

GraphQL Resolver built with Knex. Can be used to parse GraphQL ASTs into SQL, and as a resolver method standin in a GraphQL schema. Supports whichever databases are supported by Knex.

Install

$ npm install --save graphql-knex-resolver

Usage

Do the stuff you'd normally do, but use the provided resolver method in your GraphQL schema.

1. Initialize the Resolver

const Resolver = require('graphql-knex-resolver')

// setup knex
const gql = require('graphql')
const knex = require('knex')({
  client: 'pg',
  connection: {
    // ...
  }
})
const resolver = new Resolver(knex)

2. Define the Schema

// create the GraphQL schema using the resolver
const User = new gql.GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: {
      type: gql.GraphQLID
    },
    username: {
      type: gql.GraphQLString
    },
    roles: {
      type: new gql.GraphQLList(Role),
      resolve: resolver.relation({
        foreignKey: 'user_id'
      })
    }
  })
})
const Role = new gql.GraphQLObjectType({
  name: 'Role',
  fields: () => ({
    id: {
      type: gql.GraphQLID
    },
    name: {
      type: gql.GraphQLString
    }
  })
})
const schema = new gql.GraphQLSchema({
  query: new gql.GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      user: {
        type: User,
        resolve: resolver.object(),
        args: {
          // ...
        }
      },
      role: {
        type: Role,
        resolve: resolver.object(),
        args: {
          // ...
        }
      }
    })
  })
})

3. Execute a Query

const findUserByUsername = `{
  user (username: $username) {
    username
    roles {
      name
    }
  }
}`
return gql.graphql(userSchema, findUserByUsername, {
    username: "tjwebb"
  })
  .then(results => {
    console.log(results)
    // results = {
    //   data: {
    //     user: {
    //       username: 'tjwebb',
    //       roles: [
    //         { name: 'admin' }
    //       ]
    //     }
    //   }
    // }
  })

API

new Resolver(knex)

Prepare a new GraphQL Query Resolver

object()

Return an object resolver

relation(options)

Return a relation resolver.

static .toSQL(query, dialect, args)

Translates a GraphQL query into SQL, irrespective of schema. Uses the root field name as the table.

dialect is one of (docs):

  • pg
  • mysql
  • sqlite3
  • oracle
  • mariasql

Example

Using the findUserByUsername query above:

Simple Select Statement
const sql = resolver.toSQL(findUserByUsername, 'pg', {
  username: 'tjwebb'
})
// sql = select "username", from "user" where "name" = 'tjwebb'
Select Where In List Statement
const sql = resolver.toSQL(queries.parameterizedWithListType, 'pg', {
  username: [ 'tjwebb', 'admin' ]
})
// sql = select "username" from "user" where "name" = ANY ('{"tjwebb","admin"}')

Table name is inferred to be user since the root field is user. The following query would use the table name "foo":

const findUserByUsername = `{
  foo (username: $username) {
    id
    username
  }
}`

graphql-knex-resolver's People

Contributors

tjwebb avatar

Watchers

James Cloos avatar  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.