-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.js
49 lines (41 loc) · 1.55 KB
/
basic.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
import t from 'tcomb'
import TCombGraphQLSchema, { arg, fn, type, input } from '..'
const schema = new TCombGraphQLSchema()
const Textable = t.interface({ text: t.String }, 'Textable')
const Message = type('Message', null, { text: t.String }, Textable)
const message = fn({ id: t.ID }, t.maybe(Message), null, 'Fetch a message by ID')
const SendMessageInput = input('SendMessageInput', 'sendMessage input', { text: t.String })
const SendMessageOutput = type('SendMessageOutput', 'sendMessage response', { sent: t.Boolean })
const messageSent = fn({ toId: arg(t.maybe(t.ID), null, 'optionally limit to particular User ID') }, Message)
const sendMessage = fn({ input: SendMessageInput }, SendMessageOutput, messageSent, 'Send a message')
schema.addMutations({ sendMessage })
schema.addQueries({ message })
schema.addResolvers({
Textable: {
__resolveType(data, ctx, info) {}
}
})
const Beep = type('Beep', 'A beep noise', { beepiness: t.Integer })
const Boop = type('Boop', 'A boop noise', { boopiness: t.Integer })
const BotNoise = t.union([Beep, Boop], 'BotNoise')
BotNoise.dispatch = x => x.boopiness ? Boop : Beep
const botNoises = fn({}, t.list(t.maybe(BotNoise)), null, 'Fetch a list of bot noises')
schema.addQueries({ botNoises })
schema.addResolvers({
Query: {
botNoises: () => ([{ boopiness: 20 }])
}
})
console.log(schema.print())
const query = `{
message(id: 1) {
text
}
botNoises {
... on Beep { beepiness }
... on Boop { boopiness }
}
}
`
const exec = schema.compile()
exec(query).then(results => console.log(JSON.stringify(results)))