{"id":326798,"date":"2022-01-10T07:26:51","date_gmt":"2022-01-10T07:26:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326798"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326798","title":{"rendered":"<span>NestJS + GraphQL + Lambda<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0426\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c GraphQL \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 NestJS. \u0410 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u041b\u044f\u043c\u0431\u0434\u0430-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Terraform. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043c\u043d\u043e\u0433\u0438\u043c \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 PostgreSQL. \u0414\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u044c\u043c\u0435\u043c docker-compose:<\/p>\n<pre><code class=\"yaml\">version: '3.1'  services:   db:     image: 'postgres:14.1'     restart: unless-stopped     volumes:       - .\/volumes\/postgresql\/data:\/var\/lib\/postgresql\/data     environment:       POSTGRES_PASSWORD: example       POSTGRES_DB: nest     ports:       - 5432:5432     networks:       - postgres  networks:   postgres:     driver: bridge<\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"800\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/743\/ee0\/af8\/743ee0af8ec89167c1c2c25ce93f4a11.gif\" data-width=\"1280\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 (\u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\" rel=\"noopener noreferrer nofollow\">Nest CLI<\/a>):<\/p>\n<pre><code class=\"bash\">nest new app<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"bash\">nest generate module user nest generate service user<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0431\u0430\u0437\u044b \u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043b\u044f GraphQL:<\/p>\n<pre><code class=\"javascript\">@Entity() @ObjectType() export class User {     @PrimaryGeneratedColumn()     @Field(type => Int)     id: number;      @Column({nullable: false})     @Field({nullable: false})     name: string;      @Column({nullable: true})     @Field({nullable: true})     dob: Date;      @Column({nullable: true})     @Field({nullable: true})     address: string;      @Column({nullable: true})     @Field({nullable: true})     description: string;      @Column({nullable: true})     @Field({nullable: true})     imageUrl: string;      @Column({nullable: true, default: new Date()})     @Field({nullable: true})     createdAt: Date;      @Column({nullable: true, default: new Date()})     @Field({nullable: true})     updatedAt: Date; }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0440\u0435\u0448\u0430\u043b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 CRUD, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0435\u0439<\/p>\n<pre><code class=\"javascript\">@Injectable() export class UserService {     constructor(         @Inject(USER_REPOSITORY)         private userRepository: Repository&lt;User>     ) {     }      create(data: TUserCreate): Promise&lt;User> {         const user = this.userRepository.create(data)         return this.userRepository.save(user)     }      findById(id: number): Promise&lt;User> {         return this.userRepository.findOne(id)     }      async findAll(searchText: string = '', take: number = 10, skip: number = 0): Promise&lt;UserSearchResult> {          const query = searchText ? {             where: [                 {name: ILike('%'+searchText+'%')}             ]         } : {}          const getQuery = {             ...query,             take,             skip,             order: {                 name: \"ASC\",             }         }         const [total, list] = await Promise.all([this.userRepository.count(query), this.userRepository.find(getQuery as FindManyOptions)])         return {             total, list         } as UserSearchResult     }      async updateById(id: number, data: TUserUpdate): Promise&lt;User> {         await this.userRepository.update({id}, data)         return this.findById(id)     }      async deleteById(id: number): Promise&lt;boolean> {         return !!(await this.userRepository.delete({id}))     } }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0438\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043b\u0430\u0441\u0441\u0430-\u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"javascript\">@Resolver(of => User) export class UsersResolver {     constructor(         private userService: UserService,     ) {     }      @Mutation(returns => User)     async createUser(         @Args('name', {type: () => String}) name: string,         @Args('address', {type: () => String}) address: string = '',         @Args('description', {type: () => String}) description: string = '',         @Args('imageUrl', {type: () => String}) imageUrl: string = '',         @Args('dob', {type: () => String}) dob: string = null,     ) {         return this.userService.create({             name,             address,             description,             imageUrl,             dob: dob ? new Date(dob) : null         } as TUserCreate);     }      @Query(returns => User)     async getUser(@Args('id', {type: () => Int}) id: number) {         return this.userService.findById(id);     }      @Query(returns => UserSearchResult)     async getAllUsers(         @Args('searchText', {type: () => String}) searchText: string,         @Args('take', {type: () => Int}) take: number=10,         @Args('skip', {type: () => Int}) skip: number=0,     ) {         return this.userService.findAll(searchText, take, skip)     }      @Mutation(returns => User)     async updateUser(         @Args('id', {type: () => Int}) id: number,         @Args('name', {type: () => String}) name: string,         @Args('address', {type: () => String}) address: string = '',         @Args('description', {type: () => String}) description: string = '',         @Args('imageUrl', {type: () => String}) imageUrl: string = '',         @Args('dob', {type: () => String}) dob: string = null,     ) {         return this.userService.updateById(id, {             name,             address,             description,             imageUrl,             dob: dob ? new Date(dob) : null         } as TUserUpdate);     }      @Mutation(returns => Boolean)     async deleteUser(@Args('id', {type: () => Int}) id: number) {         return this.userService.deleteById(id);     }  }<\/code><\/pre>\n<p>\u0412 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e GraphQL. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0438 \u044d\u0442\u043e \u0432\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043e <a href=\"https:\/\/docs.nestjs.com\/graphql\/quick-start\" rel=\"noopener noreferrer nofollow\">GraphQL \u0434\u043b\u044f NestJS<\/a>, \u043d\u043e \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e GraphQL Playground \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043f\u0443\u0442\u0438 <code>\/graphql<\/code>. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 Lambda \u0447\u0435\u0440\u0435\u0437 ApiGateway, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c stage \u0441 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u0438\u043c\u0435\u043d\u0435\u043c, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u043f\u0443\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \/api. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0443\u0442\u044c \u0434\u043b\u044f GraphQL Playground \u0441 <code>\/graphql<\/code>  \u0432 <code>\/api\/graphql<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>useGlobalPrefix:true<\/code>. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 express \u0434\u043e\u0431\u0430\u0432\u0438\u043c <code>app.setGlobalPrefix('api');<\/code><\/p>\n<pre><code class=\"javascript\">@Module({     imports: [         DatabaseModule,         GraphQLModule.forRootAsync({             useFactory: () => {                 const schemaModuleOptions: Partial&lt;GqlModuleOptions> = {};                  \/\/ If we are in development, we want to generate the schema.graphql                 if (process.env.NODE_ENV !== 'production' || process.env.IS_OFFLINE) {                     schemaModuleOptions.autoSchemaFile = 'src\/user\/user.schema.gql';                 } else {                     \/\/ For production, the file should be generated                     schemaModuleOptions.typePaths = ['*.gql'];                 }                  return {                     context: ({req}) => ({req}),                     useGlobalPrefix:true, \/\/ &lt;==                     playground: true, \/\/ Allow playground in production                     introspection: true, \/\/ Allow introspection in production                     ...schemaModuleOptions,                 };             }         } as GqlModuleAsyncOptions),     ],     providers: [         ...userProviders,         UserService,         UsersResolver     ] })<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c Playground \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" alt=\"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\" title=\"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\" height=\"800\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a02\/447\/725\/a0244772546323561d0c2f34fc6f7ea1.gif\" data-width=\"1280\"\/><figcaption>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/figcaption><\/figure>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" alt=\"\u041f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\" title=\"\u041f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\" height=\"800\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1b6\/18e\/5c8\/1b618e5c8a2df10f7510d2ecbaf7e09d.gif\" data-width=\"1280\"\/><figcaption>\u041f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432 Lambda \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 <code>express<\/code> \u043d\u0430 <code>aws-serverless-express<\/code><\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c app.ts:<\/p>\n<pre><code class=\"javascript\">import {NestFactory} from '@nestjs\/core'; import {ExpressAdapter} from '@nestjs\/platform-express'; import {INestApplication} from '@nestjs\/common'; import {AppModule} from '.\/app.module'; import * as express from 'express'; import {Express} from 'express'; import {Server} from \"http\"; import {createServer} from 'aws-serverless-express';  export async function createApp(     expressApp: Express, ): Promise&lt;INestApplication> {     const app = await NestFactory.create(         AppModule,         new ExpressAdapter(expressApp),     );     app.setGlobalPrefix('api');     return app; }  export async function bootstrap(): Promise&lt;Server> {     const expressApp = express();     const app = await createApp(expressApp);     await app.init();     return createServer(expressApp); }<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0444\u0430\u0439\u043b \u0441 handler \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043b\u044f\u043c\u0431\u0434\u044b:<\/p>\n<pre><code class=\"javascript\">import {Server} from 'http'; import {Context} from 'aws-lambda'; import {proxy, Response} from 'aws-serverless-express'; import {bootstrap} from '.\/app';  let cachedServer: Server;  export async function handler(event: any, context: Context): Promise&lt;Response> {     if (!cachedServer) {         cachedServer = await bootstrap();     }     return proxy(cachedServer, event, context, 'PROMISE').promise; } <\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0434\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u0432 AWS. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/learn.hashicorp.com\/tutorials\/terraform\/install-cli?in=terraform\/aws-get-started\" rel=\"noopener noreferrer nofollow\">Terraform<\/a>. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 terraform, \u0430 \u0432 \u043d\u0435\u0439 \u0444\u0430\u0439\u043b <code>main.tf<\/code> . \u0414\u0430\u043b\u044c\u0448\u0435 \u043a\u0438\u0434\u0430\u044e \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433 \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044e:<\/p>\n<pre><code class=\"yaml\"># \u0417\u0430\u0434\u0430\u0434\u0438\u043c \u0440\u0435\u0433\u0438\u043e\u043d \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e provider \"aws\" {   region = \"us-east-1\" }  # \u0414\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u0443 \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 zip \u0430\u0440\u0445\u0438\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0432 \u0430\u0440\u0445\u0438\u0432 data \"archive_file\" \"app_zip\" {   type        = \"zip\"   source_dir  = \"..\/app\/dist\"   output_path = \".\/app.zip\" }  # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c API GW resource \"aws_apigatewayv2_api\" \"app\" {   name          = \"api\"   protocol_type = \"HTTP\" }  # \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e stage.  resource \"aws_apigatewayv2_stage\" \"app\" {   api_id = aws_apigatewayv2_api.app.id    name        = \"api\"   auto_deploy = true    # \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f API GW \u0432 CloudWatch   access_log_settings {     destination_arn = aws_cloudwatch_log_group.api_gw.arn      format = jsonencode({       requestId               = \"$context.requestId\"       sourceIp                = \"$context.identity.sourceIp\"       requestTime             = \"$context.requestTime\"       protocol                = \"$context.protocol\"       httpMethod              = \"$context.httpMethod\"       resourcePath            = \"$context.resourcePath\"       routeKey                = \"$context.routeKey\"       status                  = \"$context.status\"       responseLength          = \"$context.responseLength\"       integrationErrorMessage = \"$context.integrationErrorMessage\"     }     )   } }  # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e Lambda \u0432 API GW resource \"aws_apigatewayv2_integration\" \"app\" {   api_id = aws_apigatewayv2_api.app.id    integration_uri    = aws_lambda_function.app.invoke_arn   integration_type   = \"AWS_PROXY\"   integration_method = \"POST\" } # \u0414\u043e\u0431\u0430\u0432\u0438\u043c Route - \u043b\u044e\u0431\u043e\u0439 route \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043b\u044f\u043c\u0431\u0434\u0443 resource \"aws_apigatewayv2_route\" \"app\" {   api_id = aws_apigatewayv2_api.app.id    route_key = \"ANY \/{proxy+}\"   target    = \"integrations\/${aws_apigatewayv2_integration.app.id}\" } # \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433 \u0433\u0440\u0443\u043f\u043f\u0443 \u0432 Cloud Watch \u0434\u043b\u044f API GW resource \"aws_cloudwatch_log_group\" \"api_gw\" {   name = \"\/aws\/api_gw\/${aws_apigatewayv2_api.app.name}\"   retention_in_days = 30 }  # \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043c API GW \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e resource \"aws_lambda_permission\" \"api_gw\" {   statement_id  = \"AllowExecutionFromAPIGateway\"   action        = \"lambda:InvokeFunction\"   function_name = aws_lambda_function.app.function_name   principal     = \"apigateway.amazonaws.com\"    source_arn = \"${aws_apigatewayv2_api.app.execution_arn}\/*\/*\" } # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c Security Group \u0434\u043b\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043c \u0435\u0435 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u0441\u0442\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u043d\u0435\u0435 \u0438\u0437 \u0432\u043d\u0435 # \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u044d\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u0435\u043c\u043e. \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u043d \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. resource \"aws_security_group\" \"allow_db\" {   name        = \"allow_db\"   description = \"Allow DB\"    ingress {     from_port        = 5430     to_port          = 5440     protocol         = \"tcp\"     cidr_blocks      = [\"0.0.0.0\/0\"]     ipv6_cidr_blocks = [\"::\/0\"]   }   egress {     from_port        = 0     to_port          = 0     protocol         = \"-1\"     cidr_blocks      = [\"0.0.0.0\/0\"]     ipv6_cidr_blocks = [\"::\/0\"]   } } # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0439 \u043f\u0430\u0440\u043e\u043b\u044c \u0434\u043b\u044f \u0431\u0430\u0437\u044b resource \"random_password\" \"password\" {   length           = 20   special          = false   override_special = \"_%@\" } # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0431\u0430\u0437\u044b resource \"aws_db_instance\" \"default\" {   allocated_storage      = 20   db_subnet_group_name   = aws_db_subnet_group.db_subnet_group.name   engine                 = \"postgres\"   identifier             = \"dev-db\"   engine_version         = \"13\"   instance_class         = \"db.t3.micro\"   name                   = \"nest\"   username               = \"postgres\"   password               = random_password.password.result   skip_final_snapshot    = true   publicly_accessible    = true   vpc_security_group_ids = [aws_security_group.allow_db.id]  }  # \u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u043e\u0434\u0441\u0435\u0442\u044c 'a' \u0434\u043b\u044f \u0440\u0435\u0433\u0438\u043e\u043d\u0430 us-east-1 resource \"aws_default_subnet\" \"db_subnet_a\" {   availability_zone = \"us-east-1a\"   tags = {     Name = \"Default subnet for us-east-1a\"   } }  # \u041d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043f\u043e\u0434\u0441\u0435\u0442\u044c 'b' \u0434\u043b\u044f \u0440\u0435\u0433\u0438\u043e\u043d\u0430 us-east-1 resource \"aws_default_subnet\" \"db_subnet_b\" {   availability_zone = \"us-east-1b\"    tags = {     Name = \"Default subnet for us-east-1b\"   } }  # \u041e\u0431\u044a\u0435\u0434\u0435\u043d\u0438\u043c \u043f\u043e\u0434\u0441\u0435\u0442\u0438 \u0432 \u0433\u0440\u0443\u043f\u043f\u0443 resource \"aws_db_subnet_group\" \"db_subnet_group\" {   name       = \"db_subnet_group\"   subnet_ids = [aws_default_subnet.db_subnet_a.id, aws_default_subnet.db_subnet_b.id] }  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u0440\u0445\u0438\u0432 \u0441 \u043a\u043e\u0434\u043e\u043c resource \"aws_lambda_function\" \"app\" {   filename         = data.archive_file.app_zip.output_path   source_code_hash = data.archive_file.app_zip.output_base64sha256   function_name    = \"app\"   handler          = \"serverless.handler\"   runtime          = \"nodejs14.x\"   memory_size      = 1024   role             = aws_iam_role.lambda_exec.arn   timeout          = 30   # \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043d\u043d\u044b\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f, \u0443\u043a\u0430\u0437\u0430\u0432 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0431\u0430\u0437\u0435   environment {     variables = {       POSTGRES_HOST     = aws_db_instance.default.address       POSTGRES_PORT     = aws_db_instance.default.port       POSTGRES_USER     = aws_db_instance.default.username       POSTGRES_PASSWORD = random_password.password.result       POSTGRES_DATABASE = aws_db_instance.default.name       NODE_ENV          = \"production\"     }   } }  # \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433 \u0433\u0440\u0443\u043f\u043f\u0443 \u0432 CloudWatch \u0434\u043b\u044f \u043b\u044f\u043c\u0431\u0434\u0430-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 resource \"aws_cloudwatch_log_group\" \"app\" {   name = \"\/aws\/lambda\/${aws_lambda_function.app.function_name}\"   retention_in_days = 30 }  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u0434\u043b\u044f \u043b\u044f\u043c\u0431\u0434\u044b resource \"aws_iam_role\" \"lambda_exec\" {   name = \"serverless_lambda\"    assume_role_policy = jsonencode({     Version   = \"2012-10-17\"     Statement = [       {         Action    = \"sts:AssumeRole\"         Effect    = \"Allow\"         Sid       = \"\"         Principal = {           Service = \"lambda.amazonaws.com\"         }       }     ]   }) }  # \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043f\u043e\u043b\u0438\u0441\u0438 \u043a \u0440\u043e\u043b\u0438 \u0441 \u0434\u043e\u0441\u0442\u0443\u043f \u043a VPC resource \"aws_iam_role_policy_attachment\" \"lambda_policy\" {   role       = aws_iam_role.lambda_exec.name   policy_arn = \"arn:aws:iam::aws:policy\/service-role\/AWSLambdaVPCAccessExecutionRole\" } <\/code><\/pre>\n<pre><code class=\"bash\">terraform apply<\/code><\/pre>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"580\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/051\/c09\/72b\/051c0972bc3bfb8e7ba1b8f1b4025ec0.gif\" data-width=\"1280\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432 \u0432\u0430\u0448\u0435\u043c AWS \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0443\u0442\u0441\u044f \u043d\u0443\u0436\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u044b:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"844\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2c8\/72e\/231\/2c872e231e9c2c3f739b45f6e7976c46.gif\" data-width=\"1280\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c Terraform \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u0435\u043d \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432 \u043e\u0431\u043b\u0430\u043a\u0435. \u041c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0430\u043a\u043a\u0430\u0443\u043d\u0442 AWS \u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u0432 \u043d\u0435\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 <code>terraform destroy<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c GraphQL Playground \u0432 \u043b\u044f\u043c\u0431\u0434\u0435:<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"800\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/320\/81f\/5c4\/32081f5c4dcca02a7253ce378dc2b65b.gif\" data-width=\"1280\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u043b\u044f\u043c\u0431\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441 GraphQL \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 NestJS \u0438 \u0437\u0430\u0434\u0435\u043f\u043b\u043e\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Terraform. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0440\u0435\u0430\u043b\u0437\u0438\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u043d\u0430 \u0441\u0445\u043e\u0436\u0438\u0445 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f\u0445. \u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0433\u043b\u044f\u043d\u0443\u0442\u044c <a href=\"https:\/\/github.com\/avkos\/graphql-nest-serverless\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/599797\/\"> https:\/\/habr.com\/ru\/post\/599797\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0426\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c GraphQL \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0435 NestJS. \u0410 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u041b\u044f\u043c\u0431\u0434\u0430-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Terraform. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043c\u043d\u043e\u0433\u0438\u043c \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0440\u0435\u043b\u044f\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 PostgreSQL. \u0414\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u044c\u043c\u0435\u043c docker-compose:<\/p>\n<pre><code class=\"yaml\">version: '3.1'  services:   db:     image: 'postgres:14.1'     restart: unless-stopped     volumes:       - .\/volumes\/postgresql\/data:\/var\/lib\/postgresql\/data     environment:       POSTGRES_PASSWORD: example       POSTGRES_DB: nest     ports:       - 5432:5432     networks:       - postgres  networks:   postgres:     driver: bridge<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 (\u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <a href=\"https:\/\/docs.nestjs.com\/cli\/overview\" rel=\"noopener noreferrer nofollow\">Nest CLI<\/a>):<\/p>\n<pre><code class=\"bash\">nest new app<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0438 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"bash\">nest generate module user nest generate service user<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0431\u0430\u0437\u044b \u0438 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0434\u043b\u044f GraphQL:<\/p>\n<pre><code class=\"javascript\">@Entity() @ObjectType() export class User {     @PrimaryGeneratedColumn()     @Field(type => Int)     id: number;      @Column({nullable: false})     @Field({nullable: false})     name: string;      @Column({nullable: true})     @Field({nullable: true})     dob: Date;      @Column({nullable: true})     @Field({nullable: true})     address: string;      @Column({nullable: true})     @Field({nullable: true})     description: string;      @Column({nullable: true})     @Field({nullable: true})     imageUrl: string;      @Column({nullable: true, default: new Date()})     @Field({nullable: true})     createdAt: Date;      @Column({nullable: true, default: new Date()})     @Field({nullable: true})     updatedAt: Date; }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0440\u0435\u0448\u0430\u043b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 CRUD, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0435\u0439<\/p>\n<pre><code class=\"javascript\">@Injectable() export class UserService {     constructor(         @Inject(USER_REPOSITORY)         private userRepository: Repository&lt;User>     ) {     }      create(data: TUserCreate): Promise&lt;User> {         const user = this.userRepository.create(data)         return this.userRepository.save(user)     }      findById(id: number): Promise&lt;User> {         return this.userRepository.findOne(id)     }      async findAll(searchText: string = '', take: number = 10, skip: number = 0): Promise&lt;UserSearchResult> {          const query = searchText ? {             where: [                 {name: ILike('%'+searchText+'%')}             ]         } : {}          const getQuery = {             ...query,             take,             skip,             order: {                 name: \"ASC\",             }         }         const [total, list] = await Promise.all([this.userRepository.count(query), this.userRepository.find(getQuery as FindManyOptions)])         return {             total, list         } as UserSearchResult     }      async updateById(id: number, data: TUserUpdate): Promise&lt;User> {         await this.userRepository.update({id}, data)         return this.findById(id)     }      async deleteById(id: number): Promise&lt;boolean> {         return !!(await this.userRepository.delete({id}))     } }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u043c \u0438\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043b\u0430\u0441\u0441\u0430-\u0440\u0435\u0437\u043e\u043b\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"javascript\">@Resolver(of => User) export class UsersResolver {     constructor(         private userService: UserService,     ) {     }      @Mutation(returns => User)     async createUser(         @Args('name', {type: () => String}) name: string,         @Args('address', {type: () => String}) address: string = '',         @Args('description', {type: () => String}) description: string = '',         @Args('imageUrl', {type: () => String}) imageUrl: string = '',         @Args('dob', {type: () => String}) dob: string = null,     ) {         return this.userService.create({             name,             address,             description,             imageUrl,             dob: dob ? new Date(dob) : null         } as TUserCreate);     }      @Query(returns => User)     async getUser(@Args('id', {type: () => Int}) id: number) {         return this.userService.findById(id);     }      @Query(returns => UserSearchResult)     async getAllUsers(         @Args('searchText', {type: () => String}) searchText: string,         @Args('take', {type: () => Int}) take: number=10,         @Args('skip', {type: () => Int}) skip: number=0,     ) {         return this.userService.findAll(searchText, take, skip)     }      @Mutation(returns => User)     async updateUser(         @Args('id', {type: () => Int}) id: number,         @Args('name', {type: () => String}) name: string,         @Args('address', {type: () => String}) address: string = '',         @Args('description', {type: () => String}) description: string = '',         @Args('imageUrl', {type: () => String}) imageUrl: string = '',         @Args('dob', {type: () => String}) dob: string = null,     ) {         return this.userService.updateById(id, {             name,             address,             description,             imageUrl,             dob: dob ? new Date(dob) : null         } as TUserUpdate);     }      @Mutation(returns => Boolean)     async deleteUser(@Args('id', {type: () => Int}) id: number) {         return this.userService.deleteById(id);     }  }<\/code><\/pre>\n<p>\u0412 \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e GraphQL. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0438 \u044d\u0442\u043e \u0432\u0441\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043e <a href=\"https:\/\/docs.nestjs.com\/graphql\/quick-start\" rel=\"noopener noreferrer nofollow\">GraphQL \u0434\u043b\u044f NestJS<\/a>, \u043d\u043e \u0435\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. <\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e GraphQL Playground \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043f\u0443\u0442\u0438 <code>\/graphql<\/code>. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 Lambda \u0447\u0435\u0440\u0435\u0437 ApiGateway, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c stage \u0441 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u0438\u043c\u0435\u043d\u0435\u043c, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u043f\u0443\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \/api. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0443\u0442\u044c \u0434\u043b\u044f GraphQL Playground \u0441 <code>\/graphql<\/code>  \u0432 <code>\/api\/graphql<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>useGlobalPrefix:true<\/code>. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 express \u0434\u043e\u0431\u0430\u0432\u0438\u043c <code>app.setGlobalPrefix('api');<\/code><\/p>\n<pre><code class=\"javascript\">@Module({     imports: [         DatabaseModule,         GraphQLModule.forRootAsync({             useFactory: () => {                 const schemaModuleOptions: Partial&lt;GqlModuleOptions> = {};                  \/\/ If we are in development, we want to generate the schema.graphql                 if (process.env.NODE_ENV !== 'production' || process.env.IS_OFFLINE) {                     schemaModuleOptions.autoSchemaFile = 'src\/user\/user.schema.gql';                 } else {                     \/\/ For production, the file should be generated                     schemaModuleOptions.typePaths = ['*.gql'];                 }                  return {                     context: ({req}) => ({req}),                     useGlobalPrefix:true, \/\/ &lt;==                     playground: true, \/\/ Allow playground in production                     introspection: true, \/\/ Allow introspection in production                     ...schemaModuleOptions,                 };             }         } as GqlModuleAsyncOptions),     ],     providers: [         ...userProviders,         UserService,         UsersResolver     ] })<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c Playground \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<figure class=\"full-width\"><figcaption>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/figcaption><\/figure>\n<figure class=\"full-width\"><figcaption>\u041f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432 Lambda \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 <code>express<\/code> \u043d\u0430 <code>aws-serverless-express<\/code><\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c app.ts:<\/p>\n<pre><code class=\"javascript\">import {NestFactory} from '@nestjs\/core'; import {ExpressAdapter} from '@nestjs\/platform-express'; import {INestApplication} from '@nestjs\/common'; import {AppModule} from '.\/app.module'; import * as express from 'express'; import {Express} from 'express'; import {Server} from \"http\"; import {createServer} from 'aws-serverless-express';  export async function createApp(     expressApp: Express, ): Promise&lt;INestApplication> {     const app = await NestFactory.create(         AppModule,         new ExpressAdapter(expressApp),     );     app.setGlobalPrefix('api');     return app; }  export async function bootstrap(): Promise&lt;Server> {     const expressApp = express();     const app = await createApp(expressApp);     await app.init();     return createServer(expressApp); }<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u0444\u0430\u0439\u043b \u0441 handler \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043b\u044f\u043c\u0431\u0434\u044b:<\/p>\n<pre><code class=\"javascript\">import {Server} from 'http'; import {Context} from 'aws-lambda'; import {proxy, Response} from 'aws-serverless-express'; import {bootstrap} from '.\/app';  let cachedServer: Server;  export async function handler(event: any, context: Context): Promise&lt;Response> {     if (!cachedServer) {         cachedServer = await bootstrap();     }     return proxy(cachedServer, event, context, 'PROMISE').promise; } <\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u0434\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u0432 AWS. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/learn.hashicorp.com\/tutorials\/terraform\/install-cli?in=terraform\/aws-get-started\" rel=\"noopener noreferrer nofollow\">Terraform<\/a>. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043f\u043a\u0443 terraform, \u0430 \u0432 \u043d\u0435\u0439 \u0444\u0430\u0439\u043b <code>main.tf<\/code> . \u0414\u0430\u043b\u044c\u0448\u0435 \u043a\u0438\u0434\u0430\u044e \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433 \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044e:<\/p>\n<pre><code class=\"yaml\"># \u0417\u0430\u0434\u0430\u0434\u0438\u043c \u0440\u0435\u0433\u0438\u043e\u043d \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e provider \"aws\" {   region = \"us-east-1\" }  # \u0414\u0435\u043f\u043b\u043e\u0438\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u0443 \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 zip \u0430\u0440\u0445\u0438\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u0434 \u0432 \u0430\u0440\u0445\u0438\u0432 data \"archive_file\" \"app_zip\" {   type        = \"zip\"   source_dir  = \"..\/app\/dist\"   output_path = \".\/app.zip\" }  # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c API GW resource \"aws_apigatewayv2_api\" \"app\" {   name          = \"api\"   protocol_type = \"HTTP\" }  # \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e stage.  resource \"aws_apigatewayv2_stage\" \"app\" {   api_id = aws_apigatewayv2_api.app.id    name        = \"api\"   auto_deploy = true    # \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f API GW \u0432 CloudWatch   access_log_settings {     destination_arn = aws_cloudwatch_log_group.api_gw.arn      format = jsonencode({       requestId               = \"$context.requestId\"       sourceIp                = \"$context.identity.sourceIp\"       requestTime             = \"$context.requestTime\"       protocol                = \"$context.protocol\"       httpMethod              = \"$context.httpMethod\"       resourcePath            = \"$context.resourcePath\"       routeKey                = \"$context.routeKey\"       status                  = \"$context.status\"       responseLength          = \"$context.responseLength\"       integrationErrorMessage = \"$context.integrationErrorMessage\"     }     )   } }  # \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e Lambda \u0432 API GW resource \"aws_apigatewayv2_integration\" \"app\" {   api_id = aws_apigatewayv2_api.app.id    integration_uri    =<\/code><\/pre>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326798","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326798"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326798\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}