# aws-cdk-appsync-transformer

> AWS Amplify inspired CDK construct for creating @directive based AppSync APIs

Latest version **1.63.0-rc.3** (published 2020-10-23) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install aws-cdk-appsync-transformer
pnpm add aws-cdk-appsync-transformer
yarn add aws-cdk-appsync-transformer
bun add aws-cdk-appsync-transformer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; large bundle.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.63.0-rc.3 |
| Published | 2020-10-23 |
| First published | 2020-07-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 19 |
| Unpacked size | 75.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Ken Winner |
| Maintainers | kwinner |
| Keywords | aws, cdk, aws-cdk, appsync, amplify, transformer |

## Links

- npm: https://www.npmjs.com/package/aws-cdk-appsync-transformer
- Repository: https://github.com/kcwinner/appsync-transformer-construct
- Homepage: https://github.com/kcwinner/appsync-transformer-construct#readme
- Issues: https://github.com/kcwinner/appsync-transformer-construct/issues
- npm.io page: https://npm.io/package/aws-cdk-appsync-transformer

## Dependencies (19)

- [graphql](https://npm.io/package/graphql.md) ^14.6.0
- [@aws-cdk/core](https://npm.io/package/@aws-cdk/core.md) 1.63.0
- [@types/graphql](https://npm.io/package/@types/graphql.md) ^14.5.0
- [cloudform-types](https://npm.io/package/cloudform-types.md) ^5.0.0
- [@aws-cdk/aws-iam](https://npm.io/package/@aws-cdk/aws-iam.md) 1.63.0
- [@aws-cdk/aws-lambda](https://npm.io/package/@aws-cdk/aws-lambda.md) 1.63.0
- [@aws-cdk/aws-appsync](https://npm.io/package/@aws-cdk/aws-appsync.md) 1.63.0
- [@aws-cdk/aws-cognito](https://npm.io/package/@aws-cdk/aws-cognito.md) 1.63.0
- [@aws-cdk/aws-dynamodb](https://npm.io/package/@aws-cdk/aws-dynamodb.md) 1.63.0
- [graphql-key-transformer](https://npm.io/package/graphql-key-transformer.md) ^2.19.1
- [graphql-auth-transformer](https://npm.io/package/graphql-auth-transformer.md) ^6.18.1
- [graphql-mapping-template](https://npm.io/package/graphql-mapping-template.md) ^4.13.4
- [graphql-transformer-core](https://npm.io/package/graphql-transformer-core.md) ^6.19.1
- [graphql-transformer-common](https://npm.io/package/graphql-transformer-common.md) ^4.17.1
- [graphql-dynamodb-transformer](https://npm.io/package/graphql-dynamodb-transformer.md) ^6.19.2
- [graphql-function-transformer](https://npm.io/package/graphql-function-transformer.md) ^2.3.9
- [graphql-versioned-transformer](https://npm.io/package/graphql-versioned-transformer.md) ^4.15.9
- [graphql-connection-transformer](https://npm.io/package/graphql-connection-transformer.md) ^4.18.1
- [graphql-relational-schema-transformer](https://npm.io/package/graphql-relational-schema-transformer.md) ^2.15.6

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.63.0-rc.3 (latest) — 2020-10-23
- 1.63.0-rc.2 — 2020-09-29
- 1.63.0-rc.1 — 2020-09-24
- 1.50.0-rc.1 — 2020-08-30
- 1.50.0-alpha — 2020-07-07
- 1.49.1-alpha — 2020-07-07

## README

# AppSync Transformer Construct for AWS CDK

![build](https://github.com/kcwinner/aws-cdk-appsync-transformer/workflows/build/badge.svg)
[![codecov](https://codecov.io/gh/kcwinner/aws-cdk-appsync-transformer/branch/main/graph/badge.svg)](https://codecov.io/gh/kcwinner/aws-cdk-appsync-transformer)
[![dependencies Status](https://david-dm.org/kcwinner/aws-cdk-appsync-transformer/status.svg)](https://david-dm.org/kcwinner/aws-cdk-appsync-transformer)
[![npm](https://img.shields.io/npm/dt/aws-cdk-appsync-transformer)](https://www.npmjs.com/package/aws-cdk-appsync-transformer)

[![npm version](https://badge.fury.io/js/aws-cdk-appsync-transformer.svg)](https://badge.fury.io/js/aws-cdk-appsync-transformer)
[![PyPI version](https://badge.fury.io/py/aws-cdk-appsync-transformer.svg)](https://badge.fury.io/py/aws-cdk-appsync-transformer)
<!-- [![NuGet version](https://badge.fury.io/nu/Kcwinner.AWSCDKAppSyncTransformer.svg)](https://badge.fury.io/nu/Kcwinner.AWSCDKAppSyncTransformer)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.kcwinner/AWSCDKAppSyncTransformer?color=brightgreen)](https://repo1.maven.org/maven2/io/github/kcwinner/AWSCDKAppSyncTransformer/) -->

## Why This Package

In April 2020 I wrote a [blog post](https://www.trek10.com/blog/appsync-with-the-aws-cloud-development-kit) 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. 

## How Do I Use It

### Example Usage

API With Default Values
```ts
import { AppSyncTransformer } from 'aws-cdk-appsync-transformer';
...
new AppSyncTransformer(this, "my-cool-api", {
    schemaPath: 'schema.graphql'
});
```

schema.graphql
```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!
}
```

### [Supported Amplify Directives](https://docs.amplify.aws/cli/graphql-transformer/directives)

Tested:
* [@model](https://docs.amplify.aws/cli/graphql-transformer/directives#model)
* [@auth](https://docs.amplify.aws/cli/graphql-transformer/directives#auth)
* [@connection](https://docs.amplify.aws/cli/graphql-transformer/directives#connection)

Experimental:
* [@key](https://docs.amplify.aws/cli/graphql-transformer/directives#key)
* [@versioned](https://docs.amplify.aws/cli/graphql-transformer/directives#versioned)
* [@function](https://docs.amplify.aws/cli/graphql-transformer/directives#function)
  * These work differently here than they do in Amplify - see [Functions](#functions) below

Not Yet Supported:
* [@searchable](https://docs.amplify.aws/cli/graphql-transformer/directives#searchable)
* [@predictions](https://docs.amplify.aws/cli/graphql-transformer/directives#predictions)
* [@http](https://docs.amplify.aws/cli/graphql-transformer/directives#http)

### Authentication

User Pool Authentication
```ts
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
            }
        }
    }
});
```

#### IAM 

Unauth Role: TODO

Auth Role: Unsupported (for now?). Authorized roles (Lambda Functions, EC2 roles, etc) are required to setup their own role permissions.

### Functions

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.

```js
[
  { typeName: 'Query', fieldName: 'listUsers' },
  { typeName: 'Query', fieldName: 'getUser' },
  { typeName: 'Mutation', fieldName: 'createUser' },
  { typeName: 'Mutation', fieldName: 'updateUser' }
]
```

### DataStore Support

1. Pass `syncEnabled: true` to the `AppSyncTransformerProps`
1. Generate necessary exports (see [Code Generation](#code-generation) below)

### Code Generation

I've written some helpers to generate code similarly to how AWS Amplify generates statements and types. You can find the code [here](https://github.com/kcwinner/advocacy/tree/master/cdk-amplify-appsync-helpers).

## Versioning

I will *attempt* to align the major and minor version of this package with [AWS CDK], but always check the release descriptions for compatibility.

I currently support [![GitHub package.json dependency version (prod)](https://img.shields.io/github/package-json/dependency-version/kcwinner/aws-cdk-appsync-transformer/@aws-cdk/core)](https://github.com/aws/aws-cdk)

## Limitations

* 

## Contributing

See [CONTRIBUTING](CONTRIBUTING.md) for details

## License

Distributed under [Apache License, Version 2.0](LICENSE)

[aws cdk]: https://aws.amazon.com/cdk

---
_Source: https://npm.io/package/aws-cdk-appsync-transformer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
