-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
101 lines (85 loc) · 2.19 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const graphql=require('graphql')
const{GraphQLObjectType ,
GraphQLString ,
GraphQLSchema,
GraphQLInt,
GraphQLList
} = graphql;
const _ = require('lodash')
const books = [
{name: 'Name of the Wind', genre: 'Fantasy', id: '1', authorId: '1'},
{name: 'The Final Empire', genre: 'Fantasy', id: '2', authorId: '2'},
{name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorId: '3'},
{name: 'The Hero of Ages', genre: 'Fantasy', id: '4', authorId: '2'},
{name: 'The Colour of Magic', genre: 'Fantasy', id: '5', authorId: '3'},
{name: 'The Light Fantastic', genre: 'Fantasy', id: '6', authorId: '3'}
]
;
const authors = [
{name: 'Patrick Rothfuss', age: 44, id:"1"},
{name: 'Brandon Sanderson', age: 42, id:"2"},
{name: 'Terry Pratchett', age: 66, id:"3"},
]
;
const BookType = new GraphQLObjectType(
{
name: 'Book',
fields: () => ({
id : {type:GraphQLString},
name : {type:GraphQLString},
genre : {type:GraphQLString},
author: {
type: AuthorType,
resolve(parent,args){
console.log("parent");
return _.find(authors,{id:parent.authorid})
}
}
})
}
);
const AuthorType = new GraphQLObjectType(
{
name: 'Author',
fields: () => ({
id : {type:GraphQLString},
name : {type:GraphQLString},
age : {type:GraphQLInt},
books: {
type: new GraphQLList(BookType),
resolve(parent,args)
{
return _.filter (books , {authorId : parent.id})
}
}
})
}
);
const RootQuery = new GraphQLObjectType(
{
name:'RootQueryType',
fields:{
book:{
type: BookType,
args: {id:{type:GraphQLString}},
resolve (parents , args)
{
console.log(typeof(args.id));
return _.find (books,{id: args.id}) ;
}
},
author :{
type: AuthorType,
args: {id:{type:GraphQLString}},
resolve (parents , args)
{
console.log(typeof(args.id));
return _.find (authors,{id: args.id}) ;
}
}
}
}
);
module.exports = new GraphQLSchema({
query: RootQuery
})