Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add schema directives for sync operation when conflict resolution is enabled #8521

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test multi auth model with conflict resolution 1`] = `
"## [Start] Authorization Steps. **
$util.qr($ctx.stash.put(\\"hasAuth\\", true))
#set( $isAuthorized = false )
#set( $primaryFieldMap = {} )
#if( $util.authType() == \\"IAM Authorization\\" )
#if( !$isAuthorized )
#if( $ctx.identity.userArn == $ctx.stash.authRole )
#set( $isAuthorized = true )
#end
#end
#end
#if( $util.authType() == \\"User Pool Authorization\\" )
#if( !$isAuthorized )
#set( $authFilter = [{
\\"owner\\": {
\\"eq\\": $util.defaultIfNull($ctx.identity.claims.get(\\"username\\"), $util.defaultIfNull($ctx.identity.claims.get(\\"cognito:username\\"), \\"___xamznone____\\"))
}
}] )
$util.qr($ctx.stash.put(\\"authFilter\\", { \\"or\\": $authFilter }))
#end
#end
#if( !$isAuthorized && $util.isNull($ctx.stash.authFilter) && $primaryFieldMap.isEmpty() )
$util.unauthorized()
#end
$util.toJson({\\"version\\":\\"2018-05-29\\",\\"payload\\":{}})
## [End] Authorization Steps. **"
`;

exports[`test single auth model is enabled with conflict resolution 1`] = `
"## [Start] Authorization Steps. **
$util.qr($ctx.stash.put(\\"hasAuth\\", true))
#set( $isAuthorized = false )
#set( $primaryFieldMap = {} )
#if( $util.authType() == \\"User Pool Authorization\\" )
#if( !$isAuthorized )
#set( $authFilter = [{
\\"owner\\": {
\\"eq\\": $util.defaultIfNull($ctx.identity.claims.get(\\"username\\"), $util.defaultIfNull($ctx.identity.claims.get(\\"cognito:username\\"), \\"___xamznone____\\"))
}
}] )
$util.qr($ctx.stash.put(\\"authFilter\\", { \\"or\\": $authFilter }))
#end
#end
#if( !$isAuthorized && $util.isNull($ctx.stash.authFilter) && $primaryFieldMap.isEmpty() )
$util.unauthorized()
#end
$util.toJson({\\"version\\":\\"2018-05-29\\",\\"payload\\":{}})
## [End] Authorization Steps. **"
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import _ from 'lodash';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform, ConflictHandlerType } from '@aws-amplify/graphql-transformer-core';
import _ from 'lodash';

test('test single auth model is enabled with conflict resolution', () => {
const validSchema = `
type Post @model @auth(rules: [{ allow: owner}]) {
id: ID!
title: String!
createdAt: String
updatedAt: String
}`;
const transformer = new GraphQLTransform({
authConfig: {
defaultAuthentication: {
authenticationType: 'AMAZON_COGNITO_USER_POOLS',
},
additionalAuthenticationProviders: [],
},
transformConfig: {
ResolverConfig: {
project: {
ConflictDetection: 'VERSION',
ConflictHandler: ConflictHandlerType.AUTOMERGE,
},
},
},
transformers: [new ModelTransformer(), new AuthTransformer()],
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();
expect(out.schema).toContain(
`syncPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String, lastSync: AWSTimestamp): ModelPostConnection`,
);
expect(out.pipelineFunctions['Query.syncPosts.auth.1.req.vtl']).toMatchSnapshot();
});

test('test multi auth model with conflict resolution', () => {
const validSchema = `
type Post @model @auth(rules: [{ allow: owner }, { allow: private, provider: iam }]) {
id: ID!
title: String!
createdAt: String
updatedAt: String
}`;
const transformer = new GraphQLTransform({
authConfig: {
defaultAuthentication: {
authenticationType: 'AMAZON_COGNITO_USER_POOLS',
},
additionalAuthenticationProviders: [{ authenticationType: 'AWS_IAM' }],
},
transformConfig: {
ResolverConfig: {
project: {
ConflictDetection: 'VERSION',
ConflictHandler: ConflictHandlerType.AUTOMERGE,
},
},
},
transformers: [new ModelTransformer(), new AuthTransformer()],
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();
expect(out.schema).toContain(
`syncPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String, lastSync: AWSTimestamp): ModelPostConnection @aws_iam @aws_cognito_user_pools`,
);
expect(out.pipelineFunctions['Query.syncPosts.auth.1.req.vtl']).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { ResourceConstants } from 'graphql-transformer-common';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { ResourceConstants } from 'graphql-transformer-common';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { AppSyncAuthConfiguration, AppSyncAuthConfigurationOIDCEntry, AppSyncAuthMode } from '@aws-amplify/graphql-transformer-interfaces';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from 'graphql';
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { ResourceConstants } from 'graphql-transformer-common';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthTransformer } from '@aws-amplify/graphql-auth-transformer';
import { AuthTransformer } from '../graphql-auth-transformer';
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { SearchableModelTransformer } from '@aws-amplify/graphql-searchable-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
private authPolicyResources = new Set<string>();
private unauthPolicyResources = new Set<string>();

constructor(config: AuthTransformerConfig) {
constructor(config: AuthTransformerConfig = { addAwsIamAuthInOutputSchema: false }) {
super('amplify-auth-transformer', authDirectiveDefinition);
this.config = config;
this.modelDirectiveConfig = new Map();
Expand Down Expand Up @@ -370,9 +370,10 @@ Static group authorization should perform as expected.`,
this.addOperationToResourceReferences(typeName, operationName, acm.getRoles());
}
};
// default model operations TODO: protect sync queries once supported
// default model operations
addServiceDirective(ctx.output.getQueryTypeName()!, 'read', modelConfig?.queries?.get);
addServiceDirective(ctx.output.getQueryTypeName()!, 'read', modelConfig?.queries?.list);
addServiceDirective(ctx.output.getQueryTypeName()!, 'read', modelConfig?.queries?.sync);
addServiceDirective(ctx.output.getMutationTypeName()!, 'create', modelConfig?.mutations?.create);
addServiceDirective(ctx.output.getMutationTypeName()!, 'update', modelConfig?.mutations?.update);
addServiceDirective(ctx.output.getMutationTypeName()!, 'delete', modelConfig?.mutations?.delete);
Expand Down