File tree Expand file tree Collapse file tree 2 files changed +48
-50
lines changed Expand file tree Collapse file tree 2 files changed +48
-50
lines changed Original file line number Diff line number Diff line change @@ -248,10 +248,10 @@ Query to be executed
248
248
249
249
``` graphql
250
250
{
251
- message (id : " 1" ) {
251
+ student (id : " 1" ) {
252
252
id
253
- text
254
- student {
253
+ name
254
+ teacher {
255
255
id
256
256
name
257
257
}
@@ -267,25 +267,25 @@ We can also have each student have a list of associated messages. By adding a li
267
267
``` Javascript
268
268
const schema = gql `
269
269
type Query {
270
- me : Student
270
+ me : Teacher
271
271
}
272
- type Student {
272
+ type Teacher {
273
273
id : ID !
274
274
name : String !
275
- messages : [Message ! ]
275
+ students : [Student ! ]
276
276
}
277
- type Message {
277
+ type Student {
278
278
id : ID !
279
- text : String !
280
- student : Student !
279
+ name : String !
280
+ teacher : Teacher !
281
281
}
282
282
` ;
283
283
284
284
const resolvers = {
285
- Student : {
286
- messages : student => {
287
- return Object .values (messages ).filter (
288
- message => message . studentId === student .id ,
285
+ Teacher : {
286
+ students : teacher => {
287
+ return Object .values (students ).filter (
288
+ student => student . teacherId === teacher .id ,
289
289
);
290
290
},
291
291
},
Original file line number Diff line number Diff line change @@ -6,49 +6,47 @@ const app = express();
6
6
7
7
app . use ( cors ( ) ) ;
8
8
9
- const students = {
9
+ const teachers = {
10
10
1 : {
11
11
id : "1" ,
12
12
name : "Aria" ,
13
- messageIds : [ 1 ]
14
13
} ,
15
14
2 : {
16
15
id : "2" ,
17
16
name : "Emily" ,
18
- messageIds : [ 2 ]
19
17
}
20
18
} ;
21
19
22
- let messages = {
20
+ let students = {
23
21
1 : {
24
- id : "1 " ,
25
- text : "Hello World " ,
26
- studentId : "1"
22
+ id : "3 " ,
23
+ name : "Annika " ,
24
+ teacherId : "1"
27
25
} ,
28
26
2 : {
29
- id : "2 " ,
30
- text : "By World " ,
31
- studentId : "2"
27
+ id : "4 " ,
28
+ name : "Cammie " ,
29
+ teacherId : "2"
32
30
}
33
31
} ;
34
32
35
33
const schema = gql `
36
34
type Query {
37
- me: Student
38
- student (id: ID!): Student
39
- students : [Student !]
40
- messages : [Message !]!
41
- message (id: ID!): Message !
35
+ me: Teacher
36
+ teacher (id: ID!): Teacher
37
+ teachers : [Teacher !]
38
+ students : [Student !]!
39
+ student (id: ID!): Student !
42
40
}
43
- type Student {
41
+ type Teacher {
44
42
id: ID!
45
43
name: String!
46
- messages : [Message !]
44
+ students : [Student !]
47
45
}
48
- type Message {
46
+ type Student {
49
47
id: ID!
50
- text : String!
51
- student: Student !
48
+ name : String!
49
+ teacher: Teacher !
52
50
}
53
51
` ;
54
52
@@ -57,38 +55,38 @@ const resolvers = {
57
55
me : ( parent , args , { me } ) => {
58
56
return me ;
59
57
} ,
60
- student : ( parents , { id } ) => {
61
- return students [ id ] ;
58
+ teacher : ( parents , { id } ) => {
59
+ return teachers [ id ] ;
60
+ } ,
61
+ teachers : ( ) => {
62
+ return Object . values ( teachers ) ;
62
63
} ,
63
64
students : ( ) => {
64
65
return Object . values ( students ) ;
65
66
} ,
66
- messages : ( ) => {
67
- return Object . values ( messages ) ;
68
- } ,
69
- message : ( parent , { id } ) => {
70
- return messages [ id ] ;
67
+ student : ( parent , { id } ) => {
68
+ return students [ id ] ;
71
69
}
72
70
} ,
73
- Message : {
74
- student : message => {
75
- return students [ message . studentId ] ;
71
+ Student : {
72
+ teacher : student => {
73
+ return teachers [ student . teacherId ] ;
76
74
}
77
75
} ,
78
- Student : {
79
- messages : student => {
80
- return Object . values ( messages ) . filter (
81
- message => message . studentId === student . id
76
+ Teacher : {
77
+ students : teacher => {
78
+ return Object . values ( students ) . filter (
79
+ student => student . teacherId === teacher . id ,
82
80
) ;
83
- }
84
- }
81
+ } ,
82
+ } ,
85
83
} ;
86
84
87
85
const server = new ApolloServer ( {
88
86
typeDefs : schema ,
89
87
resolvers,
90
88
context : {
91
- me : students [ 1 ]
89
+ me : teachers [ 1 ]
92
90
}
93
91
} ) ;
94
92
You can’t perform that action at this time.
0 commit comments