Skip to content

Commit 5b7f4c3

Browse files
authored
fix: remove aws_iam directive incorrectly added to interfaces (#3283)
1 parent 677fc00 commit 5b7f4c3

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

packages/amplify-graphql-auth-transformer/src/__tests__/iam-custom-operations.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,45 @@ describe('Custom operations have @aws_iam directives when enableIamAuthorization
514514
expect(out.schema).toMatch(/type EventInvocationResponse.*@aws_iam/);
515515
});
516516

517+
test('Does not add @aws_iam to interfaces', () => {
518+
const strategy = makeStrategy(strategyType);
519+
const schema = /* GraphQL */ `
520+
interface FooInterface {
521+
id: ID!
522+
}
523+
type Foo {
524+
description: String
525+
}
526+
type EventInvocationResponse @aws_api_key {
527+
success: Boolean!
528+
}
529+
type Query {
530+
getFooCustom: Foo
531+
}
532+
type Mutation {
533+
updateFooCustom: Foo
534+
doSomethingAsync(body: String!): EventInvocationResponse
535+
@function(name: "FnDoSomethingAsync", invocationType: Event)
536+
@auth(rules: [{ allow: public, provider: apiKey }])
537+
}
538+
type Subscription {
539+
onUpdateFooCustom: Foo @aws_subscribe(mutations: ["updateFooCustom"])
540+
}
541+
`;
542+
543+
const out = testTransform({
544+
schema,
545+
dataSourceStrategies: constructDataSourceStrategies(schema, strategy),
546+
authConfig: makeAuthConfig(),
547+
synthParameters: makeSynthParameters(),
548+
transformers: makeTransformers(),
549+
sqlDirectiveDataSourceStrategies: makeSqlDirectiveDataSourceStrategies(schema, strategy),
550+
});
551+
552+
// Also expect the custom type referenced by the custom operation to be authorized
553+
expect(out.schema).not.toMatch(/interface FooInterface.*@aws_iam/);
554+
});
555+
517556
test('Does not add duplicate @aws_iam directive to custom type if already present', () => {
518557
const strategy = makeStrategy(strategyType);
519558
const schema = /* GraphQL */ `

packages/amplify-graphql-auth-transformer/src/graphql-auth-transformer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,15 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
394394
return !type.directives?.some((dir) => dir.name.value === 'model');
395395
};
396396

397+
const isNotInterface = (type: TypeDefinitionNode): boolean => {
398+
return type.kind !== Kind.INTERFACE_TYPE_DEFINITION;
399+
};
400+
397401
ctx.inputDocument.definitions
398402
.filter(isObjectTypeDefinitionNode)
399403
.filter(isNonModelType)
400404
.filter(needsAwsIamDirective)
405+
.filter(isNotInterface)
401406
.forEach((def) => extendTypeWithDirectives(ctx, def.name.value, [makeDirective('aws_iam', [])]));
402407
};
403408

0 commit comments

Comments
 (0)