This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
182 lines (167 loc) · 6.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var GraphQLScalarType = require('graphql').GraphQLScalarType;
var GraphQLInputObjectType = require('graphql').GraphQLInputObjectType;
var GraphQLString = require('graphql').GraphQLString;
var isValidLiteralValue = require('graphql').isValidLiteralValue;
var coerceValue = require('graphql').coerceValue;
var valueFromAST = require('graphql').valueFromAST;
var GraphQLError = require('graphql').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.resolveTypeFromAst Optional. If provided, is called with full AST for the input argument and must return
* corresponding GraphQLInputObjectType or null
*/
var resolveTypeFromAst = options.resolveTypeFromAst;
/**
* @param {function} options.resolveTypeFromValue Optional. If provided, is called with a variable value and must return
* corresponding GraphQLInputObjectType or null
*/
var resolveTypeFromValue = options.resolveTypeFromValue;
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 type, inputType, ast;
if (typeof resolveTypeFromValue === 'function') {
inputType = resolveTypeFromValue(value);
} else {
if (typeKey) {
if (value[typeKey]) {
type = value[typeKey];
} else {
throw new GraphQLError(name + '(UnionInputType): Expected an object with "' + typeKey + '" property');
}
} else if (value._type_ && value._value_) {
type = value._type_;
}
else {
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];
}
}
const errors = coerceValue(value, inputType).errors;
if (!errors) {
return value;
} else {
const errorString = errors.map((error) => {
return "\n" + error.message;
}).join('');
throw new GraphQLError(errorString);
}
},
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;
};