-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
46 lines (41 loc) · 1.27 KB
/
lib.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
const _ = require('lodash');
const {
GraphQLBoolean,
GraphQLString,
GraphQLNonNull,
GraphQLObjectType,
} = require('graphql');
const { AnyScalar } = require('graphql-anyscalar');
class LevelerObjectType extends GraphQLObjectType {
constructor(def) {
const levelerDef = Object.assign({}, def);
const fields = () => {
const _get = {
type: AnyScalar,
args: {
path: { type: new GraphQLNonNull(GraphQLString) },
defaultValue: { type: AnyScalar },
allowUndefined: { type: GraphQLBoolean },
},
resolve: (obj, { path, defaultValue = undefined, allowUndefined = false }) => {
const val = _.get(obj, path, defaultValue);
if (!allowUndefined && typeof val === 'undefined') {
throw new Error(`The "${path}" property does not exist.`);
}
return val;
},
};
const _root = {
type: this,
resolve: obj => obj,
};
return { _get, _root };
};
levelerDef.fields = (defFields => () => {
const resolvedFields = typeof defFields === 'function' ? defFields() : defFields;
return Object.assign({}, fields(), resolvedFields);
})(levelerDef.fields);
super(levelerDef);
}
}
module.exports = { LevelerObjectType };