Skip to content

Commit bf8ede6

Browse files
committed
added more to readme + step setup
1 parent 52ab05f commit bf8ede6

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,117 @@ There are three types of interactions: querries, mutations, and subscirptions.
4141

4242
### Querries
4343

44+
Inside of schema, along with teh type definitions
45+
46+
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.
47+
48+
#### Example 1 Basic Query
49+
50+
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```.
51+
52+
```javascript
53+
const schema = gql`
54+
type Query {
55+
me: Student
56+
}
57+
58+
type Student {
59+
name: String!
60+
}
61+
`;
62+
63+
const resolvers = {
64+
Query: {
65+
me: () => {
66+
return {
67+
name: 'Aria Malkani',
68+
};
69+
},
70+
},
71+
};
72+
```
73+
74+
To actually query this data, start up the server and run
75+
```graphql
76+
{
77+
me {
78+
name
79+
}
80+
}
81+
82+
```
83+
84+
#### Example 2 Query with arguments
85+
We can also have querries with arguments. For isntance,
86+
87+
```Javascript
88+
let students = {
89+
1: {
90+
id: '1',
91+
name: 'Aria',
92+
},
93+
2: {
94+
id: '2',
95+
name: 'Emily',
96+
},
97+
};
98+
99+
const schema = gql`
100+
type Query {
101+
me: Student
102+
student(id: ID!): Student
103+
}
104+
105+
type Student {
106+
id: ID!
107+
name: String!
108+
}
109+
`;
110+
111+
const resolvers = {
112+
Query: {
113+
me: () => {
114+
return students[1];
115+
},
116+
student: (parents, {id}) => {
117+
return students[id];
118+
},
119+
},
120+
};
121+
```
122+
123+
To execute the query, you would run
124+
```graphql
125+
{
126+
student(id: "2") {
127+
name
128+
}
129+
me {
130+
name
131+
}
132+
}
133+
```
134+
#### Example 3: Query with multiple values returned
135+
```
136+
const schema = gql`
137+
type Query {
138+
users: [User!]
139+
user(id: ID!): User
140+
me: User
141+
}
142+
143+
type User {
144+
id: ID!
145+
username: String!
146+
}
147+
`;
148+
149+
150+
```
151+
152+
153+
154+
44155

45156

46157

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"scripts": {
3-
"start": "node index.js"
3+
"step1": "node step1.js",
4+
"step2": "node step2.js",
5+
"step3": "node step3.js",
6+
"step4": "node step4.js",
7+
"step5": "node step5.js"
48
},
59
"dependencies": {
610
"apollo-server-express": "^2.2.2",

step1.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
const schema = gql`
10+
type Query {
11+
me: User
12+
}
13+
14+
type User {
15+
username: String!
16+
}
17+
`;
18+
19+
const resolvers = {
20+
Query: {
21+
me: () => {
22+
return {
23+
username: "Robin Wieruch"
24+
};
25+
}
26+
}
27+
};
28+
29+
const server = new ApolloServer({
30+
typeDefs: schema,
31+
resolvers
32+
});
33+
34+
server.applyMiddleware({ app, path: "/graphql" });
35+
36+
app.listen({ port: 8000 }, () => {
37+
console.log("Apollo Server on http://localhost:8000/graphql");
38+
});

0 commit comments

Comments
 (0)