diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts index 96d84b376..7560769e2 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts @@ -985,6 +985,51 @@ describe('AppSyncModelVisitor', () => { }); }); + describe('manyToMany with @auth directive', () => { + it('should correctly process @auth directive in manyToMany relation', () => { + const schema = /* GraphQL */ ` + type Post @model @auth(rules: [ + { allow: groups, groups: ["Admins"] }, + { allow: public, operations: [read] }, + { allow: private, operations: [create] } + ]){ + slug: ID! @primaryKey + title: String! + content: String! + tags: [Tag] @manyToMany(relationName: "PostTag") + } + + type Tag @model @auth(rules: [ + { allow: groups, groups: ["Admins"] }, + { allow: public, operations: [read] }, + { allow: private, operations: [create] } + ]){ + name: ID! @primaryKey + posts: [Post] @manyToMany(relationName: "PostTag") + } + `; + + const { models } = createAndGenerateVisitor(schema, { usePipelinedTransformer: true, respectPrimaryKeyAttributesOnConnectionField: true, transformerVersion: 2 }); + + const jointTableModel = models.PostTag; + const authDirective = jointTableModel.directives.find(d => d.name === 'auth'); + + expect(authDirective).toBeDefined(); + expect(authDirective!.arguments.rules).toEqual([ + { + allow: 'groups', + groups: ['Admins'], + operations: ['create', 'update', 'delete', 'read'], + groupClaim: 'cognito:groups', + groupField: undefined, + provider: 'userPools', + }, + { allow: 'public', operations: ['read'] }, + { allow: 'private', operations: ['create'] }, + ]); + }); + }); + describe('Primary Key Type', () => { describe('V1 GraphQL schema tests', () => { const schemaV1 = /* GraphQL */ ` diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts index 30bef1bf6..34c2de3f5 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts @@ -965,6 +965,20 @@ export class AppSyncModelVisitor< value[1].model, graphqlName(toUpper(value[0].directive.arguments.relationName)), ); + + const extractedAuthDirectives = [...value[0].model.directives, ...value[1].model.directives] + .filter(directive => directive.name === 'auth'); + + const serializedDirectives = extractedAuthDirectives.map(directive => JSON.stringify(directive)); + + const uniqueSerializedDirectives = serializedDirectives.filter((serializedDirective, index, array) => + array.indexOf(serializedDirective) === index + ); + + const authDirectives = uniqueSerializedDirectives.map(serializedDirective => JSON.parse(serializedDirective)); + + intermediateModel.directives = [...intermediateModel.directives, ...authDirectives]; + const modelDirective = intermediateModel.directives.find(directive => directive.name === 'model'); if (modelDirective) { // Maps @primaryKey and @index of intermediate model to old @key