You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/getting-started.mdx
+49-37Lines changed: 49 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
title: Get started with Apollo Server
3
-
description: ""
3
+
description: ''
4
4
---
5
5
6
6
This tutorial helps you:
7
7
8
-
* Obtain a basic understanding of GraphQL principles
9
-
* Define a GraphQL **schema** that represents the structure of your data set
10
-
* Run an instance of Apollo Server that lets you execute queries against your schema
8
+
- Obtain a basic understanding of GraphQL principles
9
+
- Define a GraphQL **schema** that represents the structure of your data set
10
+
- Run an instance of Apollo Server that lets you execute queries against your schema
11
11
12
12
This tutorial assumes that you are familiar with the command line and
13
13
JavaScript and have installed a recent Node.js (14+) version. Additionally, for those interested, this tutorial includes an optional section describing how to set up Apollo Server with TypeScript.
@@ -16,24 +16,26 @@ JavaScript and have installed a recent Node.js (14+) version. Additionally, for
16
16
17
17
1. From your preferred development directory, create a directory for a new project and `cd` into it:
18
18
19
-
```bash
20
-
mkdir graphql-server-example
21
-
cd graphql-server-example
22
-
```
19
+
```bash
20
+
mkdir graphql-server-example
21
+
cd graphql-server-example
22
+
```
23
23
24
24
2. Initialize a new Node.js project with `npm` (or another package manager you
25
-
prefer, such as Yarn):
25
+
prefer, such as Yarn):
26
26
27
-
```bash
28
-
npm init --yes
29
-
```
27
+
```bash
28
+
npm init --yes
29
+
```
30
30
31
31
Your project directory now contains a `package.json` file.
32
32
33
33
## Step 2: Install dependencies
34
+
34
35
Applications that run Apollo Server require two top-level dependencies:
35
-
*[`graphql`](https://npm.im/graphql) (also known as `graphql-js`) is the library that implements the core GraphQL parsing and execution algorithms.
36
-
*[`@apollo/server`](https://www.npmjs.com/package/@apollo/server) is the main library for Apollo Server itself. Apollo Server knows how to turn HTTP requests and responses into GraphQL operations and run them in an extensible context with support for plugins and other features.
36
+
37
+
-[`graphql`](https://npm.im/graphql) (also known as `graphql-js`) is the library that implements the core GraphQL parsing and execution algorithms.
38
+
-[`@apollo/server`](https://www.npmjs.com/package/@apollo/server) is the main library for Apollo Server itself. Apollo Server knows how to turn HTTP requests and responses into GraphQL operations and run them in an extensible context with support for plugins and other features.
37
39
38
40
Run the following command to install both of these packages and save them in
39
41
your project's `node_modules` directory:
@@ -55,10 +57,10 @@ mkdir src
55
57
touch src/index.ts
56
58
```
57
59
58
-
2. Run the following command to install the `typescript`package into your project's dev dependencies:
60
+
2. Run the following command to install the `typescript`and `@types/node` packages into your project's dev dependencies:
59
61
60
62
```bash
61
-
npm install --save-dev typescript
63
+
npm install --save-dev typescript @types/node
62
64
```
63
65
64
66
3. Next, create a `tsconfig.json` file in your project:
@@ -74,25 +76,29 @@ touch tsconfig.json
74
76
"compilerOptions": {
75
77
"rootDirs": ["src"],
76
78
"outDir": "dist",
77
-
"lib": ["es2019"],
78
-
"target": "es2019",
79
+
"lib": ["es2020"],
80
+
"target": "es2020",
79
81
"module": "esnext",
80
82
"moduleResolution": "node",
81
-
"esModuleInterop": true
83
+
"esModuleInterop": true,
84
+
"types": ["node"]
82
85
}
83
86
}
84
87
```
85
88
86
89
For more information on the compiler options above, see the [TypeScript Compiler docs](https://www.typescriptlang.org/tsconfig).
87
90
88
91
5. Finally, replace the default `scripts` entry in your `package.json` file with the following `type` and `scripts` entries:
92
+
89
93
```json title="package.json"
90
94
{
91
95
// ...etc.
92
96
"type": "module",
93
97
"scripts": {
94
-
"start": "tsc && node ./dist/index.js"
95
-
},
98
+
"compile": "tsc",
99
+
"postinstall": "npm run compile",
100
+
"start": "npm run compile && node ./dist/index.js"
101
+
}
96
102
// other dependencies
97
103
}
98
104
```
@@ -103,7 +109,7 @@ The above `start` script tells TypeScript to compile your code into JavaScript b
@@ -119,13 +125,14 @@ If you are using JavaScript, create a `index.js` file that will contain **all**
119
125
```
120
126
121
127
Now replace the default `scripts` entry in your `package.json` file with these `type` and `scripts` entries:
128
+
122
129
```json title="package.json"
123
130
{
124
131
// ...etc.
125
132
"type": "module",
126
133
"scripts": {
127
134
"start": "node index.js"
128
-
},
135
+
}
129
136
// other dependencies
130
137
}
131
138
```
@@ -171,7 +178,6 @@ const typeDefs = `#graphql
171
178
172
179
</MultiCodeBlock>
173
180
174
-
175
181
> Adding `#graphql` to the beginning of a [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) provides GraphQL syntax highlighting in supporting IDEs.
176
182
177
183
This snippet defines a simple, valid GraphQL schema. Clients will be able to execute a query named `books`, and our server will return an array of zero or more `Book`s.
@@ -279,14 +285,14 @@ We can now execute GraphQL queries on our server. To execute our first query, we
279
285
280
286
Visit `http://localhost:4000` in your browser, which will open the Apollo Sandbox:
* An Operations panel for writing and executing queries (in the middle)
287
-
* A Response panel for viewing query results (on the right)
288
-
* Tabs for schema exploration, search, and settings (on the left)
289
-
* A URL bar for connecting to other GraphQL servers (in the upper left)
292
+
- An Operations panel for writing and executing queries (in the middle)
293
+
- A Response panel for viewing query results (on the right)
294
+
- Tabs for schema exploration, search, and settings (on the left)
295
+
- A URL bar for connecting to other GraphQL servers (in the upper left)
290
296
291
297
Our server supports a single query named `books`. Let's execute it!
292
298
@@ -303,7 +309,12 @@ query GetBooks {
303
309
304
310
Paste this string into the Operations panel and click the blue button in the upper right. The results (from our hardcoded data set) appear in the Response panel:
> **Note:** If Apollo Sandbox can't find your schema, ensure you have introspection enabled by passing `introspection: true` to the `ApolloServer` constructor. We recommend disabling introspection when using Apollo Server in a production environment.
309
320
@@ -314,7 +325,10 @@ One of the most important concepts of GraphQL is that clients can choose to quer
314
325
You can view and fork the complete example on Github:
@@ -331,10 +345,8 @@ This example application is a great starting point for working with
331
345
Apollo Server. Check out the following resources to learn more about the basics
332
346
of schemas, resolvers, and deployment:
333
347
334
-
*[Schema basics](/apollo-server/schema/schema/)
335
-
*[Resolvers](/apollo-server/data/resolvers/)
336
-
*[Deploying with Heroku](/apollo-server/deployment/heroku/)
337
-
338
-
Want to learn how to modularize and scale a GraphQL API? Check out the [Apollo Federation Docs](/federation) to learn how a federated architecture can create a unified *supergraph* that combines multiple GraphQL APIs.
339
-
348
+
-[Schema basics](/apollo-server/schema/schema/)
349
+
-[Resolvers](/apollo-server/data/resolvers/)
350
+
-[Deploying with Heroku](/apollo-server/deployment/heroku/)
340
351
352
+
Want to learn how to modularize and scale a GraphQL API? Check out the [Apollo Federation Docs](/federation) to learn how a federated architecture can create a unified _supergraph_ that combines multiple GraphQL APIs.
0 commit comments