-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.js
127 lines (116 loc) · 3.33 KB
/
types.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
var graphql = require('graphql');
// Example http://graphql.org/graphql-js/type/#graphqlscalartype
/*
var OddType = new GraphQLScalarType({
name: 'Odd',
serialize: oddValue,
parseValue: oddValue,
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return oddValue(parseInt(ast.value, 10));
}
return null;
}
});
function oddValue(value) {
return value % 2 === 1 ? value : null;
}
*/
// Example http://graphql.org/graphql-js/type/#graphqlobjecttype
/*
var AddressType = new GraphQLObjectType({
name: 'Address',
fields: {
street: { type: GraphQLString },
number: { type: GraphQLInt },
formatted: {
type: GraphQLString,
resolve(obj) {
return obj.number + ' ' + obj.street
}
}
}
});
var PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: { type: GraphQLString },
bestFriend: { type: PersonType },
})
});
*/
// Example enum http://graphql.org/graphql-js/type/#graphqlenumtype
// var RGBType = new GraphQLEnumType({
// name: 'RGB',
// values: {
// RED: { value: 0 },
// GREEN: { value: 1 },
// BLUE: { value: 2 }
// }
// });
var types = {
GraphQLEmail: new graphql.GraphQLScalarType({
name: 'Email',
description: 'GraphQLEmail',
serialize(_value) { return _value; },
parseValue(ast) {
var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if (!re.test(ast.value)) {
throw new graphql.GraphQLError('Invalid Email format.', [ast]);
}
return ast.value;
},
parseLiteral(_value) { return _value; }
}),
GraphQLURL: new graphql.GraphQLScalarType({
name: 'URL',
description: 'GraphQLURL',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLDate: new graphql.GraphQLScalarType({
name: 'GraphQLDate',
description: 'Date',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLTime: new graphql.GraphQLScalarType({
name: 'GraphQLTime',
description: 'Time',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLDateTime: new graphql.GraphQLScalarType({
name: 'GraphQLDateTime',
description: 'DateTime',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLString: new graphql.GraphQLScalarType({
name: 'GraphQLString',
description: 'String',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLPassword: new graphql.GraphQLScalarType({
name: 'GraphQLPassword',
description: 'Password',
serialize(_value) { return _value; },
parseValue(_value) { return _value; },
parseLiteral(_value) { return _value; }
}),
GraphQLGeoPoint: new graphql.GraphQLObjectType({
name: 'GeoPoint',
fields: {
lat: { type: new graphql.GraphQLNonNull(graphql.GraphQLFloat) },
lon: { type: new graphql.GraphQLNonNull(graphql.GraphQLFloat) },
alt: { type: graphql.GraphQLFloat, defaultValue: 0 }
}
})
};
module.exports = types;