-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema.js
79 lines (71 loc) · 1.78 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
const SpellType = require('./types/spellType');
const ClassType = require('./types/classType');
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLList,
} = require('graphql');
const axios = require('axios');
const BASE_URL = 'http://dnd5eapi.co/api'
async function getSpellByIndex(index) {
const response = await axios.get(`${BASE_URL}/spells/${index}`);
return response.data;
}
async function getClassByIndex(index) {
const response = await axios.get(`${BASE_URL}/classes/${index}`);
return response.data;
}
const QueryType = new GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: () => ({
spell: {
type: SpellType,
args: {
index: {
type: GraphQLInt
}
},
resolve: (root, args) => {
return getSpellByIndex(args.index);
}
},
spells: {
type: new GraphQLList(SpellType),
resolve: async () => {
const { data: { results } } = await axios.get(`${BASE_URL}/spells`)
const dataToSend = results.map(async (spell) => {
const { data } = await axios.get(spell.url);
return data;
})
return dataToSend;
}
},
class: {
type: ClassType,
args: {
index: {
type: GraphQLInt
}
},
resolve: (root, args) => {
return getClassByIndex(args.index);
}
},
classes: {
type: new GraphQLList(ClassType),
resolve: async () => {
const { data: { results } } = await axios.get(`${BASE_URL}/classes`)
const dataToSend = results.map(async (item) => {
const { data } = await axios.get(item.url);
return data;
})
return dataToSend;
}
}
})
})
module.exports = new GraphQLSchema({
query: QueryType,
})