For CDK versions > 1.63.0 see cdk-appsync-transformer. This follows the community accepted construct naming scheme and the project now uses projen.
In April 2020 I wrote a blog post on using the AWS Cloud Development Kit with AppSync. I wrote my own transformer in order to emulate AWS Amplify's method of using GraphQL directives in order to template a lot of the Schema Definition Language.
This package is my attempt to convert all of that effort into a separate construct in order to clean up the process.
API With Default Values
import { AppSyncTransformer } from 'aws-cdk-appsync-transformer';
...
new AppSyncTransformer(this, "my-cool-api", {
schemaPath: 'schema.graphql'
});
schema.graphql
type Customer @model
@auth(rules: [
{ allow: groups, groups: ["Admins"] },
{ allow: private, provider: iam, operations: [read, update] }
]) {
id: ID!
firstName: String!
lastName: String!
active: Boolean!
address: String!
}
type Product @model
@auth(rules: [
{ allow: groups, groups: ["Admins"] },
{ allow: public, provider: iam, operations: [read] }
]) {
id: ID!
name: String!
description: String!
price: String!
active: Boolean!
added: AWSDateTime!
orders: [Order] @connection
}
type Order @model
@key(fields: ["id", "productID"]) {
id: ID!
productID: ID!
total: String!
ordered: AWSDateTime!
}
Tested:
Experimental:
- @key
- @versioned
- @function
- These work differently here than they do in Amplify - see Functions below
Not Yet Supported:
User Pool Authentication
const userPool = new UserPool(this, 'my-cool-user-pool', {
...
})
...
const userPoolClient = new UserPoolClient(this, `${id}-client`, {
userPool: this.userPool,
...
})
...
new AppSyncTransformer(this, "my-cool-api", {
schemaPath: 'schema.graphql',
authorizationConfig: {
defaultAuthorization: {
authorizationType: AuthorizationType.USER_POOL,
userPoolConfig: {
userPool: userPool,
appIdClientRegex: userPoolClient.userPoolClientId,
defaultAction: UserPoolDefaultAction.ALLOW
}
}
}
});
Unauth Role: TODO
Auth Role: Unsupported (for now?). Authorized roles (Lambda Functions, EC2 roles, etc) are required to setup their own role permissions.
Fields with the @function
directive will be accessible via api.outputs.FUNCTION_RESOLVERS
. It will return an array like below.Currently these are not named and do not specify a region. There are improvements that can be made here but this simple way has worked for me so I've implemented it first. Typically I send all @function
requests to one Lambda Function and have it route as necessary.
[
{ typeName: 'Query', fieldName: 'listUsers' },
{ typeName: 'Query', fieldName: 'getUser' },
{ typeName: 'Mutation', fieldName: 'createUser' },
{ typeName: 'Mutation', fieldName: 'updateUser' }
]
- Pass
syncEnabled: true
to theAppSyncTransformerProps
- Generate necessary exports (see Code Generation below)
I've written some helpers to generate code similarly to how AWS Amplify generates statements and types. You can find the code here.
I will attempt to align the major and minor version of this package with AWS CDK, but always check the release descriptions for compatibility.
See CONTRIBUTING for details
Distributed under Apache License, Version 2.0