-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
91 lines (82 loc) · 2.43 KB
/
schema.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
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
GraphQLList
} from 'graphql'
import { sql } from "./server.js"
import DataLoader from "dataloader"
export function getauthor_loader() {
return new DataLoader(batchauthors)
}
const batchauthors = async (ids) => {
const author_result = await sql`select * from authors where authorid = ANY(${ids})`
console.log(author_result)
return author_result
}
const BookType = new GraphQLObjectType({
name: 'bookdata',
description: 'This represents a book object',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLInt) },
title: {
type: GraphQLString,
extensions: { complexity: 5 }
},
authorid: { type: new GraphQLNonNull(GraphQLInt) },
author: {
type: AuthorType,
resolve: (bookdata, args, { authorloader }) => {
return authorloader.load(bookdata.authorid)
}
}
})
})
const AuthorType = new GraphQLObjectType({
name: 'authordata',
description: 'This represents an author object',
fields: () => ({
authorid: { type: new GraphQLNonNull(GraphQLInt) },
name: { type: GraphQLString }
})
})
const RootQueryType = new GraphQLObjectType({
name: 'RootQuery',
description: 'Root Query',
fields: () => ({
listbooks: {
type: new GraphQLList(BookType),
description: "Gets the list of all available books and it's authors",
resolve: async () => {
const bks = await sql`select * from books;`
return bks
}
},
bookbyId: {
type: BookType,
description: "Gets a single boook by id",
args: {
id: { type: GraphQLInt }
},
resolve: async (parent, args) => {
const id = args.id
const bk = await sql`select * from books where id = ${id};`
console.log(bk)
return bk[0]
}
},
listauthors: {
type: new GraphQLList(AuthorType),
description: "Gets the list of all available authors",
resolve: async () => {
const auths = await sql`select * from authors;`
return auths
}
}
})
})
export const schema = new GraphQLSchema({
query: RootQueryType
})