@@ -48,10 +48,10 @@ npm install @nestjs/graphql graphql-tools graphql apollo-server-express type-gra
48
48
Update the ` src/app.module.ts ` as follow.
49
49
50
50
``` ts
51
- import { Module } from " @nestjs/common" ;
52
- import { GraphQLModule } from " @nestjs/graphql" ;
53
- import { AppController } from " ./app.controller" ;
54
- import { AppService } from " ./app.service" ;
51
+ import { Module } from ' @nestjs/common' ;
52
+ import { GraphQLModule } from ' @nestjs/graphql' ;
53
+ import { AppController } from ' ./app.controller' ;
54
+ import { AppService } from ' ./app.service' ;
55
55
56
56
@Module ({
57
57
imports: [
@@ -113,7 +113,7 @@ export class AppModule {}
113
113
Create the following file ` src/user/user.entity.ts ` .
114
114
115
115
``` ts
116
- import { Field , ID , ObjectType } from " @nestjs/graphql" ;
116
+ import { Field , ID , ObjectType } from ' @nestjs/graphql' ;
117
117
118
118
@ObjectType ()
119
119
export class User {
@@ -131,7 +131,7 @@ export class User {
131
131
Create the following file ` src/user/create-user.input.ts ` .
132
132
133
133
``` ts
134
- import { Field , InputType } from " @nestjs/graphql" ;
134
+ import { Field , InputType } from ' @nestjs/graphql' ;
135
135
136
136
@InputType ()
137
137
export class CreateUserInput {
@@ -148,16 +148,16 @@ export class CreateUserInput {
148
148
Update ` src/user/user.resolver.ts ` and add the ` users ` query and ` createUser ` mutation.
149
149
150
150
``` ts
151
- import { Args , Mutation , Query , Resolver } from " @nestjs/graphql" ;
152
- import { CreateUserInput } from " ./create-user.input" ;
153
- import { User } from " ./user.entity" ;
151
+ import { Args , Mutation , Query , Resolver } from ' @nestjs/graphql' ;
152
+ import { CreateUserInput } from ' ./create-user.input' ;
153
+ import { User } from ' ./user.entity' ;
154
154
155
155
const users: User [] = [];
156
156
157
- @Resolver (" User" )
157
+ @Resolver (' User' )
158
158
export class UserResolver {
159
159
@Mutation (() => User )
160
- createUser(@Args (" input" ) input : CreateUserInput ) {
160
+ createUser(@Args (' input' ) input : CreateUserInput ) {
161
161
const user = new User ();
162
162
user .name = input .name ;
163
163
user .nickName = input .nickName ;
@@ -240,7 +240,7 @@ Goto the GraphQL Playground - http://localhost:4000/graphql.
240
240
Install the required packages.
241
241
242
242
``` bash
243
- npm install @nestjs/typeorm typeorm mysql
243
+ npm install @nestjs/typeorm typeorm mysql2
244
244
```
245
245
246
246
Add the TypeOrmModule Configuration into ` src/app.module.ts ` as follow.
@@ -294,14 +294,14 @@ export class UserModule {}
294
294
Update the following file ` src/user/user.entity.ts ` .
295
295
296
296
``` ts
297
- import { Field , ID , ObjectType } from " @nestjs/graphql" ;
298
- import { Column , Entity , PrimaryGeneratedColumn } from " typeorm" ;
297
+ import { Field , ID , ObjectType } from ' @nestjs/graphql' ;
298
+ import { Column , Entity , PrimaryGeneratedColumn } from ' typeorm' ;
299
299
300
300
@ObjectType ()
301
301
@Entity ()
302
302
export class User {
303
303
@Field (() => ID )
304
- @PrimaryGeneratedColumn (" uuid" )
304
+ @PrimaryGeneratedColumn (' uuid' )
305
305
id: string ;
306
306
307
307
@Field ()
@@ -325,17 +325,17 @@ nest generate service user --no-spec
325
325
Update the generated file ` src/user/user.service.ts ` as follow.
326
326
327
327
``` ts
328
- import { Injectable } from " @nestjs/common" ;
329
- import { InjectRepository } from " @nestjs/typeorm" ;
330
- import { Repository } from " typeorm" ;
331
- import { CreateUserInput } from " ./create-user.input" ;
332
- import { User } from " ./user.entity" ;
328
+ import { Injectable } from ' @nestjs/common' ;
329
+ import { InjectRepository } from ' @nestjs/typeorm' ;
330
+ import { Repository } from ' typeorm' ;
331
+ import { CreateUserInput } from ' ./create-user.input' ;
332
+ import { User } from ' ./user.entity' ;
333
333
334
334
@Injectable ()
335
335
export class UserService {
336
336
constructor (
337
337
@InjectRepository (User )
338
- private readonly userRepository : Repository <User >
338
+ private readonly userRepository : Repository <User >,
339
339
) {}
340
340
341
341
create(input : CreateUserInput ) {
@@ -363,18 +363,18 @@ export class UserService {
363
363
Update the following file ` src/user/user.resolver.ts ` .
364
364
365
365
``` ts
366
- import { NotFoundException } from " @nestjs/common" ;
367
- import { Args , Mutation , Query , Resolver , ID } from " @nestjs/graphql" ;
368
- import { CreateUserInput } from " ./create-user.input" ;
369
- import { User } from " ./user.entity" ;
370
- import { UserService } from " ./user.service" ;
366
+ import { NotFoundException } from ' @nestjs/common' ;
367
+ import { Args , Mutation , Query , Resolver , ID } from ' @nestjs/graphql' ;
368
+ import { CreateUserInput } from ' ./create-user.input' ;
369
+ import { User } from ' ./user.entity' ;
370
+ import { UserService } from ' ./user.service' ;
371
371
372
372
@Resolver (User )
373
373
export class UserResolver {
374
374
constructor (private readonly userService : UserService ) {}
375
375
376
376
@Query (() => User )
377
- async user(@Args ({ name: " id " , type : () => ID }) id : string ) {
377
+ async user(@Args ({ name: ' id ' , type : () => ID }) id : string ) {
378
378
const user = await this .userService .findOneById (id );
379
379
if (! user ) {
380
380
throw new NotFoundException (id );
@@ -383,12 +383,12 @@ export class UserResolver {
383
383
}
384
384
385
385
@Mutation (() => User )
386
- createUser(@Args (" input" ) input : CreateUserInput ) {
386
+ createUser(@Args (' input' ) input : CreateUserInput ) {
387
387
return this .userService .create (input );
388
388
}
389
389
390
390
@Mutation (() => ID , { nullable: true })
391
- async deleteUser(@Args ({ name: " id " , type : () => ID }) id : string ) {
391
+ async deleteUser(@Args ({ name: ' id ' , type : () => ID }) id : string ) {
392
392
return (await this .userService .delete (id )) ? id : null ;
393
393
}
394
394
@@ -509,4 +509,4 @@ Goto the GraphQL Playground - http://localhost:4000/graphql.
509
509
+ -- ------------------------------------+------+----------+
510
510
| eb777cfa- 65d4- 4a36- 9344 - 5452284647e6 | Mary | NULL |
511
511
+ -- ------------------------------------+------+----------+
512
- ```
512
+ ```
0 commit comments