Skip to content

Commit

Permalink
fix(appsync): addSubscription only allows for field type (#16097)
Browse files Browse the repository at this point in the history
Error in the documentation and type checking. Fixed both the readme and the related PR (integ test used a `Field` type so it still works as intended).

Related PR: #10078

Fixes: #16071

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
BryanPan342 authored Aug 18, 2021
1 parent 1e49857 commit 000d151
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ To add fields for these subscriptions, we can simply run the `addSubscription` f
to the schema's `Subscription` type.
```ts
api.addSubscription('addedFilm', new appsync.ResolvableField({
api.addSubscription('addedFilm', new appsync.Field({
returnType: film.attribute(),
args: { id: appsync.GraphqlType.id({ isRequired: true }) },
directive: [appsync.Directive.subscribe('addFilm')],
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-appsync/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnGraphQLSchema } from './appsync.generated';
import { GraphqlApi } from './graphqlapi';
import { SchemaMode, shapeAddition } from './private';
import { IIntermediateType } from './schema-base';
import { ResolvableField } from './schema-field';
import { Field, ResolvableField } from './schema-field';
import { ObjectType } from './schema-intermediate';

/**
Expand Down Expand Up @@ -164,7 +164,7 @@ export class Schema {
* @param fieldName the name of the Subscription
* @param field the resolvable field to for this Subscription
*/
public addSubscription(fieldName: string, field: ResolvableField): ObjectType {
public addSubscription(fieldName: string, field: Field): ObjectType {
if (this.mode !== SchemaMode.CODE) {
throw new Error(`Unable to add subscription. Schema definition mode must be ${SchemaMode.CODE}. Received: ${this.mode}`);
}
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-code-first.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ describe('code-first implementation through GraphQL Api functions`', () => {
Definition: `${out}`,
});
});

test('addSubscription allows for adding fields but not resolvable fields', () => {
const ds = api.addNoneDataSource('DS');

// WHEN
api.addMutation('addId', new appsync.ResolvableField({
returnType: t.required_id,
args: { id: t.required_id },
dataSource: ds,
}));
api.addSubscription('addedId', new appsync.Field({
returnType: t.required_id,
args: { id: t.required_id },
directives: [appsync.Directive.subscribe('addId')],
}));

const schemaDef = 'schema {\n mutation: Mutation\n subscription: Subscription\n}\n';
const mutationDef = 'type Mutation {\n addId(id: ID!): ID!\n}\n';
const subscriptionDef = 'type Subscription {\n addedId(id: ID!): ID!\n @aws_subscribe(mutations: ["addId"])\n}\n';

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::GraphQLSchema', {
Definition: `${schemaDef}${mutationDef}${subscriptionDef}`,
});
});
});

describe('code-first implementation through Schema functions`', () => {
Expand Down

0 comments on commit 000d151

Please sign in to comment.