Skip to content

Instantly share code, notes, and snippets.

@RedShift1
Created December 31, 2017 14:40
Show Gist options
  • Save RedShift1/cb60eb8fc67fbfc7cd823e907dd445d9 to your computer and use it in GitHub Desktop.
Save RedShift1/cb60eb8fc67fbfc7cd823e907dd445d9 to your computer and use it in GitHub Desktop.
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
}
`
const joinMonsterAdapt = require('join-monster-graphql-tools-adapter')
const joinMonster = require('join-monster').default
// node drivers for talking to SQLite
const dbPromise = require('sqlite').open('./test1-data.sl3');
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) {
dbPromise.then(
(db) => {
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'
}
}
}
})
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 => console.log(doSomethingCrazy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment