forked from prismake/typegql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.ts
56 lines (48 loc) · 972 Bytes
/
schema.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
49
50
51
52
53
54
55
56
import {
SchemaRoot,
Query,
Mutation,
Field,
ObjectType,
compileSchema
} from 'decapi'
@ObjectType()
class BookQuery {
@Field()
id: number
@Field()
name: string
constructor({ id, name }) {
this.id = id
this.name = name
}
}
@ObjectType()
class BookMutation extends BookQuery {
@Field()
edit(name: string): BookQuery {
this.name = name
return this
}
@Field()
remove(): string {
return `Book with id ${this.id} removed.`
}
}
const booksDb: BookMutation[] = [
new BookMutation({ id: 1, name: 'Lord of the Rings' }),
new BookMutation({ id: 2, name: 'Harry Potter' })
]
@SchemaRoot()
class MySchema {
@Mutation()
book(bookId: number): BookMutation | undefined {
return booksDb.find(({ id }) => id === bookId)
}
@Query({ type: [BookQuery] })
books(): BookQuery[] {
// just a utility to cast our POJOs into a class of Book
return booksDb
}
}
export const schema = compileSchema(MySchema)