Skip to content

Commit 3b589b5

Browse files
Merge branch 'johnbiundo-biundo/graphql-more-edits'
2 parents b50b0af + 6752de4 commit 3b589b5

File tree

3 files changed

+168
-67
lines changed

3 files changed

+168
-67
lines changed

content/graphql/quick-start.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Start by installing the required packages:
1212
$ npm i --save @nestjs/graphql graphql-tools graphql
1313
```
1414

15-
And depending on what you use (Express or Fastify), you need to install `apollo-server-express` or `apollo-server-fastify`.
15+
Depending on what underlying platform you use (Express or Fastify), you must also install either `apollo-server-express` or `apollo-server-fastify`.
1616

1717
#### Overview
1818

19-
Nest offers two ways of building GraphQL applications, the **schema first** and the **code first** methods.
19+
Nest offers two ways of building GraphQL applications, the **code first** and the **schema first** methods. You should choose the one that works best for you. Most of the chapters in this GraphQL section are divided into two main parts: one you should follow if you adopt **code first**, and the other to be used if you adopt **schema first**.
2020

2121
In the **code first** approach, you use decorators and TypeScript classes to generate the corresponding GraphQL schema. This approach is useful if you prefer to work exclusively with TypeScript and avoid context switching between language syntaxes.
2222

23-
In the **schema first** approach, the source of truth is a GraphQL SDL (Schema Definition Language) SDL is a language-agnostic way to share schema files between different platforms. Nest automatically generates your TypeScript definitions based on the GraphQL schemas (using either classes or interfaces) to reduce redundancy.
23+
In the **schema first** approach, the source of truth is GraphQL SDL (Schema Definition Language) files. SDL is a language-agnostic way to share schema files between different platforms. Nest automatically generates your TypeScript definitions (using either classes or interfaces) based on the GraphQL schemas to reduce the need to write redundant boilerplate code.
2424

2525
#### Getting started with GraphQL & TypeScript
2626

@@ -36,7 +36,7 @@ import { GraphQLModule } from '@nestjs/graphql';
3636
GraphQLModule.forRoot({}),
3737
],
3838
})
39-
export class ApplicationModule {}
39+
export class AppModule {}
4040
```
4141

4242
The `forRoot()` method takes an options object as an argument. These options are passed through to the underlying Apollo instance (read more about available settings [here](https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html#constructor-options-lt-ApolloServer-gt)). For example, if you want to disable the `playground` and turn off `debug` mode, pass the following options:
@@ -54,16 +54,18 @@ import { GraphQLModule } from '@nestjs/graphql';
5454
}),
5555
],
5656
})
57-
export class ApplicationModule {}
57+
export class AppModule {}
5858
```
5959

6060
As mentioned, these options will be forwarded to the `ApolloServer` constructor.
6161

6262
<app-banner-enterprise></app-banner-enterprise>
6363

64-
#### Playground
64+
#### GraphQL playground
6565

66-
The playground is a graphical, interactive, in-browser GraphQL IDE, available by default on the same URL as the GraphQL server itself. With your application running in the background, open your web browser and navigate to `http://localhost:3000/graphql` (host and port may vary depending on your configuration). You should see the GraphQL playground, as shown below.
66+
The playground is a graphical, interactive, in-browser GraphQL IDE, available by default on the same URL as the GraphQL server itself. To access the playground, you need a basic GraphQL server configured and running. To see it now, you can install and build the [working example here](https://github.com/nestjs/nest/tree/master/sample/23-graphql-code-first). Alternatively, if you're following along with these code samples, once you've complete the steps in the [Resolvers chapter](/graphql/resolvers-map), you can access the playground.
67+
68+
With that in place, and with your application running in the background, you can then open your web browser and navigate to `http://localhost:3000/graphql` (host and port may vary depending on your configuration). You will then see the GraphQL playground, as shown below.
6769

6870
<figure>
6971
<img src="/assets/playground.png" alt="" />
@@ -83,11 +85,11 @@ GraphQLModule.forRoot({
8385

8486
In the **code first** approach, you use decorators and TypeScript classes to generate the corresponding GraphQL schema.
8587

86-
To use the code first approach, start by adding the `autoSchemaFile` property to the options object.
88+
To use the code first approach, start by adding the `autoSchemaFile` property to the options object:
8789

8890
```typescript
8991
GraphQLModule.forRoot({
90-
autoSchemaFile: 'schema.gql',
92+
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
9193
}),
9294
```
9395

@@ -99,21 +101,19 @@ GraphQLModule.forRoot({
99101
}),
100102
```
101103

102-
A fully working sample is available [here](https://github.com/nestjs/nest/tree/master/sample/23-graphql-code-first).
104+
A fully working code first sample is available [here](https://github.com/nestjs/nest/tree/master/sample/23-graphql-code-first).
103105

104106
#### Schema first
105107

106-
To use the schema first approach, start by adding a `typePaths` property to the options object.
108+
To use the schema first approach, start by adding a `typePaths` property to the options object. The `typePaths` property indicates where the `GraphQLModule` should look for GraphQL SDL schema definition files you'll be writing. These files will be combined in memory; this allows you to split your schemas into several files and locate them near their resolvers.
107109

108110
```typescript
109111
GraphQLModule.forRoot({
110112
typePaths: ['./**/*.graphql'],
111113
}),
112114
```
113115

114-
The `typePaths` property indicates where the `GraphQLModule` should look for GraphQL files. These files will be combined in memory; this allows you to split your schemas into several files and locate them near their resolvers.
115-
116-
Creating GraphQL types and corresponding TypeScript definitions is redundant and tedious. It leaves us without a single source of truth -- each change made within SDL forces us to adjust interfaces as well. To address this, the `@nestjs/graphql` package can automatically generate TS definitions from the abstract syntax tree (AST). To enable this feature, add the `definitions` options property when configuring the `GraphQLModule`.
116+
You will typically also need to have TypeScript definitions (classes and interfaces) that correspond to the GraphQL SDL types. Creating the corresponding TypeScript definitions by hand is redundant and tedious. It leaves us without a single source of truth -- each change made within SDL forces us to adjust TypeScript definitions as well. To address this, the `@nestjs/graphql` package can **automatically generate** TypeScript definitions from the abstract syntax tree ([AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)). To enable this feature, add the `definitions` options property when configuring the `GraphQLModule`.
117117

118118
```typescript
119119
GraphQLModule.forRoot({
@@ -124,7 +124,7 @@ GraphQLModule.forRoot({
124124
}),
125125
```
126126

127-
The path property of the `definitions` object (e.g., `src/graphql.ts` above) indicates where to save TypeScript output. By default, all types are generated as interfaces. To generate classes instead, specify the `outputAs` property with a value of `'class'`.
127+
The path property of the `definitions` object indicates where to save generated TypeScript output. By default, all generated TypeScript types are created as interfaces. To generate classes instead, specify the `outputAs` property with a value of `'class'`.
128128

129129
```typescript
130130
GraphQLModule.forRoot({
@@ -136,7 +136,7 @@ GraphQLModule.forRoot({
136136
}),
137137
```
138138

139-
The above approach dynamically generates type definitions each time the application starts. Alternatively, it may be preferable to build a simple script to generate these on demand. For example, assume we create the following script as `generate-typings.ts`:
139+
The above approach dynamically generates TypeScript definitions each time the application starts. Alternatively, it may be preferable to build a simple script to generate these on demand. For example, assume we create the following script as `generate-typings.ts`:
140140

141141
```typescript
142142
import { GraphQLDefinitionsFactory } from '@nestjs/graphql';
@@ -150,7 +150,7 @@ definitionsFactory.generate({
150150
});
151151
```
152152

153-
Now you can run this script only when needed:
153+
Now you can run this script on demand:
154154

155155
```bash
156156
$ ts-node generate-typings
@@ -169,7 +169,7 @@ definitionsFactory.generate({
169169
});
170170
```
171171

172-
A fully working sample is available [here](https://github.com/nestjs/nest/tree/master/sample/12-graphql-schema-first).
172+
A fully working schema first sample is available [here](https://github.com/nestjs/nest/tree/master/sample/12-graphql-schema-first).
173173

174174
#### Async configuration
175175

0 commit comments

Comments
 (0)