Skip to content

Commit 4b5687d

Browse files
committed
cleaned up steps 1-4
1 parent a1d6f98 commit 4b5687d

File tree

5 files changed

+55
-56
lines changed

5 files changed

+55
-56
lines changed

README.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For instance, if we wanted to define a student with a name, we would define it a
2222

2323
```javascript
2424
const schema = gql`
25-
type Student {
25+
type Teacher {
2626
name: String!
2727
}
2828
`;
@@ -47,10 +47,10 @@ For instance, in the example below there is a query called me which resolves to
4747
```javascript
4848
const schema = gql`
4949
type Query {
50-
me: Student
50+
me: Teacher
5151
}
5252
53-
type Student {
53+
type Teacher {
5454
name: String!
5555
}
5656
`;
@@ -87,12 +87,11 @@ We can also have querries with arguments. For instance,
8787

8888
```Javascript
8989
const schema = gql`
90-
type Query {hrdkghgikjvdifbnkkeblhjjhekdikdbeh
91-
92-
student(id: ID!): Student
90+
type Query {
91+
teacher(id: ID!): Teacher
9392
}
9493
95-
type Student {
94+
type Teacher {
9695
id: ID!
9796
name: String!
9897
}
@@ -101,10 +100,10 @@ const schema = gql`
101100
const resolvers = {
102101
Query: {
103102
me: () => {
104-
return students[1];
103+
return teachers[1];
105104
},
106-
student: (parents, {id}) => {
107-
return students[id];
105+
teacher: (parents, {id}) => {
106+
return teachers[id];
108107
},
109108
},
110109
};
@@ -123,7 +122,7 @@ Query to be executed
123122

124123
```graphql
125124
{
126-
student(id: "2") {
125+
teacher(id: "2") {
127126
name
128127
}
129128
me {
@@ -139,18 +138,18 @@ By setting the return type as `[Student!]` we are expeting a list of student typ
139138
```Javascript
140139
const schema = gql`
141140
type Query {
142-
students: [Student!]
141+
teachers: [Teacher!]
143142
}
144-
type Student {
143+
type Teacher {
145144
id: ID!
146145
name: String!
147146
}
148147
`;
149148

150149
const resolvers = {
151150
Query: {
152-
students: () => {
153-
return Object.values(students);
151+
teachers: () => {
152+
return Object.values(teachers);
154153
},
155154
}
156155
};
@@ -165,14 +164,14 @@ Query to be executed
165164

166165
```graphql
167166
{
168-
students {
167+
teachers {
169168
id
170169
name
171170
}
172171
}
173172
```
174173

175-
xd
174+
#### Example 4: Using context
176175

177176
In the server definition we can give it context, and use that in the querries. In the query resolver, you can access data from four possible places `(parent, args, context, info) => { ... }`. We can define the context when we set up the server, and then access values from there.
178177

@@ -181,7 +180,7 @@ const server = new ApolloServer({
181180
typeDefs: schema,
182181
resolvers,
183182
context: {
184-
me: students[1],
183+
me: teachers[1],
185184
},
186185
});
187186

@@ -213,31 +212,31 @@ Query to be executed
213212

214213
## Example 5 Type Relationships
215214

216-
We can write a resolver so that every message has an associated Student. We do this by creating a messgae type in which one of the fields is a STudent. The resolver currently goes to the message, looks at the students id, and gets the correspodning student. When you run teh example query, it is now able to map to a student object with all of it's fields.
215+
We can write a resolver so that every teacher has an associated Student. We do this by creating a messgae type in which one of the fields is a STudent. The resolver currently goes to the teacher, looks at the students id, and gets the correspodning student. When you run teh example query, it is now able to map to a student object with all of it's fields.
217216

218217
```Javascript
219218
const schema = gql`
220219
type Query {
221-
me: Student
220+
me: Teacher
222221
}
223-
type Student {
222+
type Teacher {
224223
id: ID!
225224
name: String!
226225
}
227-
type Message {
226+
type Student {
228227
id: ID!
229228
text: String!
230-
student: Student!
229+
teacher: Teacher!
231230
}
232231
`;
233232

234233
const resolvers = {
235-
Message: {
236-
student: message => {
237-
return users[message.studentId];
234+
Student: {
235+
teacher: student => {
236+
return teachers[student.id];
238237
},
239238
},
240-
};
239+
};
241240
```
242241

243242
##### Running Example 5

src/step1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ app.use(cors());
88

99
const schema = gql`
1010
type Query {
11-
me: Student
11+
me: Teacher
1212
}
1313
14-
type Student {
14+
type Teacher {
1515
name: String!
1616
}
1717
`;

src/step2.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const app = express();
77
app.use(cors());
88

99

10-
const students = {
10+
const teachers = {
1111
1: {
1212
id: '1',
1313
name: 'Aria',
@@ -20,11 +20,11 @@ const students = {
2020

2121
const schema = gql`
2222
type Query {
23-
me: Student
24-
student(id: ID!): Student
23+
me: Teacher
24+
teacher(id: ID!): Teacher
2525
}
2626
27-
type Student {
27+
type Teacher {
2828
id: ID!
2929
name: String!
3030
}
@@ -33,10 +33,10 @@ const students = {
3333
const resolvers = {
3434
Query: {
3535
me: () => {
36-
return students[1];
36+
return teachers[1];
3737
},
38-
student: (parents, {id}) => {
39-
return students[id];
38+
teacher: (parents, {id}) => {
39+
return teachers[id];
4040
},
4141
},
4242
};

src/step3.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const app = express();
66

77
app.use(cors());
88

9-
const students = {
9+
const teachers = {
1010
1: {
1111
id: "1",
1212
name: "Aria"
@@ -19,11 +19,11 @@ const students = {
1919

2020
const schema = gql`
2121
type Query {
22-
me: Student
23-
student(id: ID!): Student
24-
students: [Student!]
22+
me: Teacher
23+
teacher(id: ID!): Teacher
24+
teachers: [Teacher!]
2525
}
26-
type Student {
26+
type Teacher {
2727
id: ID!
2828
name: String!
2929
}
@@ -32,13 +32,13 @@ const schema = gql`
3232
const resolvers = {
3333
Query: {
3434
me: () => {
35-
return students[1];
35+
return teachers[1];
3636
},
37-
student: (parents, {id}) => {
38-
return students[id];
37+
teacher: (parents, {id}) => {
38+
return teachers[id];
3939
},
40-
students: () => {
41-
return Object.values(students);
40+
teachers: () => {
41+
return Object.values(teachers);
4242
}
4343
}
4444
};

src/step4.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const app = express();
66

77
app.use(cors());
88

9-
const students = {
9+
const teachers = {
1010
1: {
1111
id: "1",
1212
name: "Aria"
@@ -19,11 +19,11 @@ const students = {
1919

2020
const schema = gql`
2121
type Query {
22-
me: Student
23-
student(id: ID!): Student
24-
students: [Student!]
22+
me: Teacher
23+
teacher(id: ID!): Teacher
24+
teachers: [Teacher!]
2525
}
26-
type Student {
26+
type Teacher {
2727
id: ID!
2828
name: String!
2929
}
@@ -34,11 +34,11 @@ const resolvers = {
3434
me: (parent, args, { me }) => {
3535
return me;
3636
},
37-
student: (parents, {id}) => {
38-
return students[id];
37+
teacher: (parents, {id}) => {
38+
return teachers[id];
3939
},
40-
students: () => {
41-
return Object.values(students);
40+
teachers: () => {
41+
return Object.values(teachers);
4242
}
4343
}
4444
};
@@ -47,7 +47,7 @@ const server = new ApolloServer({
4747
typeDefs: schema,
4848
resolvers,
4949
context: {
50-
me: students[1]
50+
me: teachers[1]
5151
}
5252
});
5353

0 commit comments

Comments
 (0)