-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
64 lines (60 loc) · 1.38 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
const fetch = require('node-fetch');
const util = require('util');
const {
GraphQLSchema,
GraphQLInt,
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = require('graphql');
const parseXml = util.promisify(require('xml2js').parseString);
const BookType = new GraphQLObjectType({
name: 'Book',
description: '...',
fields: () => ({
title: {
type: GraphQLString,
resolve: xml => xml.title[0],
},
isbn: {
type: GraphQLString,
resolve: xml => xml.isbn[0] || '',
},
}),
});
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: '....',
fields: () => ({
name: {
type: GraphQLString,
resolve: xml => xml.GoodreadsResponse.author[0].name[0],
},
books: {
type: new GraphQLList(BookType),
resolve: xml => xml.GoodreadsResponse.author[0].books[0].book,
},
}),
});
module.exports = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
description: '.....',
fields: () => ({
author: {
type: AuthorType,
args: {
id: { type: GraphQLInt },
},
resolve: (root, args) =>
fetch(
`https://www.goodreads.com/author/show.xml?id=${
args.id
}&key=XRSYrxC2hjEWPVi1nHlaQ`,
)
.then(response => response.text())
.then(parseXml),
},
}),
}),
});