forked from Cardinal90/graphql-union-input-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (137 loc) · 5.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var GraphQLScalarType = require('graphql-sync').GraphQLScalarType;
var GraphQLInputObjectType = require('graphql-sync').GraphQLInputObjectType;
var GraphQLString = require('graphql-sync').GraphQLString;
var GraphQLList = require('graphql-sync').GraphQLList;
var isValidLiteralValue = require('graphql/utilities').isValidLiteralValue;
var valueFromAST = require('./valueFromAst').valueFromAST;
var astFromValue = require('./astFromValue').astFromValue;
var GraphQLError = require('graphql/error').GraphQLError;
function helper(name, type) {
"use strict";
return (new GraphQLInputObjectType({
name: name,
fields: function() {
return {
_type_: {
type: GraphQLString
},
_value_: {
type: type
}
};
}
}));
}
/**
* UnionInputType - Union Input Type for GraphQL
*
* @param {object} options see below
* @return {any} returns validated and parsed value
*/
module.exports = function UnionInputType(options) {
"use strict";
/**
* @param {array} options.name Name for the union type. Must be unique in your schema. Has to be used in queries to nested unions.
*/
var name = options.name;
/**
* @param {array|object} options.inputTypes Optional. Either array of GraphQLInputObjectType objects or UnionInputTypes (which are Scalars really)
* or object with {name:GraphQLInputObjectType} pairs.
* Will be ignored if resolveType is provided.
*/
var referenceTypes = options.inputTypes;
/**
* @param {string} options.typeKey Optional. If provided, is used as a key containing the type name. If not, the query argument must
* contain _type_ and _value_ parameteres in this particular order
*/
var typeKey = options.typeKey;
/**
* @param {function} options.resolveType Optional. If provided, is called with a key name and must return corresponding GraphQLInputObjectType or null
*/
var resolveType = options.resolveType;
/**
* @param {function} options.resolveType Optional. If provided, is called with full AST for the input argument and must return
* corresponding GraphQLInputObjectType or null
*/
var resolveTypeFromAst = options.resolveTypeFromAst;
if (!resolveType && !resolveTypeFromAst) {
if (Array.isArray(referenceTypes)) {
referenceTypes = referenceTypes.reduce(function(acc, refType) {
if (!(refType instanceof GraphQLInputObjectType || refType instanceof GraphQLScalarType)) {
throw (new GraphQLError(name + '(UnionInputType): all inputTypes must be of GraphQLInputObjectType or GraphQLScalarType(created by UnionInputType function)'));
}
acc[refType.name] = (typeKey ? refType : helper(refType.name, refType));
return acc;
}, {});
} else if (referenceTypes !== null && typeof referenceTypes === 'object') {
Object.keys(referenceTypes).forEach(function(key) {
if (!(referenceTypes[key] instanceof GraphQLInputObjectType || referenceTypes[key] instanceof GraphQLScalarType)) {
throw (new GraphQLError(name + '(UnionInputType): all inputTypes must be of GraphQLInputObjectType or GraphQLScalarType(created by UnionInputType function'));
}
referenceTypes[key] = typeKey ? referenceTypes[key] : helper(key, referenceTypes[key]);
});
}
}
var union = (new GraphQLScalarType({
name: name,
serialize: function(value) {
return value;
},
parseValue: function(value) {
var inputType, ast, result;
if (typeKey && value[typeKey]) {
inputType = referenceTypes[value[typeKey]];
}
ast = astFromValue(value, inputType);
result = valueFromAST(ast, inputType);
result[typeKey] = value[typeKey];
return result;
},
parseLiteral: function(ast) {
var type, inputType;
if (typeof resolveTypeFromAst === 'function') {
inputType = resolveTypeFromAst(ast);
} else {
if (typeKey) {
try {
for (var i = 0; i < ast.fields.length; i++) {
if (ast.fields[i].name.value === typeKey) {
type = ast.fields[i].value.value;
break;
}
}
if (!type) {
throw (new Error);
}
} catch (err) {
throw new GraphQLError(name + '(UnionInputType): Expected an object with "' + typeKey + '" property');
}
} else {
try {
if (ast.fields[0].name.value === '_type_' && ast.fields[1].name.value === '_value_') {
type = ast.fields[0].value.value;
} else {
throw (new Error);
}
} catch (err) {
throw new GraphQLError(name + '(UnionInputType): Expected an object with _type_ and _value_ properties in this order');
}
}
if (typeof resolveType === 'function') {
inputType = resolveType(type);
if (!typeKey) {
inputType = helper(type, inputType);
}
} else {
inputType = referenceTypes[type];
}
}
if (isValidLiteralValue(inputType, ast).length == 0) {
return valueFromAST(ast, inputType);
} else {
throw new GraphQLError('expected type ' + type + ', found ' + ast.loc.source.body.substring(ast.loc.start, ast.loc.end));
}
}
}));
return union;
};