Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Last active July 2, 2017 15:02
Show Gist options
  • Save vinicius73/aa595f3ae5d1826f61df85e8c62cf672 to your computer and use it in GitHub Desktop.
Save vinicius73/aa595f3ae5d1826f61df85e8c62cf672 to your computer and use it in GitHub Desktop.
autoload graphql files
const { readdirSync, readFileSync } = require('fs')
const path = require('path')
const isGraphQLFile = fileName => fileName.endsWith('.graphql')
const readFile = fileName => readFileSync(path.join(__dirname, fileName), 'utf8')
const files = readdirSync(__dirname)
.filter(isGraphQLFile)
.map(readFile)
module.exports = files
const { singularize } = require('inflection')
const { defaultsDeep } = require('lodash')
const db = require('..')
const tables = require('./tables')
const makeResolverList = table => (root, args, context) => {
const order = defaultsDeep({ }, args.order || {}, table.order)
return db(table.name)
.where(args.filter || {})
.orderBy(order.column, order.direction)
.where(table.where)
}
const makeResolverSingle = ({ name, where = {} }) => (root, { id }, context) => {
return db(name)
.where({ id })
.where(where)
.first()
}
const RootQuery = tables.reduce((acc, table) => {
acc[table.name] = makeResolverList(table)
acc[singularize(table.name)] = makeResolverSingle(table)
return acc
}, {})
module.exports = RootQuery
const { defaultsDeep } = require('lodash')
const { map } = require('ramda')
const defaultTableOptions = {
where: {},
order: {
column: 'name',
order: 'ASC'
}
}
const adjustTable = table => defaultsDeep({ }, table, defaultTableOptions)
const adjustTables = map(adjustTable)
module.exports = adjustTables([{
name: 'clients',
where: {
deleted: false
}
}, {
name: 'users'
}, {
name: 'holidays'
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment