-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathunion.ts
48 lines (41 loc) · 1.14 KB
/
union.ts
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
import { g, InferResolvers, Infer, buildSchema } from './../src/index'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'
const x = g.type('X', {
a: g.string(),
})
const y = g.type('Y', {
b: g.string(),
})
const union = g.unionType('Union', { x, y })
type Union = Infer<typeof union>
const queryType = g.type('Query', {
greet: g.ref(() => union)
.args({
name: g.string().optional().default('Max'),
})
.description('Greets a person')
})
type Query = Infer<typeof queryType>
const resolvers: InferResolvers<{ Query: typeof queryType, Union: typeof union }, {}> = {
Query: {
greet: (parent, args, context, info) => {
if (args.name === 'Max') {
return { __typename: 'X', a: 'Hello Max' }
} else {
return { __typename: 'Y', b: 'Hello World' }
}
}
},
Union: {
__resolveType: (parent, context, info) => {
return parent.__typename
}
}
}
const schema = buildSchema({ g, resolvers })
const yoga = createYoga({ schema })
const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})