-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
458 lines (431 loc) · 10.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
* Convert tcomb types to secure, executable GraphQL schemas (WIP)
*
* @author Denny Shimkoski
*/
import t from 'tcomb'
import {
GraphQLBoolean,
GraphQLEnumType,
GraphQLFloat,
GraphQLID,
GraphQLInputObjectType,
GraphQLInt,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLSchema,
GraphQLString,
GraphQLUnionType,
graphql,
printSchema
} from 'graphql'
import { GraphQLDate } from 'graphql-iso-date'
import { addResolveFunctionsToSchema } from 'graphql-tools'
import { applyMiddleware } from 'graphql-middleware'
import { shield } from 'graphql-shield'
import deepExtend from 'deep-extend'
/**
* Extensible type map
*
* Maps tcomb types to GraphQL types
*
* Applications should register custom scalars here
*/
export const typeMap = {
Boolean: GraphQLBoolean,
Date: GraphQLDate,
ID: GraphQLID,
Integer: GraphQLInt,
Number: GraphQLFloat,
String: GraphQLString,
enums,
interface: inter,
list,
maybe,
struct,
union
}
/**
* GraphQL ID type for tcomb
*
* @param {number|string} ID
*
* @return {Object} tcomb type
*/
t.ID = t.union([t.String, t.Integer], 'ID')
/**
* Transform a tcomb type into the equivalent GraphQL type
*
* @param {Object} type - tcomb type
* @param {boolean} optional
*
* @return {Object} GraphQL type
*/
export function transform (type, optional) {
let x = typeMap[type.meta.name] || typeMap[type.meta.kind]
x = typeof(x) === 'function' ? x(type) : x
if (optional) {
return x
} else {
return x.maybe ? x.type : new GraphQLNonNull(x)
}
}
/**
* Transform enum
*
* @param {Object} type - tcomb enum
*
* @return {Object} GraphQL type
*/
function enums(type) {
return type.gql || (type.gql = new GraphQLEnumType({
name: t.getTypeName(type),
values: Object.entries(type.meta.map).reduce((values, [k, v]) => {
values[k] = { value: v }
return values
}, {})
}))
}
/**
* Transform list
*
* @param {Object} type - tcomb list
*
* @return {Object} GraphQL type
*/
function list(type) {
return new GraphQLList(transform(type.meta.type))
}
/**
* Transform maybe
*
* Wraps type temporarily to notify transform function of maybe
*
* @param {Object} type - tcomb type
*
* @return {Object} wrapped GraphQL type
*/
function maybe(type) {
return {maybe: true, type: transform(type.meta.type, true)}
}
/**
* Transform interface
*
* Application must implement resolveType
*
* @param {Object} type - tcomb interface
*
* @return {Object} GraphQL interface
*/
function inter(type) {
return object(type, GraphQLInterfaceType)
}
/**
* Transform struct
*
* @param {Object} type - tcomb struct
*
* @return {Object} GraphQL type
*/
function struct(type) {
return object(type, type.__input ? GraphQLInputObjectType : GraphQLObjectType)
}
/**
* Transform union
*
* @param {Object} type - tcomb union
*
* @return {Object} GraphQL type
*/
function union(type) {
return new GraphQLUnionType({
name: t.getTypeName(type),
description: type.__typeDesc || type.__desc,
types: type.meta.types.map(t => transform(t, true)),
resolveType: data => t.getTypeName(type.dispatch(data))
})
}
/**
* Transform tcomb type into given GraphQLType
*
* @param {Object} type - tcomb type
*
* @return {Object} GraphQL type
*/
function object(type, GraphQLType) {
if (type.gql) {
return type.gql
}
type.gql = new GraphQLType({
name: t.getTypeName(type),
description: type.__typeDesc || type.__desc,
interfaces: type.interfaces && type.interfaces.map(i => transform(i, true)),
fields: Object.entries(type.meta.props).reduce((fields, [prop, propType]) => {
fields[prop] = {
type: transform(propType),
defaultValue: propType.__val,
description: propType.__desc,
args: propType.__args && Object.entries(propType.__args).reduce((args, [k, v]) => {
const arg = { type: transform(v) }
arg.defaultValue = v.__val
arg.description = v.__desc
args[k] = arg
return args
}, {})
}
return fields
}, {})
})
// mangle: { reserved: ['GraphQLObjectType', 'GraphQLUnionType', 'GraphQLInterfaceType'] }
// TODO: revisit https://github.com/acarl005/join-monster/issues/352
type.gql._typeConfig = {}
return type.gql
}
/**
* Create proxy for tcomb type
*
* Proxies share the underlying type while retaining their own identity,
* so they can be used to annotate a single type in different contexts
*
* @param {Object} type - tcomb type
* @param {Object} props - additional properties
*
* @return {Object} proxied type
*/
function proxy (Type, props) {
const keys = Object.keys(props)
const p = new Proxy(Type, {
get(obj, key) {
if (keys.indexOf(key) > -1) {
return this[key]
}
return obj[key]
},
set(obj, key, val) {
if (keys.indexOf(key) > -1) {
this[key] = val
return true
}
obj[key] = val
return true
}
})
return Object.assign(p, props)
}
/**
* Create proxied tcomb type representing a GraphQL type
*
* @param {Object} name - type name
* @param {Object} desc - type description
* @param {Object} props - type definition
*
* @return {Object} proxied tcomb type
*
* @example
*
* const Thing = type('Thing', 'A Thing', { example: t.String })
* const ThingList = type('ThingList', 'A list of Things', { things: t.list(t.maybe(Thing)) })
*/
export function type(name, __typeDesc, props, ...interfaces) {
return proxy(t.struct(props, name), {__typeDesc, interfaces})
}
/**
* Create proxied tcomb type representing a GraphQL input
*
* @param {Object} name - type name
* @param {Object} desc - type description
* @param {Object} props - type definition
*
* @return {Object} proxied tcomb type
*
* @example
*
* const Input = type('ExampleInput', 'An integer input', { input: t.Integer })
*/
export function input(name, __typeDesc, props) {
return proxy(t.struct(props, name), {__typeDesc, __input: true})
}
/**
* Create proxied tcomb type representing a parameterized GraphQL type
*
* @param {Object} args - map of argument types
* @param {Object} OutputType - return type
* @param {Object} PubType - optional pubsub type
* @param {Object} desc - optional type description
*
* @return {Object} proxied tcomb type
*
* @example
*
* const SendMessageInput = input('SendMessageInput', 'sendMessage input', { text: t.String })
* const SendMessageOutput = type('SendMessageOutput', 'sendMessage response', { sent: t.Boolean })
* const messageSent = fn({ toId: t.ID }, Message)
* const sendMessage = fn({ input: SendMessageInput }, SendMessageOutput, messageSent, 'Send a message')
*/
export function fn(args, OutputType, __publishType, __desc) {
return proxy(OutputType, {
__args: args.meta && args.meta.props || args,
__publishType,
__desc})
}
/**
* Create proxied tcomb type representing an argument for parameterized GraphQL types
*
* @param {Object} Type - tcomb type
* @param {Object} val - optional default value
* @param {Object} desc - optional type description
*
* @return {Object} proxied tcomb type
*
* @example
*
* const mutateX = fn({
* y: arg(t.Integer, 10, 'Set X value (default 10)')
* }, MutateXOutput)
*/
export function arg(Type, __val, __desc) {
return proxy(Type, __val === null ? {__desc} : {__val, __desc})
}
/**
* Schema builder
*/
export default class TCombGraphQLSchema {
constructor() {
this.mutations = null
this.queries = null
this.resolvers = {}
this.subscriptions = null
this.shield = null
}
/**
* Add mutations
*
* @param {Object} mutations
*
* @example
*
* schema.addMutations({ sendMessage })
*/
addMutations(mutations) {
if (!this.mutations) {
this.mutations = {}
}
for (const [k, type] of Object.entries(mutations)) {
this.mutations[k] = type
if (type.__publishType) {
if (!this.subscriptions) {
this.subscriptions = {}
}
this.subscriptions[k] = type.__publishType
}
}
}
/**
* Add queries
*
* @param {Object} queries
*
* @example
*
* schema.addQueries({
* message: fn({ id: t.ID }, t.maybe(Message), null, 'Fetch a message by ID')
* })
*/
addQueries(queries) {
if (!this.queries) {
this.queries = {}
}
for (const [k, type] of Object.entries(queries)) {
this.queries[k] = type
}
}
/**
* Add resolvers
*
* @param {Object} resolvers
*
* @example
*
* schema.addResolvers({
* Query: { message: (parent, { _id }) => ... }
* })
*/
addResolvers(resolvers) {
this.resolvers = deepExtend(this.resolvers, resolvers)
}
/**
* Add permissions (graphql-shield rules)
*
* @param {Object} permissions
*
* @example
*
* schema.addPermissions({
* Query: { message: isAuthenticated }
* })
*/
addPermissions(permissions) {
this.shield = this.shield ? deepExtend(this.shield, permissions) : permissions
}
/**
* Create query function
*
* @param {function} adapt - modify schema
*
* @return {function} function that executes a given GraphQL query
*
* @example
*
* const exec = schema.compile(joinMonsterAdapter)
* exec(exampleQuery).then(...)
*/
compile(adapt) {
const schema = this.toGraphQL(adapt)
return (...args) => graphql(schema, ...args)
}
/**
* Print GraphQL schema
*
* @return {string} text representation of GraphQL schema
*/
print() {
return printSchema(this.toGraphQL())
}
/**
* Create executable GraphQL schema
*
* @param {function} adapt - modify schema
*
* @return {Object} GraphQL schema
*
* @example
*
* schema.toGraphQL(joinMonsterAdapter)
*/
toGraphQL(adapt) {
const config = {}
if (this.queries) {
config.query = transform(t.struct(this.queries, 'Query'), true)
}
if (this.mutations) {
config.mutation = transform(t.struct(this.mutations, 'Mutation'), true)
}
if (this.subscriptions) {
config.subscription = transform(t.struct(this.subscriptions, 'Subscription'), true)
}
const schema = new GraphQLSchema(config)
addResolveFunctionsToSchema({ schema, resolvers: this.resolvers })
if (this.shield) {
applyMiddleware(schema, shield(this.shield, {
fallback: 'Permission denied'
}))
}
if (adapt) {
adapt(schema)
}
return schema
}
}