-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.js
80 lines (75 loc) · 1.52 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
const {
GraphQLObjectType,
GraphQLSchema,
GraphQLInt,
GraphQLString,
GraphQLList
} = require('graphql');
const { getXML } = require('./helpers')
const getID = getXML('id')
const BookType = new GraphQLObjectType({
name: 'Book',
description: '...',
fields: () => ({
id: {
type: GraphQLInt,
resolve: getID
},
title: {
type: GraphQLString,
resolve: (book) => {
const title = book.title[0]
return title
}
},
isbn: {
type: GraphQLString,
resolve: getXML('isbn')
},
authors: {
type: new GraphQLList(AuthorType),
resolve: (book, args, ctx) =>
ctx.authorLoader.loadMany(
book.authors[0].author
.map(getXML('id'))
)
}
})
})
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: '...',
fields: () => ({
id: {
type: GraphQLInt,
resolve: getID
},
name: {
type: GraphQLString,
resolve: getXML('name')
},
books: {
type: new GraphQLList(BookType),
resolve: (author, args, ctx) =>
ctx.bookLoader.loadMany(
author.books[0].book
.map(book => book.id[0]._)
)
}
})
})
module.exports = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
description: '...',
fields: () => ({
author: {
type: AuthorType,
args: {
id: { type: GraphQLInt }
},
resolve: (root, args, ctx) => ctx.authorLoader.load(args.id)
}
})
})
})