1+ const { GraphQLObjectType, GraphQLID, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLBoolean, GraphQLList} = require ( 'graphql' ) ;
2+ var _ = require ( 'lodash' ) ;
3+
4+ // dummy data
5+ var userData = [
6+ { id : '1' , name : 'Bond' , age : 36 , profession : 'Programmer' , } ,
7+ { id : '13' , name : 'Anna' , age : 26 , profession : 'Baker' , } ,
8+ { id : '211' , name : 'Bella' , age : 16 , profession : 'Mechanic' , } ,
9+ { id : '19' , name : 'Gina' , age : 26 , profession : 'Painter' , } ,
10+ { id : '150' , name : 'Georgina' , age : 36 , profession : 'Teacher' } ,
11+ ] ;
12+
13+ var hobbyData = [
14+ { id : '1' , title : 'Programming' , description : 'Using computers to make the world a better place' , userId : '150' , } ,
15+ { id : '2' , title : 'Rowing' , description : 'Sweat and feel better before eating donoughts' , userId : '19' , } ,
16+ { id : '3' , title : 'Swimming' , description : 'Get in the ware and learn to become the water' , userId : '1' , } ,
17+ { id : '4' , title : 'Fencing' , description : 'A hobby for fency people' , userId : '150' , } ,
18+ { id : '5' , title : 'Programming' , description : 'Wear hiking boots and explore the world' , userId : '1' , } ,
19+ ] ;
20+
21+ var postData = [
22+ { id : '1' , comment : 'Building a Mind' , userId : '1' } ,
23+ { id : '2' , comment : 'GraphQL is Amazing' , userId : '1' } ,
24+ { id : '3' , comment : 'How to Change the World' , userId : '19' } ,
25+ { id : '4' , comment : 'How to Change the World' , userId : '211' } ,
26+ { id : '5' , comment : 'How to Change the World' , userId : '1' } ,
27+ ] ;
28+
29+ // create types
30+ const UserType = new GraphQLObjectType ( {
31+ name : 'User' ,
32+ description : 'Documentation for user...' ,
33+ fields : ( ) => ( {
34+ id : { type : GraphQLString } ,
35+ name : { type : GraphQLString } ,
36+ age : { type : GraphQLInt } ,
37+ profession : { type : GraphQLString } ,
38+
39+ posts :{
40+ type : new GraphQLList ( PostType ) ,
41+ resolve ( parent , args ) {
42+ return _ . filter ( postData , { userId : parent . id } ) ;
43+ }
44+ } ,
45+
46+ hobbies :{
47+ type : new GraphQLList ( HobbyType ) ,
48+ resolve ( parent , args ) {
49+ return _ . filter ( hobbyData , { userId : parent . id } ) ;
50+ }
51+ }
52+ } )
53+ } ) ;
54+
55+ const HobbyType = new GraphQLObjectType ( {
56+ name : 'Hobby' ,
57+ description : 'Hobby Description' ,
58+ fields : ( ) => ( {
59+ id : { type : GraphQLID } ,
60+ title : { type : GraphQLString } ,
61+ description : { type : GraphQLString } ,
62+ user : {
63+ type : UserType ,
64+ resolve ( parent , args ) {
65+ return _ . find ( userData , { id : parent . userId } ) ;
66+ }
67+ } ,
68+ } )
69+ } ) ;
70+
71+ const PostType = new GraphQLObjectType ( {
72+ name : 'Post' ,
73+ description : 'Post description' ,
74+ fields : ( ) => ( {
75+ id : { type : GraphQLID } ,
76+ comment : { type : GraphQLString } ,
77+ user : {
78+ type : UserType ,
79+ resolve ( parent , args ) {
80+ return _ . find ( userData , { id : parent . userId } ) ;
81+ }
82+ } ,
83+ } )
84+ } ) ;
85+
86+ // RootQuery
87+ const RootQuery = new GraphQLObjectType ( {
88+ name : 'RootQueryType' ,
89+ description : 'Description' ,
90+ fields : {
91+ user :{
92+ type : UserType ,
93+ args : { id : { type : GraphQLString } } ,
94+
95+ resolve ( parent , args ) {
96+
97+ return _ . find ( userData , { id : args . id } ) ;
98+
99+ }
100+ } ,
101+
102+ hobby :{
103+ type : HobbyType ,
104+ args : { id : { type : GraphQLID } } ,
105+
106+ resolve ( parent , args ) {
107+
108+ return _ . find ( hobbyData , { id : args . id } ) ;
109+
110+ }
111+ } ,
112+
113+ post :{
114+ type : PostType ,
115+ args : { id : { type : GraphQLID } } ,
116+
117+ resolve ( parent , args ) {
118+
119+ return _ . find ( postData , { id : args . id } ) ;
120+
121+ }
122+ } ,
123+
124+ }
125+ } ) ;
126+
127+ const schema = new GraphQLSchema ( {
128+ query : RootQuery
129+ } )
130+
131+ module . exports = schema ;
0 commit comments