-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
76 lines (67 loc) · 1.78 KB
/
schema.js
File metadata and controls
76 lines (67 loc) · 1.78 KB
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
const { GraphQLInt, GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLList } = require('graphql')
const fetch = require('node-fetch')
// const util = require('util')
// const parseXML = util.promisify(require('xml2js').parseString) // if xml result from API uses callbacks, not promises, so util makes it a promise
/*
fetch(
'http://www.boredapi.com/api/activity/'
).then((res) => res.json()) // if xml result, res.text()
// .then(parseXML)
*/
const FilmType = new GraphQLObjectType({
name: 'Film',
description: '...',
fields: () => ({
title: {
type: GraphQLString,
// How to extract the name from the data?
resolve: json => // first argument is what's returned from the fetch
json.title
},
original_title: {
type: GraphQLString,
resolve: json =>
json.original_title
},
people: {
type: GraphQLList(People),
resolve: json => {
return Promise.all(json.people.map(char =>
fetch(char)
.then(res => res.json())
))
}
}
})
})
const People = new GraphQLObjectType({
name: 'People',
description: '...',
fields: () => ({
name: {
type: GraphQLString,
resolve: json => json.name
},
gender: {
type: GraphQLString,
resolve: json => json.gender
}
})
})
module.exports = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
description: '...',
fields: () => ({
film: {
type: FilmType,
args: {
id: { type: GraphQLString }
},
resolve: (root, args) => fetch( // Fn that graphQL will use to fetch the data
`https://ghibliapi.herokuapp.com/films/${args.id}`
).then((res) => res.json())
},
})
})
})