Last active
July 2, 2017 15:02
-
-
Save vinicius73/aa595f3ae5d1826f61df85e8c62cf672 to your computer and use it in GitHub Desktop.
autoload graphql files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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