All Projects β†’ join-monster β†’ Join Monster Graphql Tools Adapter

join-monster / Join Monster Graphql Tools Adapter

Use Join Monster to fetch your data with Apollo Server.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Join Monster Graphql Tools Adapter

Graphql Cli
πŸ“Ÿ Command line tool for common GraphQL development workflows
Stars: ✭ 1,814 (+1295.38%)
Mutual labels:  graphql, apollo, schema
Babel Plugin Import Graphql
Enables import syntax for .graphql and .gql files
Stars: ✭ 284 (+118.46%)
Mutual labels:  graphql, apollo, schema
Countries
🌎 Public GraphQL API for information about countries
Stars: ✭ 156 (+20%)
Mutual labels:  graphql, apollo, schema
Graphback
Graphback - Out of the box GraphQL server and client
Stars: ✭ 323 (+148.46%)
Mutual labels:  graphql, apollo, schema
Sqldatasource
SQL DataSource for Apollo GraphQL projects
Stars: ✭ 176 (+35.38%)
Mutual labels:  graphql, apollo, sql
Rdbms To Graphql
A Java CLI program that generates a GraphQL schema from a JDBC data source.
Stars: ✭ 44 (-66.15%)
Mutual labels:  graphql, schema, sql
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+1524.62%)
Mutual labels:  schema, sql
Thorium
Platform for starship simulator controls
Stars: ✭ 109 (-16.15%)
Mutual labels:  graphql, apollo
Create Apollo App
Create Apollo App is a command-line tool designed to generate fully-configured starter Apollo GraphQL projects with essential dependencies for developing web, server and mobile applications and zero build configuration.
Stars: ✭ 110 (-15.38%)
Mutual labels:  graphql, apollo
Universal React Apollo Example
Universal React Apollo App (GraphQL) consuming: https://github.com/WeLikeGraphQL/wordpress-graphql-api-example!
Stars: ✭ 117 (-10%)
Mutual labels:  graphql, apollo
Guide
πŸ“– The GraphQL Guide website
Stars: ✭ 104 (-20%)
Mutual labels:  graphql, apollo
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+8336.15%)
Mutual labels:  graphql, schema
React Graphql Github Apollo
πŸš€ A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+1102.31%)
Mutual labels:  graphql, apollo
Meteor Integration
πŸš€ meteor add apollo
Stars: ✭ 107 (-17.69%)
Mutual labels:  graphql, apollo
Shapeshifter
🐺 Generate relational schemas, PropTypes, Flow aliases, and TypeScript interfaces from JSON or GraphQL schematic files.
Stars: ✭ 105 (-19.23%)
Mutual labels:  graphql, schema
Apollo Link
πŸ”— Interface for fetching and modifying control flow of GraphQL requests
Stars: ✭ 1,434 (+1003.08%)
Mutual labels:  graphql, apollo
Postguard
πŸ› Statically validate Postgres SQL queries in JS / TS code and derive schemas.
Stars: ✭ 104 (-20%)
Mutual labels:  schema, sql
Livepeerjs
JavaScript tools and applications that interact with Livepeer's smart contracts and peer-to-peer network
Stars: ✭ 116 (-10.77%)
Mutual labels:  graphql, apollo
Dataloader
DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
Stars: ✭ 11,040 (+8392.31%)
Mutual labels:  graphql, batch
Goofi
✨Let's contribute to OSS. Here is how to find good first issues in GitHub. "goofi" is an abbreviation of "good first issues".
Stars: ✭ 125 (-3.85%)
Mutual labels:  graphql, apollo

Join Monster GraphQL Tools Adapter

Use Join Monster's SQL generation and query batching powers with the Apollo graphql-tools server package.

What's this package for?

Suppose you have a GraphQL schema for a forum website, defined with the Schema Language like so:

const typeDefs = `
type Comment {
  id: Int!,
  body: String!,
  postId: Int,
  authorId: Int,
  archived: Boolean
}

type Post {
  id: Int!,
  body: String!,
  authorId: Int,
  numComments: Int!,
  comments: [Comment]
}

type User {
  id: Int!,
  email: String!,
  fullName: String!,
  favNums: [Int],
  posts: [Post]
}

type Query {
  user(id: Int!): User
}
`

module.exports = typeDefs

When using graphql-js, the reference implementation, you tag the Type constructors with extra metadata to configure Join Monster. The schema language does not allow adding arbitrary properties to the type definitions.

This package let's you add those tags without messing with the internals of the built schema object. Once you familiarize yourself with Join Monster's API, you can use all the same properties by passing it to this function.

const joinMonsterAdapt = require('join-monster-graphql-tools-adapter')
const typeDefs = require('../path/to/types')

const joinMonster = require('join-monster').default
// node drivers for talking to SQLite
const db = require('sqlite')
const { makeExecutableSchema } = require('graphql-tools')

const resolvers = {
  Query: {
    // call joinMonster in the "user" resolver, and all child fields that are tagged with "sqlTable" are handled!
    user(parent, args, ctx, resolveInfo) {
      return joinMonster(resolveInfo, ctx, sql => {
        return db.all(sql)
      }, { dialect: 'sqlite3' })
    }
  },
  User: {
    // the only field that needs a resolver, joinMonster hydrates the rest!
    fullName(user) {
      return user.first_name + ' ' + user.last_name
    }
  }
}

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
})

// tag the schema types with the extra join monster metadata
joinMonsterAdapt(schema, {
  Query: {
    fields: {
      // add a function to generate the "where condition"
      user: {
        where: (table, args) => `${table}.id = ${args.id}`
      }
    }
  },
  User: {
    // map the User object type to its SQL table
    sqlTable: 'accounts',
    uniqueKey: 'id',
    // tag the User's fields
    fields: {
      email: {
        sqlColumn: 'email_address'
      },
      fullName: {
        sqlDeps: [ 'first_name', 'last_name' ],
      },
      posts: {
        sqlJoin: (userTable, postTable) => `${userTable}.id = ${postTable}.author_id`,
      }
    }
  },
  Post: {
    sqlTable: 'posts',
    uniqueKey: 'id',
    fields: {
      numComments: {
        // count with a correlated subquery
        sqlExpr: table => `(SELECT count(*) FROM comments where ${table}.id = comments.post_id)`
      },
      comments: {
        // fetch the comments in another batch request instead of joining
        sqlBatch: {
          thisKey: 'post_id',
          parentKey: 'id'
        }
      }
    }
  },
  Comment: {
    sqlTable: 'comments',
    uniqueKey: 'id',
    fields: {
      postId: {
        sqlColumn: 'post_id'
      },
      authorId: {
        sqlColumn: 'author_id'
      }
    }
  }
})

Now that our schema is Join-monsterized, we are ready to start executing some queries!

const { graphql } = require('graphql')

const query = `{
  user(id: 1) {
    id
    fullName
    email
    posts {
      id
      body
      numComments
      comments {
        id
        body
        authorId
        archived
      }
    }
  }
}`
graphql(schema, query).then(doSomethingCrazy)

Advisory

There is a known issue (see #4) that passing the logger in graphql-tools makeExecutableSchema breaks automatic fetching of default column values. For the time being, it is suggested to remove the logger or add sqlColumn to every field.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].