Skip to content

Commit

Permalink
fix: fix API and doc (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure authored Feb 19, 2022
1 parent 515caee commit 6ff7690
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 131 deletions.
4 changes: 2 additions & 2 deletions doc/dataSources.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ appSync:
- `versioned`: Set to `true` to use Conflict Detection and Resolution with this data source.
- `deltaSyncConfig`:
- `deltaSyncTableName`: The Delta Sync table name.
- `baseTableTTL`: The number of minutes that an Item is stored in the data source.
- `deltaSyncTableTTL`: The number of minutes that a Delta Sync log entry is stored in the Delta Sync table.
- `baseTableTTL`: The number of minutes that an Item is stored in the data source. Defaults to `43200`
- `deltaSyncTableTTL`: The number of minutes that a Delta Sync log entry is stored in the Delta Sync table. Defaults to `1440`

## AWS Lambda

Expand Down
2 changes: 1 addition & 1 deletion doc/general-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ appSync:
- `name`: The name of this AppSync API
- `schema`: The filename of the schema file. Defaults to `schema.graphql`. [Read more](#Schema)
- `authentication`: See [Authentication](authentication.md)
- `additionalAuthenticationProviders`: See [Authentication](authentication.md)
- `additionalAuthentications`: See [Authentication](authentication.md)
- `apiKeys`: See [API Keys](API-keys.md)
- `domain`: See [Custom domains](custom-domain.md)
- `dataSources`: See [DataSources](dataSources.md)
Expand Down
1 change: 1 addition & 0 deletions doc/pipeline-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ appSync:
- `response`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{functionName}.response.vtl`.
- `maxBatchSize`: The maximum [batch size](https://aws.amazon.com/blogs/mobile/introducing-configurable-batching-size-for-aws-appsync-lambda-resolvers/) to use (only available for AWS Lambda DataAources)
- `substitutions`: See [VTL template substitutions](substitutions.md)
- `sync`: [See SyncConfig](syncConfig.md)

## Inline DataSources

Expand Down
24 changes: 1 addition & 23 deletions doc/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ appSync:
- `response`: The request mapping template file name to use for this resolver, or `false` for [direct lambda](https://docs.aws.amazon.com/appsync/latest/devguide/direct-lambda-reference.html). Defaults to `{type}.{field}.response.vtl`.
- `substitutions`: See [VTL template substitutions](substitutions.md)
- `caching`: [See below](#Caching)
- `sync`: [See blow](#Sync)
- `sync`: [See SyncConfig](syncConfig.md)

## PIPELINE resolvers

Expand Down Expand Up @@ -91,28 +91,6 @@ You can also customize each resolver using the following config:
- `ttl`: The TTL of the cache for this resolver in seconds
- `keys`: An array of keys to use for the cache.

## Sync

The [Delta Sync](https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-delta-sync.html) configuration for this resolver.

```yaml
Query.user:
dataSource: my-table
sync:
conflictDetection: 'VERSION'
conflictHandler: 'LAMBDA'
function:
timeout: 30
handler: 'functions/userSync.handler'
```

- `conflictDetection`: Defaults to `VERSION`
- `conflictHandler`: `OPTIMISTIC_CONCURRENCY`, `AUTOMERGE` or `LAMBDA`
- `function`: When `conflictHandler` is `LAMBDA`, a Lambda function definition as you would define it under the `functions` section of your `serverless.yml` file.
- `functionName`: When `conflictHandler` is `LAMBDA`, the name of the function as defined under the `functions` section of the `serverless.yml` file
- `functionAlias`: When `conflictHandler` is `LAMBDA`, a specific function alias to use.
- `functionArn`: When `conflictHandler` is `LAMBDA`, the function ARN to use.

# Organize your resolvers

You can define your data sources into several files for organizational reasons. You can pass each file into the `dataSources` attribute as an array.
Expand Down
23 changes: 23 additions & 0 deletions doc/syncConfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sync Config

[Delta Sync](https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-delta-sync.html) configuration for [resolvers](resolvers.md) and [pipeline functions](pipeline-functions.md).

## Quick start

```yaml
Query.user:
dataSource: my-table
sync:
conflictDetection: 'VERSION'
conflictHandler: 'LAMBDA'
function:
timeout: 30
handler: 'functions/userSync.handler'
```
- `conflictDetection`: `VERSION` or `NONE`. Defaults to `VERSION`
- `conflictHandler`: When `conflictDetection` is `VERSION`, configures how conflict resolution happens. `OPTIMISTIC_CONCURRENCY`, `AUTOMERGE` or `LAMBDA`. Defaults to `OPTIMISTIC_CONCURRENCY`
- `function`: When `conflictHandler` is `LAMBDA`, a Lambda function definition as you would define it under the `functions` section of your `serverless.yml` file.
- `functionName`: When `conflictHandler` is `LAMBDA`, the name of the function as defined under the `functions` section of the `serverless.yml` file
- `functionAlias`: When `conflictHandler` is `LAMBDA`, a specific function alias to use.
- `functionArn`: When `conflictHandler` is `LAMBDA`, the function ARN to use.
28 changes: 26 additions & 2 deletions doc/upgrading-from-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ appSync:
userPoolId: # user pool ID
```

Additional auth provider stay under `additionalAuthenticationProviders` but follow the same stucture as `authentication`.
Additional auth provider are now under `additionalAuthentications`. Items follow the same stucture as `authentication`.

Example:

Expand All @@ -220,7 +220,7 @@ additionalAuthenticationProviders:
becomes

```yaml
additionalAuthenticationProviders:
additionalAuthentications:
- type: AMAZON_COGNITO_USER_POOLS
config:
userPoolId: # user pool ID
Expand Down Expand Up @@ -267,3 +267,27 @@ extend type Query {
getPost(id: ID!): Post
}
```

**ElasticSearch**

As of September 2021, Amazon Elasticsearch Service is Amazon OpenSearch Service. DataSources of type `AMAZON_ELASTICSEARCH` should now use `AMAZON_OPENSEARCH_SERVICE` instead.

Example:

```yaml
dataSources:
- type: AMAZON_ELASTICSEARCH
name: myEndpoint
config:
endpoint: https://abcdefgh.us-east-1.es.amazonaws.com
```

becomes:

```yaml
dataSources:
myEndpoint:
type: AMAZON_OPENSEARCH_SERVICE
config:
endpoint: https://abcdefgh.us-east-1.es.amazonaws.com
```
9 changes: 5 additions & 4 deletions src/__tests__/__snapshots__/dataSources.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Object {
},
"TableName": "data",
"UseCallerCredentials": false,
"Versioned": true,
},
"Name": "dynamo",
"ServiceRoleArn": Object {
Expand Down Expand Up @@ -779,13 +780,13 @@ Object {
],
},
"Description": "OpenSearch resolver",
"ElasticsearchConfig": Object {
"Name": "opensearch",
"OpenSearchServiceConfig": Object {
"AwsRegion": Object {
"Ref": "AWS::Region",
},
"Endpoint": "https://mydomain.us-east-1.es.amazonaws.com",
},
"Name": "opensearch",
"ServiceRoleArn": Object {
"Fn::GetAtt": Array [
"GraphQlDsopensearchRole",
Expand Down Expand Up @@ -869,7 +870,8 @@ Object {
],
},
"Description": "OpenSearch resolver",
"ElasticsearchConfig": Object {
"Name": "opensearch",
"OpenSearchServiceConfig": Object {
"AwsRegion": Object {
"Ref": "AWS::Region",
},
Expand All @@ -888,7 +890,6 @@ Object {
],
},
},
"Name": "opensearch",
"ServiceRoleArn": Object {
"Fn::GetAtt": Array [
"GraphQlDsopensearchRole",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/getAppSyncConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Array [

exports[`returns basic config 1`] = `
Object {
"additionalAuthenticationProviders": Array [],
"additionalAuthentications": Array [],
"apiKeys": Array [],
"authentication": Object {
"type": "API_KEY",
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/__snapshots__/resolvers.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Resolvers Pipeline Function should generate Resources with sync configuration 1`] = `
Object {
"GraphQlFunctionConfigurationmyFunction": Object {
"Properties": Object {
"ApiId": Object {
"Fn::GetAtt": Array [
"GraphQlApi",
"ApiId",
],
},
"DataSourceName": Object {
"Fn::GetAtt": Array [
"GraphQlDsmyLambdaFunction",
"Name",
],
},
"Description": undefined,
"FunctionVersion": "2018-05-29",
"MaxBatchSize": undefined,
"Name": "myFunction",
"RequestMappingTemplate": "Content of path/to/mappingTemplates/myFunction.request.vtl",
"ResponseMappingTemplate": "Content of path/to/mappingTemplates/myFunction.response.vtl",
"SyncConfig": Object {
"ConflictDetection": "VERSION",
"ConflictHandler": "LAMBDA",
"LambdaConflictHandlerConfig": Object {
"LambdaConflictHandlerArn": Object {
"Fn::GetAtt": Array [
"MyFunctionUnderscoreSyncLambdaFunction",
"Arn",
],
},
},
},
},
"Type": "AWS::AppSync::FunctionConfiguration",
},
}
`;

exports[`Resolvers Pipeline Function should generate Resources with sync configuration 2`] = `
Object {
"myFunction_Sync": Object {
"handler": "index.handler",
},
}
`;

exports[`Resolvers Pipeline Resovlers should generate Resources with default mapping templates 1`] = `
Object {
"GraphQlResolverQueryuser": Object {
Expand Down
24 changes: 17 additions & 7 deletions src/__tests__/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Api', () => {
it('should compile the Api Resource with additional auths', () => {
const api = new Api(
given.appSyncConfig({
additionalAuthenticationProviders: [
additionalAuthentications: [
{
type: 'AMAZON_COGNITO_USER_POOLS',
config: {
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('Api', () => {
it('should compile the Api Resource with embedded additional authorizer Lambda function', () => {
const api = new Api(
given.appSyncConfig({
additionalAuthenticationProviders: [
additionalAuthentications: [
{
type: 'AWS_LAMBDA',
config: {
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('Api', () => {
appIdClientRegex: '[a-z]',
},
},
additionalAuthenticationProviders: [
additionalAuthentications: [
{
type: 'AWS_IAM',
},
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('Api', () => {
appIdClientRegex: '[a-z]',
},
},
additionalAuthenticationProviders: [],
additionalAuthentications: [],
}),
plugin,
);
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('Api', () => {
authentication: {
type: 'API_KEY',
},
additionalAuthenticationProviders: [
additionalAuthentications: [
{
type: 'AMAZON_COGNITO_USER_POOLS',
config: {
Expand Down Expand Up @@ -572,7 +572,7 @@ describe('Api', () => {
it('should generate the Lambda Authorizer Resources from additional auth', () => {
const api = new Api(
given.appSyncConfig({
additionalAuthenticationProviders: [
additionalAuthentications: [
{
type: 'AWS_LAMBDA',
config: {
Expand Down Expand Up @@ -603,11 +603,21 @@ describe('Api', () => {
});

describe('Caching', () => {
it('should not generate Resources when disabled', () => {
it('should not generate Resources when not configured', () => {
const api = new Api(given.appSyncConfig({ caching: undefined }), plugin);
expect(api.compileCachingResources()).toEqual({});
});

it('should not generate Resources when disabled', () => {
const api = new Api(
given.appSyncConfig({
caching: { enabled: false, behavior: 'FULL_REQUEST_CACHING' },
}),
plugin,
);
expect(api.compileCachingResources()).toEqual({});
});

it('should generate Resources with defaults', () => {
const api = new Api(
given.appSyncConfig({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/given.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const appSyncConfig = (partial?: Partial<AppSyncConfig>) => {
authentication: {
type: 'API_KEY',
},
additionalAuthenticationProviders: [],
additionalAuthentications: [],
mappingTemplatesLocation: {
resolvers: 'path/to/mappingTemplates',
pipelineFunctions: 'path/to/mappingTemplates',
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ describe('Resolvers', () => {
`);
});

it('should generate Resources with sync configuration', () => {
const api = new Api(
given.appSyncConfig({
dataSources: {
myLambdaFunction: {
name: 'myLambdaFunction',
type: 'AWS_LAMBDA',
config: { functionArn: 'arn:lambda:' },
},
},
}),
plugin,
);
expect(
api.compilePipelineFunctionResource({
dataSource: 'myLambdaFunction',
name: 'myFunction',
sync: {
conflictDetection: 'VERSION',
conflictHandler: 'LAMBDA',
function: {
handler: 'index.handler',
},
},
}),
).toMatchSnapshot();
expect(api.functions).toMatchSnapshot();
});

it('should generate Pipeline Function Resources with maxBatchSize', () => {
const api = new Api(
given.appSyncConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Basic Invalid should validate: Invalid Datasource 1`] = `
"/dataSources/myDynamoSource1/type: must be one of AMAZON_DYNAMODB, AMAZON_ELASTICSEARCH, AMAZON_OPENSEARCH_SERVICE, AWS_LAMBDA, HTTP, NONE, RELATIONAL_DATABASE
"/dataSources/myDynamoSource1/type: must be one of AMAZON_DYNAMODB, AMAZON_OPENSEARCH_SERVICE, AWS_LAMBDA, HTTP, NONE, RELATIONAL_DATABASE
/dataSources: contains invalid data source definitions"
`;

Expand Down
Loading

0 comments on commit 6ff7690

Please sign in to comment.