@@ -6,38 +6,45 @@ permalink: /docs/getting-started/
6
6
next : /docs/videos/
7
7
---
8
8
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 ) ** .
10
10
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 ) ** .)
13
13
14
14
## Setup
15
15
16
- We'll start by making a folder for our demo server.
16
+ Start by making a folder for your demo server:
17
17
18
18
``` sh
19
19
mkdir graphql-demo
20
20
cd graphql-demo
21
21
```
22
22
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:
24
25
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 .
26
27
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.
28
29
29
- We install these three packages by running :
30
+ Install these three packages using ** [ npm ] ( https://docs.npmjs.com/getting-started/installing-node ) ** :
30
31
31
32
``` sh
32
33
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
36
35
```
37
36
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
+ ```
39
46
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 ` :
41
48
42
49
``` json
43
50
{
@@ -47,27 +54,34 @@ Now that we have our packages installed, let's quickly define our data set of us
47
54
},
48
55
"2" : {
49
56
"id" : " 2" ,
50
- "name" : " Lee "
57
+ "name" : " Marie "
51
58
},
52
59
"3" : {
53
60
"id" : " 3" ,
54
- "name" : " Nick "
61
+ "name" : " Jessie "
55
62
}
56
63
}
57
64
```
58
65
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!):
61
72
62
73
``` js
74
+ // Import the required libraries
63
75
var graphql = require (' graphql' );
64
76
var graphqlHTTP = require (' express-graphql' );
65
77
var express = require (' express' );
66
78
67
- // Import our data set from above
79
+ // Import the data you created above
68
80
var data = require (' ./data.json' );
69
81
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).
71
85
var userType = new graphql.GraphQLObjectType ({
72
86
name: ' User' ,
73
87
fields: {
@@ -76,17 +90,24 @@ var userType = new graphql.GraphQLObjectType({
76
90
}
77
91
});
78
92
79
- // Define our schema, with one top level field, named `user`, that
93
+ // Define the schema with one top- level field, `user`, that
80
94
// 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.
81
97
var schema = new graphql.GraphQLSchema ({
82
98
query: new graphql.GraphQLObjectType ({
83
99
name: ' Query' ,
84
100
fields: {
85
101
user: {
86
102
type: userType,
103
+ // `args` describes the arguments that the `user` query accepts
87
104
args: {
88
105
id: { type: graphql .GraphQLString }
89
106
},
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`
90
111
resolve : function (_ , args ) {
91
112
return data[args .id ];
92
113
}
@@ -95,32 +116,31 @@ var schema = new graphql.GraphQLSchema({
95
116
})
96
117
});
97
118
98
- console .log (' Server online!' );
99
119
express ()
100
120
.use (' /graphql' , graphqlHTTP ({ schema: schema, pretty: true }))
101
121
.listen (3000 );
122
+
123
+ console .log (' GraphQL server running on http://localhost:3000/graphql' );
102
124
```
103
125
104
126
<script data-inline >
105
127
var graphql = require (' graphql' );
106
128
107
- // Import our data set from above
108
129
var data = {
109
130
" 1" : {
110
131
" id" : " 1" ,
111
132
" name" : " Dan"
112
133
},
113
134
" 2" : {
114
135
" id" : " 2" ,
115
- " name" : " Lee "
136
+ " name" : " Marie "
116
137
},
117
138
" 3" : {
118
139
" id" : " 3" ,
119
- " name" : " Nick "
140
+ " name" : " Jessie "
120
141
}
121
142
};
122
143
123
- // Define our user type, with two string fields; `id` and `name`
124
144
var userType = new graphql.GraphQLObjectType ({
125
145
name: ' User' ,
126
146
fields: {
@@ -129,8 +149,6 @@ var userType = new graphql.GraphQLObjectType({
129
149
}
130
150
});
131
151
132
- // Define our schema, with one top level field, named `user`, that
133
- // takes an `id` argument and returns the User with that ID.
134
152
var schema = new graphql.GraphQLSchema ({
135
153
query: new graphql.GraphQLObjectType ({
136
154
name: ' Query' ,
@@ -151,20 +169,34 @@ var schema = new graphql.GraphQLSchema({
151
169
global .schema = schema;
152
170
</script >
153
171
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
159
173
160
174
``` sh
161
175
node index.js
162
176
```
163
177
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.
168
200
169
201
<script data-inline >
170
202
import MiniGraphiQL from ' ../_core/MiniGraphiQL' ;
@@ -177,17 +209,40 @@ that user's name.
177
209
` } / > );
178
210
</script >
179
211
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 ` .
183
215
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
+ ```
187
220
{
188
- user(id: "2 ") {
221
+ user(id: "1 ") {
189
222
name
190
223
}
191
224
}
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