Skip to content

Commit dc5ac44

Browse files
authored
Fix: many to many auth (#748)
* fix: many-to-many relationship with auth directive * chore: formatting
1 parent 98b4243 commit dc5ac44

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,51 @@ describe('AppSyncModelVisitor', () => {
985985
});
986986
});
987987

988+
describe('manyToMany with @auth directive', () => {
989+
it('should correctly process @auth directive in manyToMany relation', () => {
990+
const schema = /* GraphQL */ `
991+
type Post @model @auth(rules: [
992+
{ allow: groups, groups: ["Admins"] },
993+
{ allow: public, operations: [read] },
994+
{ allow: private, operations: [create] }
995+
]){
996+
slug: ID! @primaryKey
997+
title: String!
998+
content: String!
999+
tags: [Tag] @manyToMany(relationName: "PostTag")
1000+
}
1001+
1002+
type Tag @model @auth(rules: [
1003+
{ allow: groups, groups: ["Admins"] },
1004+
{ allow: public, operations: [read] },
1005+
{ allow: private, operations: [create] }
1006+
]){
1007+
name: ID! @primaryKey
1008+
posts: [Post] @manyToMany(relationName: "PostTag")
1009+
}
1010+
`;
1011+
1012+
const { models } = createAndGenerateVisitor(schema, { usePipelinedTransformer: true, respectPrimaryKeyAttributesOnConnectionField: true, transformerVersion: 2 });
1013+
1014+
const jointTableModel = models.PostTag;
1015+
const authDirective = jointTableModel.directives.find(d => d.name === 'auth');
1016+
1017+
expect(authDirective).toBeDefined();
1018+
expect(authDirective!.arguments.rules).toEqual([
1019+
{
1020+
allow: 'groups',
1021+
groups: ['Admins'],
1022+
operations: ['create', 'update', 'delete', 'read'],
1023+
groupClaim: 'cognito:groups',
1024+
groupField: undefined,
1025+
provider: 'userPools',
1026+
},
1027+
{ allow: 'public', operations: ['read'] },
1028+
{ allow: 'private', operations: ['create'] },
1029+
]);
1030+
});
1031+
});
1032+
9881033
describe('Primary Key Type', () => {
9891034
describe('V1 GraphQL schema tests', () => {
9901035
const schemaV1 = /* GraphQL */ `

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,20 @@ export class AppSyncModelVisitor<
965965
value[1].model,
966966
graphqlName(toUpper(value[0].directive.arguments.relationName)),
967967
);
968+
969+
const extractedAuthDirectives = [...value[0].model.directives, ...value[1].model.directives]
970+
.filter(directive => directive.name === 'auth');
971+
972+
const serializedDirectives = extractedAuthDirectives.map(directive => JSON.stringify(directive));
973+
974+
const uniqueSerializedDirectives = serializedDirectives.filter((serializedDirective, index, array) =>
975+
array.indexOf(serializedDirective) === index
976+
);
977+
978+
const authDirectives = uniqueSerializedDirectives.map(serializedDirective => JSON.parse(serializedDirective));
979+
980+
intermediateModel.directives = [...intermediateModel.directives, ...authDirectives];
981+
968982
const modelDirective = intermediateModel.directives.find(directive => directive.name === 'model');
969983
if (modelDirective) {
970984
// Maps @primaryKey and @index of intermediate model to old @key

0 commit comments

Comments
 (0)