-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
66 lines (54 loc) · 1.39 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'
/**
* Module dependendies.
*/
const createContext = require('./context')
const schema = require('./schema')
const ready = require('domready')
const graph = require('graphql').graphql
// graphql(schema, ...) lazy bind
const G = graph.bind(graph, schema)
/**
* Returns a function suitable for running a graphql
* query for the created context.
*
* @param {Object} opts
* @param {Object} [ctx]
* @return {Function}
*/
module.exports = createWrapper
function createWrapper(opts, ctx) {
const context = ctx || createContext(opts)
const wrap = (v) => 'function' == typeof v ? v : () => v
query.graph = G
query.render = render
query.context = context
return query
function query(input) {
if (String(input).match(/IntrospectionQuery/i)) {
return G(input)
} else {
return G(wrap(input)(context),
context,
context,
context.store)
.then(respond)
.then(render)
}
}
function respond(res) {
if (res.errors) { throw res.errors[0] }
else { return res.data }
}
function render() {
const renderer = context.restore('renderer')
const camera = context.restore('camera')
const scene = context.restore('scene')
if (renderer && camera && scene) {
if (document.body.contains(renderer.domElement)) {
renderer.render(scene, camera);
}
}
return context
}
}