import { Construct } from 'constructs' ; import * as apig from "@aws-cdk/aws-apigatewayv2-alpha" ; import { Api , ApiProps , ApiFunctionRouteProps , ApiPayloadFormatVersion , } from "./Api" ; import { Function as Fn , FunctionDefinition } from "./Function" ; export interface ApolloApiProps extends Omit < ApiProps , "routes" > { readonly server : FunctionDefinition ; readonly rootPath ? : string ; } export class ApolloApi extends Api { private lambdaIntegration ? : apig . HttpRouteIntegration ; private rootPath ? : string ; constructor ( scope , id , props ) { const { server , rootPath = "/" , defaultPayloadFormatVersion , ... restProps } = props || { } ; if ( ! server ) { throw new Error ( `Missing "server" in the "${ id }" ApolloApi` ) ; } const { routes } = props as ApiProps ; if ( routes ) { throw new Error ( `Please use the "server" option instead of the "routes" to configure the handler for the "${ id }" ApolloApi` ) ; } super ( scope , id , { ... restProps , defaultPayloadFormatVersion : defaultPayloadFormatVersion || ApiPayloadFormatVersion . V1 , routes : { [ `GET ${ rootPath }` ] : server , [ `POST ${ rootPath }` ] : server , } , } ) ; this . rootPath = rootPath ; } public get serverFunction ( ) : Fn { const serverFn = this . getFunction ( `GET ${ this . rootPath }` ) ; if ( ! serverFn ) { throw new Error ( `Failed to get "serverFunction" in the "${ this . node . id }" ApolloApi` ) ; } return serverFn ; } protected createFunctionIntegration ( scope , routeKey , routeProps , postfixName ) { if ( ! this . lambdaIntegration ) { this . lambdaIntegration = super . createFunctionIntegration ( scope , routeKey , routeProps , postfixName ) ; } return this . lambdaIntegration ; } }