forked from tjwebb/graphql-knex-resolver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (36 loc) · 1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict'
const Knex = require('knex')
const assert = require('assert')
const graphql = require('graphql')
const lib = require('./lib')
module.exports = {
getResolver (knex) {
return function (parent, args, options) {
console.log('getObjectResolver parent', parent)
console.log('getObjectResolver args', args)
console.log('getObjectResolver options', options)
return lib.QueryBuilder.buildQuery(options, knex)
.then(result => {
if (options.returnType instanceof graphql.GraphQLList) {
return result
}
else {
return result[0]
}
})
}
},
toSQL(gql, dialect, args) {
let ast
try {
ast = graphql.parse(gql)
}
catch (e) {
throw e
}
assert.equal(ast.kind, 'Document', 'The GraphQL query must be a complete Document')
const knex = Knex({ client: dialect })
const sql = lib.QueryBuilder.buildSQL(ast, knex)
return knex.raw(sql, args || { }).toString()
}
}