-
Notifications
You must be signed in to change notification settings - Fork 73
/
query.js
34 lines (31 loc) · 1.16 KB
/
query.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
'use strict';
var b = require('ast-types').builders;
var buildResolver = require('./resolver');
var getPrimaryKey = require('../../util/get-primary-key');
var typeMap = {
string: 'GraphQLString',
integer: 'GraphQLInt',
float: 'GraphQLFloat'
};
module.exports = function buildQuery(type, data, opts) {
var model = data.models[type.name];
var primaryKey = getPrimaryKey(model) || {};
var keyName = primaryKey.name;
var keyType = typeMap[primaryKey.type];
return b.objectExpression([
b.property('init', b.identifier('type'), b.identifier(type.varName)),
b.property('init', b.identifier('args'), b.objectExpression(keyName ? [
b.property('init', b.identifier('id'), b.objectExpression([
b.property('init', b.identifier('name'), b.literal(keyName)),
b.property('init', b.identifier('type'), b.newExpression(
b.identifier('GraphQLNonNull'),
[b.identifier(keyType)]
))
]))
] : []))
].concat(opts.outputDir ? [b.property(
'init',
b.identifier('resolve'),
buildResolver(type)
)] : []));
};