Skip to content

Commit

Permalink
Fix: many to many auth (#748)
Browse files Browse the repository at this point in the history
* fix: many-to-many relationship with auth directive

* chore: formatting
  • Loading branch information
olliethedev authored Jan 3, 2024
1 parent 98b4243 commit dc5ac44
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ `
Expand Down
14 changes: 14 additions & 0 deletions packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc5ac44

Please sign in to comment.