Skip to content

Commit b8b1925

Browse files
committed
updated examples 1 and 2
1 parent bf8ede6 commit b8b1925

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Gr
2-
31
# Graphql
42

53
GraphQL is query language, a middle layer that defines the API between the front end and backend. It is a way of querying the data you need in a concise, clean manner.
@@ -45,7 +43,7 @@ Inside of schema, along with teh type definitions
4543

4644
The schema consists of type definitions and a top level query type for reading the data. This defines the type of querries you can make to get data. For every query listed, there is a corresponding resolver that defines how it should be parsed.
4745

48-
#### Example 1 Basic Query
46+
#### Example 1 Basic Query
4947

5048
For instance, in the example below there is a query called me which resolves to a student type, which has to have a name field. In the resolver. it is set to resolve the Student to ```Aria Malkani```.
5149

@@ -71,18 +69,23 @@ const resolvers = {
7169
};
7270
```
7371

74-
To actually query this data, start up the server and run
72+
##### Running Example 1
73+
74+
Running Instructions: ```npm run step1```
75+
Example code: step1.js
76+
77+
Query to be executed
7578
```graphql
7679
{
7780
me {
7881
name
7982
}
8083
}
81-
8284
```
8385

86+
8487
#### Example 2 Query with arguments
85-
We can also have querries with arguments. For isntance,
88+
We can also have querries with arguments. For instance,
8689

8790
```Javascript
8891
let students = {
@@ -120,7 +123,16 @@ const resolvers = {
120123
};
121124
```
122125

123-
To execute the query, you would run
126+
In this case, the resolved for student can parse arguments passed into the query, which allows you to query a specific id. This allows you to query for specific stdents pased on unique identifiers.
127+
128+
In this case, we are also assuming the "me" is the first student in the list, and so we return just that one. As long as what is returned matches the Student type, graphql can compile and execute the query.
129+
130+
##### Running Example 2
131+
132+
Running Instructions: ```npm run step2```
133+
Example code: step2.js
134+
135+
Query to be executed
124136
```graphql
125137
{
126138
student(id: "2") {
@@ -131,6 +143,7 @@ To execute the query, you would run
131143
}
132144
}
133145
```
146+
134147
#### Example 3: Query with multiple values returned
135148
```
136149
const schema = gql`
@@ -146,7 +159,6 @@ const schema = gql`
146159
}
147160
`;
148161
149-
150162
```
151163

152164

step1.js

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

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

1919
const resolvers = {
20-
Query: {
21-
me: () => {
22-
return {
23-
username: "Robin Wieruch"
24-
};
25-
}
26-
}
27-
};
20+
Query: {
21+
me: () => {
22+
return {
23+
name: 'Aria Malkani',
24+
};
25+
},
26+
},
27+
};
2828

2929
const server = new ApolloServer({
3030
typeDefs: schema,

step2.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
const { ApolloServer, gql } = require("apollo-server-express");
4+
5+
const app = express();
6+
7+
app.use(cors());
8+
9+
10+
const students = {
11+
1: {
12+
id: '1',
13+
name: 'Aria',
14+
},
15+
2: {
16+
id: '2',
17+
name: 'Emily',
18+
},
19+
};
20+
21+
const schema = gql`
22+
type Query {
23+
me: Student
24+
student(id: ID!): Student
25+
}
26+
27+
type Student {
28+
id: ID!
29+
name: String!
30+
}
31+
`;
32+
33+
const resolvers = {
34+
Query: {
35+
me: () => {
36+
return students[1];
37+
},
38+
student: (parents, {id}) => {
39+
return students[id];
40+
},
41+
},
42+
};
43+
44+
45+
const server = new ApolloServer({
46+
typeDefs: schema,
47+
resolvers
48+
});
49+
50+
server.applyMiddleware({ app, path: "/graphql" });
51+
52+
app.listen({ port: 8000 }, () => {
53+
console.log("Apollo Server on http://localhost:8000/graphql");
54+
});
55+

0 commit comments

Comments
 (0)