GraphqlApi
Source:
src/AWS/AppSync/GraphqlApi.ts
An AWS AppSync GraphQL API.
Owns the API, its SDL schema (applied via startSchemaCreation and
awaited until active), its authentication modes, and an optional
server-side cache. Pair with DataSource, Resolver,
and ApiKey to serve GraphQL over Lambda or DynamoDB.
Creating a GraphQL API
Section titled “Creating a GraphQL API”import * as AppSync from "alchemy/AWS/AppSync";
const api = yield* AppSync.GraphqlApi("Api", { schema: ` type Query { hello: String! } schema { query: Query } `,});const key = yield* AppSync.ApiKey("Key", { api });Authentication Modes
Section titled “Authentication Modes”Lambda authorizer
const api = yield* AppSync.GraphqlApi("Api", { authenticationType: "AWS_LAMBDA", lambdaAuthorizerConfig: { authorizerUri: authorizer.functionArn }, schema,});// AppSync must be allowed to invoke the authorizer:yield* AWS.Lambda.Permission("AppSyncInvoke", { functionName: authorizer.functionName, principal: "appsync.amazonaws.com", action: "lambda:InvokeFunction", sourceArn: api.apiArn,});Cognito user pools as an additional auth mode
const api = yield* AppSync.GraphqlApi("Api", { authenticationType: "API_KEY", additionalAuthenticationProviders: [ { authenticationType: "AMAZON_COGNITO_USER_POOLS", userPoolConfig: { userPoolId: pool.userPoolId }, }, ], schema,});Caching
Section titled “Caching”const api = yield* AppSync.GraphqlApi("Api", { schema, cache: { type: "SMALL", behavior: "FULL_REQUEST_CACHING", ttl: "60 seconds" },});Environment Variables
Section titled “Environment Variables”const api = yield* AppSync.GraphqlApi("Api", { schema, environmentVariables: { STAGE: "prod" },});// in APPSYNC_JS resolver code:// export function response(ctx) { return ctx.env.STAGE; }