ApolloApi

The ApolloApi construct is a higher level CDK construct that makes it easy to create an Apollo Server with AWS Lambda. It provides a simple way to define the GraphQL handler route in your API. And allows you to configure the specific Lambda function if necessary. It also allows you to configure authorization, custom domains, etc.

The ApolloApi construct internally extends the Api construct.

Initializer#

new ApolloApi(scope: Construct, id: string, props: ApolloApiProps)

Parameters

Examples#

Using the minimal config#

import { ApolloApi } from "@serverless-stack/resources";
new ApolloApi(this, "Api", {
server: "src/graphql.handler",
});

And here is an example of a simple handler defined in src/graphql.js.

import { ApolloServer, gql } from "apollo-server-lambda";
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: () => "Hello world!",
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true, // Enable GraphQL playground
});
exports.handler = server.createHandler();

Configuring the Lambda function#

You can configure the Lambda function used for the Apollo Server.

new ApolloApi(this, "Api", {
server: {
handler: "src/graphql.handler",
timeout: 10,
memorySize: 512,
},
});

Configuring the API#

You can also configure the API with a custom domain, access log format, CORS settings, and authorization settings. For more detailed examples refer to the Api examples.

Configuring custom domains#

new ApolloApi(this, "Api", {
customDomain: "api.domain.com",
server: "src/graphql.handler",
});

Configuring the access log format#

Use a CSV format instead of default JSON format.

new ApolloApi(this, "Api", {
accessLog:
"$context.identity.sourceIp,$context.requestTime,$context.httpMethod,$context.routeKey,$context.protocol,$context.status,$context.responseLength,$context.requestId",
server: "src/graphql.handler",
});

Configuring CORS#

Override the default behavior of allowing all methods, and only allow the GET method.

import { HttpMethod } from "@aws-cdk/aws-apigatewayv2";
new ApolloApi(this, "Api", {
cors: {
allowMethods: [HttpMethod.GET],
},
server: "src/graphql.handler",
});

Adding auth#

You can secure your APIs (and other AWS resources) by setting the defaultAuthorizationType to AWS_IAM and using the Auth construct.

new ApolloApi(this, "Api", {
defaultAuthorizationType: ApiAuthorizationType.AWS_IAM,
server: "src/graphql.handler",
});

For more examples, refer to the Api examples.

Properties#

Refer to the properties in the Api construct.

Methods#

Refer to the methods in the Api construct.

note

The addRoutes method is not available in ApolloApi.

ApolloApiProps#

Takes the following construct props in addition to the ApiProps.

note

The routes option cannot be set in ApolloApi.

server#

Type : FunctionDefinition

The function definition used to create the function for GraphQL handler.