-
Notifications
You must be signed in to change notification settings - Fork 73
/
schema.js
50 lines (47 loc) · 1.59 KB
/
schema.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
'use strict';
var camelCase = require('lodash/string/camelCase');
var map = require('lodash/collection/map');
var b = require('ast-types').builders;
var buildVar = require('./variable');
var buildQuery = require('./query');
var buildFieldWrapperFunction = require('./field-wrapper-function');
module.exports = function(data, opts) {
var queryFields = [];
if (opts.relay) {
queryFields.push(b.property(
'init',
b.identifier('node'),
b.identifier('nodeField')
));
} else {
queryFields = map(data.types, function(type) {
return b.property(
'init',
b.identifier(camelCase(type.name)),
buildQuery(type, data, opts)
);
});
}
return buildVar('schema',
b.newExpression(
b.identifier('GraphQLSchema'),
[b.objectExpression([
b.property(
'init',
b.identifier('query'),
b.newExpression(
b.identifier('GraphQLObjectType'),
[b.objectExpression([
b.property('init', b.identifier('name'), b.literal('RootQueryType')),
b.property('init', b.identifier('fields'), buildFieldWrapperFunction(
'RootQuery',
b.objectExpression(queryFields),
opts
))
])]
)
)
])]
)
);
};