Skip to content

Commit 9a042d4

Browse files
committed
simple re-write of getting started
1 parent 909287e commit 9a042d4

File tree

1 file changed

+100
-45
lines changed

1 file changed

+100
-45
lines changed

site/docs/QuickStart-GettingStarted.md

Lines changed: 100 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,45 @@ permalink: /docs/getting-started/
66
next: /docs/videos/
77
---
88

9-
Let's build a basic GraphQL server from scratch. We'll be using the **[graphql-js](https://github.com/graphql/graphql-js)** reference implementation of GraphQL for this example.
9+
Let's build a basic GraphQL server from scratch using **[graphql-js](https://github.com/graphql/graphql-js)**.
1010

11-
Our server will be simple; it will have one type, a `User`, where a user has two fields; an `id` and a `name`. For an example of a more complex server and additional features,
12-
check out the **[walkthrough](../intro)** section of the docs.
11+
Our server's schema will be simple: it will have one type, a `User`, with two fields, `id` and `name`.
12+
(For an example of a more complex server, check out the **[Walkthrough](../intro)**.)
1313

1414
## Setup
1515

16-
We'll start by making a folder for our demo server.
16+
Start by making a folder for your demo server:
1717

1818
```sh
1919
mkdir graphql-demo
2020
cd graphql-demo
2121
```
2222

23-
Now, we'll install the three packages that we need:
23+
The example server requires **[Node.js](https://nodejs.org/en/)**
24+
and three additional packages for our server:
2425

25-
1. **[graphql](https://github.com/graphql/graphql-js)**, the reference implementation of GraphQL in JS.
26+
1. **[graphql](https://github.com/graphql/graphql-js)**, the reference implementation of GraphQL in JavaScript.
2627
2. **[express](https://github.com/strongloop/express)**, a basic web framework.
27-
3. **[express-graphql](https://github.com/graphql/express-graphql)**, middleware for express to make it easy to expose a GraphQL server.
28+
3. **[express-graphql](https://github.com/graphql/express-graphql)**, an express middleware that exposes a GraphQL server.
2829

29-
We install these three packages by running:
30+
Install these three packages using **[npm](https://docs.npmjs.com/getting-started/installing-node)**:
3031

3132
```sh
3233
npm init -f
33-
npm install graphql --save
34-
npm install express --save
35-
npm install express-graphql --save
34+
npm install graphql express express-graphql --save
3635
```
3736

38-
## Server
37+
## Data
38+
39+
Our server will consist of two files, `data.json` and `index.js`.
40+
To create these files, run
41+
42+
```sh
43+
touch data.json
44+
touch index.js
45+
```
3946

40-
Now that we have our packages installed, let's quickly define our data set of users, in `data.json`:
47+
Now define the user data in `data.json`:
4148

4249
```json
4350
{
@@ -47,27 +54,34 @@ Now that we have our packages installed, let's quickly define our data set of us
4754
},
4855
"2": {
4956
"id": "2",
50-
"name": "Lee"
57+
"name": "Marie"
5158
},
5259
"3": {
5360
"id": "3",
54-
"name": "Nick"
61+
"name": "Jessie"
5562
}
5663
}
5764
```
5865

59-
We'll create a very basic GraphQL schema to describe that data set in
60-
`index.js`, then allow that GraphQL Schema to be queried over HTTP.
66+
## Server
67+
68+
Next you'll create a very basic GraphQL schema to describe the data;
69+
you can then allow this schema to be queried over HTTP.
70+
71+
Insert the following into `index.js` (and be sure to read the comments!):
6172

6273
```js
74+
// Import the required libraries
6375
var graphql = require('graphql');
6476
var graphqlHTTP = require('express-graphql');
6577
var express = require('express');
6678

67-
// Import our data set from above
79+
// Import the data you created above
6880
var data = require('./data.json');
6981

70-
// Define our user type, with two string fields; `id` and `name`
82+
// Define the User type with two string fields: `id` and `name`.
83+
// The type of User is GraphQLObjectType, which has child fields
84+
// with their own types (in this case, GraphQLString).
7185
var userType = new graphql.GraphQLObjectType({
7286
name: 'User',
7387
fields: {
@@ -76,17 +90,24 @@ var userType = new graphql.GraphQLObjectType({
7690
}
7791
});
7892

79-
// Define our schema, with one top level field, named `user`, that
93+
// Define the schema with one top-level field, `user`, that
8094
// takes an `id` argument and returns the User with that ID.
95+
// Note that the `query` is a GraphQLObjectType, just like User.
96+
// The `user` field, however, is a userType, which we defined above.
8197
var schema = new graphql.GraphQLSchema({
8298
query: new graphql.GraphQLObjectType({
8399
name: 'Query',
84100
fields: {
85101
user: {
86102
type: userType,
103+
// `args` describes the arguments that the `user` query accepts
87104
args: {
88105
id: { type: graphql.GraphQLString }
89106
},
107+
// The resolve function describes how to "resolve" or fulfill
108+
// the incoming query.
109+
// In this case we use the `id` argument from above as a key
110+
// to get the User from `data`
90111
resolve: function (_, args) {
91112
return data[args.id];
92113
}
@@ -95,32 +116,31 @@ var schema = new graphql.GraphQLSchema({
95116
})
96117
});
97118

98-
console.log('Server online!');
99119
express()
100120
.use('/graphql', graphqlHTTP({ schema: schema, pretty: true }))
101121
.listen(3000);
122+
123+
console.log('GraphQL server running on http://localhost:3000/graphql');
102124
```
103125

104126
<script data-inline>
105127
var graphql = require('graphql');
106128

107-
// Import our data set from above
108129
var data = {
109130
"1": {
110131
"id": "1",
111132
"name": "Dan"
112133
},
113134
"2": {
114135
"id": "2",
115-
"name": "Lee"
136+
"name": "Marie"
116137
},
117138
"3": {
118139
"id": "3",
119-
"name": "Nick"
140+
"name": "Jessie"
120141
}
121142
};
122143

123-
// Define our user type, with two string fields; `id` and `name`
124144
var userType = new graphql.GraphQLObjectType({
125145
name: 'User',
126146
fields: {
@@ -129,8 +149,6 @@ var userType = new graphql.GraphQLObjectType({
129149
}
130150
});
131151

132-
// Define our schema, with one top level field, named `user`, that
133-
// takes an `id` argument and returns the User with that ID.
134152
var schema = new graphql.GraphQLSchema({
135153
query: new graphql.GraphQLObjectType({
136154
name: 'Query',
@@ -151,20 +169,34 @@ var schema = new graphql.GraphQLSchema({
151169
global.schema = schema;
152170
</script>
153171

154-
That's it! Our basic GraphQL server is done!
155-
156-
## Testing
157-
158-
Let's test our GraphQL server. We start by running
172+
That's it - your basic GraphQL server is done! Start it by running
159173

160174
```sh
161175
node index.js
162176
```
163177

164-
The server should announce that it is online. We can verify that by hitting
165-
`http://localhost:3000/graphql?query={user(id:%221%22){name}}`, which will
166-
execute this GraphQL query which asks to fetch the user with ID "1", and return
167-
that user's name.
178+
The server should announce that it is running at
179+
[localhost:3000/graphql](http://localhost:3000/graphql).
180+
If you navigate to this address you will receive this notice:
181+
182+
```javascript
183+
{
184+
"errors": [
185+
{
186+
"message": "Must provide query string."
187+
}
188+
]
189+
}
190+
```
191+
192+
We know our server is running - now we just need to send it a query!
193+
194+
## Queries
195+
196+
Below is a very simple query you can make against your schema. To the right is
197+
the result your server should deliver. Take a moment to read the query and the
198+
result. Note that the result and query have the same basic "shape": whereas the
199+
result is JSON key-value pairs, the query is the just the keys.
168200

169201
<script data-inline>
170202
import MiniGraphiQL from '../_core/MiniGraphiQL';
@@ -177,17 +209,40 @@ that user's name.
177209
`} />);
178210
</script>
179211

180-
To fetch a different user, we can run `http://localhost:3000/graphql?query={user(id:%222%22){name}}`
181-
to execute this GraphQL query with a changed the ID, so we'll get the name of
182-
that user instead:
212+
You can edit the above query; the result will automatically update when you do.
213+
If you make a syntax mistake it will be underlined in red. Try replacing
214+
`id: "1"` with `id: "2"`; replace `name` with `id` or with `name id`.
183215

184-
<script data-inline>
185-
import MiniGraphiQL from '../_core/MiniGraphiQL';
186-
renderHere(<MiniGraphiQL schema={global.schema} query={ `
216+
Now that you know what a GraphQL query looks like you can query your own server.
217+
Let's start with the simple query
218+
219+
```
187220
{
188-
user(id: "2") {
221+
user(id: "1") {
189222
name
190223
}
191224
}
192-
`} />);
193-
</script>
225+
```
226+
227+
Remove all the whitespace in the query: `{user(id:"1"){name}}` (whitespace is optional in GraphQL).
228+
You can send this to your server via a GET request with a URL query string:
229+
**http://localhost:3000/graphql?query={user(id:"1"){name}}**
230+
- the server should respond with
231+
232+
```javascript
233+
{
234+
"data": {
235+
"user": {
236+
"name": "Dan"
237+
}
238+
}
239+
}
240+
```
241+
242+
To be standards compliant, the query itself should be URL-encoded.
243+
If you received a GraphQL Syntax Error from the above query, try replacing it with
244+
the URL-encoded version: `%7Buser(id:%221%22)%7Bname%7D%7D`.
245+
(You can URL-encode any string in JavaScript with the global `encodeURI` function.)
246+
247+
Congratulations! You've built your first GraphQL server. Try different queries,
248+
or changing the data, or even adding new fields to the schema.

0 commit comments

Comments
 (0)