This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
65 lines (57 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
const fetch = require('node-fetch');
const util = require('util');
const parseXML = util.promisify(require('xml2js').parseString);
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLList
} = require('graphql');
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=q00f0EEyXAvZs9O1hMT70w`
).then(response => response.text()).then(parseXML)
}
})
})
});