From f0724d7985ab86f076129d83fb3e3a6b88c479af Mon Sep 17 00:00:00 2001 From: Bryan Pan Date: Sun, 21 Feb 2021 21:19:02 -0800 Subject: [PATCH] fix(appsync): revert to allow resolver creation from data source (#12973) * Revert to old behavior of allowing a data source to create a resolver as UNIT resolvers stem from data sources. * Maintaining new behavior of having `GraphqlApi` create resolvers as PIPELINE resolvers stem from the `GraphqlApi` itself Fixes #12635 Fixes #11522 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/aws-appsync/lib/data-source.ts | 6 +- .../@aws-cdk/aws-appsync/test/appsync.test.ts | 3 +- .../test/integ.api-import.expected.json | 4 +- .../test/integ.auth-apikey.expected.json | 4 +- .../test/integ.graphql-iam.expected.json | 18 +- .../test/integ.graphql.expected.json | 442 +++++++++--------- 6 files changed, 240 insertions(+), 237 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/lib/data-source.ts b/packages/@aws-cdk/aws-appsync/lib/data-source.ts index 00895965a23a3..6eb23053d11ee 100644 --- a/packages/@aws-cdk/aws-appsync/lib/data-source.ts +++ b/packages/@aws-cdk/aws-appsync/lib/data-source.ts @@ -126,7 +126,11 @@ export abstract class BaseDataSource extends CoreConstruct { * creates a new resolver for this datasource and API using the given properties */ public createResolver(props: BaseResolverProps): Resolver { - return this.api.createResolver({ dataSource: this, ...props }); + return new Resolver(this, `${props.typeName}${props.fieldName}Resolver`, { + api: this.api, + dataSource: this, + ...props, + }); } /** diff --git a/packages/@aws-cdk/aws-appsync/test/appsync.test.ts b/packages/@aws-cdk/aws-appsync/test/appsync.test.ts index ca496e99afbff..f4ddfc3ba9da0 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync.test.ts @@ -55,8 +55,7 @@ test('appsync should error when creating pipeline resolver with data source', () // THEN expect(() => { - api.createResolver({ - dataSource: ds, + ds.createResolver({ typeName: 'test', fieldName: 'test2', pipelineConfig: [test1, test2], diff --git a/packages/@aws-cdk/aws-appsync/test/integ.api-import.expected.json b/packages/@aws-cdk/aws-appsync/test/integ.api-import.expected.json index 87f6776cc79d1..e4b54f04116fc 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.api-import.expected.json +++ b/packages/@aws-cdk/aws-appsync/test/integ.api-import.expected.json @@ -135,7 +135,7 @@ } } }, - "ApiQuerygetTestsResolver025B8E0A": { + "ApidsQuerygetTestsResolver952F49EE": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -152,7 +152,7 @@ "Apids0DB53FEA" ] }, - "ApiMutationaddTestResolver7A08AE91": { + "ApidsMutationaddTestResolverBCF0400B": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-appsync/test/integ.auth-apikey.expected.json b/packages/@aws-cdk/aws-appsync/test/integ.auth-apikey.expected.json index 390d55bd9bb38..2d1ef3f31504f 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.auth-apikey.expected.json +++ b/packages/@aws-cdk/aws-appsync/test/integ.auth-apikey.expected.json @@ -121,7 +121,7 @@ } } }, - "ApiQuerygetTestsResolver025B8E0A": { + "ApitestDataSourceQuerygetTestsResolverA3BBB672": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -142,7 +142,7 @@ "ApitestDataSource96AE54D5" ] }, - "ApiMutationaddTestResolver7A08AE91": { + "ApitestDataSourceMutationaddTestResolver36203D6B": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-appsync/test/integ.graphql-iam.expected.json b/packages/@aws-cdk/aws-appsync/test/integ.graphql-iam.expected.json index 4cd9043b28c4b..3ab0d2f229ce5 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.graphql-iam.expected.json +++ b/packages/@aws-cdk/aws-appsync/test/integ.graphql-iam.expected.json @@ -150,7 +150,7 @@ } } }, - "ApiQuerygetTestResolver4C1F8B0C": { + "ApidsQuerygetTestResolverCCED7EC2": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -171,7 +171,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetTestsResolver025B8E0A": { + "ApidsQuerygetTestsResolver952F49EE": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -192,7 +192,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationaddTestResolver7A08AE91": { + "ApidsMutationaddTestResolverBCF0400B": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -400,14 +400,12 @@ ] } }, - "Handler": "iam-query.handler", "Role": { "Fn::GetAtt": [ "LambdaIAM687B49AF", "Arn" ] }, - "Runtime": "nodejs12.x", "Environment": { "Variables": { "APPSYNC_ENDPOINT": { @@ -417,7 +415,9 @@ ] } } - } + }, + "Handler": "iam-query.handler", + "Runtime": "nodejs12.x" }, "DependsOn": [ "LambdaIAMDefaultPolicy96DEA124", @@ -496,14 +496,12 @@ ] } }, - "Handler": "iam-query.handler", "Role": { "Fn::GetAtt": [ "testFailServiceRole9FF22F85", "Arn" ] }, - "Runtime": "nodejs12.x", "Environment": { "Variables": { "APPSYNC_ENDPOINT": { @@ -513,7 +511,9 @@ ] } } - } + }, + "Handler": "iam-query.handler", + "Runtime": "nodejs12.x" }, "DependsOn": [ "testFailServiceRole9FF22F85" diff --git a/packages/@aws-cdk/aws-appsync/test/integ.graphql.expected.json b/packages/@aws-cdk/aws-appsync/test/integ.graphql.expected.json index d540f3777ed00..ab94605706d2c 100644 --- a/packages/@aws-cdk/aws-appsync/test/integ.graphql.expected.json +++ b/packages/@aws-cdk/aws-appsync/test/integ.graphql.expected.json @@ -90,7 +90,7 @@ "Type": "NONE" } }, - "ApiQuerygetServiceVersionResolver269A74C1": { + "ApinoneQuerygetServiceVersionResolver336A3C2C": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -198,204 +198,7 @@ } } }, - "ApiorderDsServiceRoleCC2040C0": { - "Type": "AWS::IAM::Role", - "Properties": { - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Action": "sts:AssumeRole", - "Effect": "Allow", - "Principal": { - "Service": "appsync.amazonaws.com" - } - } - ], - "Version": "2012-10-17" - } - } - }, - "ApiorderDsServiceRoleDefaultPolicy3315FCF4": { - "Type": "AWS::IAM::Policy", - "Properties": { - "PolicyDocument": { - "Statement": [ - { - "Action": [ - "dynamodb:BatchGetItem", - "dynamodb:GetRecords", - "dynamodb:GetShardIterator", - "dynamodb:Query", - "dynamodb:GetItem", - "dynamodb:Scan", - "dynamodb:ConditionCheckItem", - "dynamodb:BatchWriteItem", - "dynamodb:PutItem", - "dynamodb:UpdateItem", - "dynamodb:DeleteItem" - ], - "Effect": "Allow", - "Resource": [ - { - "Fn::GetAtt": [ - "OrderTable416EB896", - "Arn" - ] - }, - { - "Fn::Join": [ - "", - [ - { - "Fn::GetAtt": [ - "OrderTable416EB896", - "Arn" - ] - }, - "/index/*" - ] - ] - } - ] - } - ], - "Version": "2012-10-17" - }, - "PolicyName": "ApiorderDsServiceRoleDefaultPolicy3315FCF4", - "Roles": [ - { - "Ref": "ApiorderDsServiceRoleCC2040C0" - } - ] - } - }, - "ApiorderDsB50C8AAD": { - "Type": "AWS::AppSync::DataSource", - "Properties": { - "ApiId": { - "Fn::GetAtt": [ - "ApiF70053CD", - "ApiId" - ] - }, - "Name": "Order", - "Type": "AMAZON_DYNAMODB", - "DynamoDBConfig": { - "AwsRegion": { - "Ref": "AWS::Region" - }, - "TableName": { - "Ref": "OrderTable416EB896" - } - }, - "ServiceRoleArn": { - "Fn::GetAtt": [ - "ApiorderDsServiceRoleCC2040C0", - "Arn" - ] - } - } - }, - "ApipaymentDsServiceRole0DAC58D6": { - "Type": "AWS::IAM::Role", - "Properties": { - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Action": "sts:AssumeRole", - "Effect": "Allow", - "Principal": { - "Service": "appsync.amazonaws.com" - } - } - ], - "Version": "2012-10-17" - } - } - }, - "ApipaymentDsServiceRoleDefaultPolicy528E42B0": { - "Type": "AWS::IAM::Policy", - "Properties": { - "PolicyDocument": { - "Statement": [ - { - "Action": [ - "dynamodb:BatchGetItem", - "dynamodb:GetRecords", - "dynamodb:GetShardIterator", - "dynamodb:Query", - "dynamodb:GetItem", - "dynamodb:Scan", - "dynamodb:ConditionCheckItem", - "dynamodb:BatchWriteItem", - "dynamodb:PutItem", - "dynamodb:UpdateItem", - "dynamodb:DeleteItem" - ], - "Effect": "Allow", - "Resource": [ - { - "Fn::Join": [ - "", - [ - "arn:", - { - "Ref": "AWS::Partition" - }, - ":dynamodb:", - { - "Ref": "AWS::Region" - }, - ":", - { - "Ref": "AWS::AccountId" - }, - ":table/PaymentTable" - ] - ] - }, - { - "Ref": "AWS::NoValue" - } - ] - } - ], - "Version": "2012-10-17" - }, - "PolicyName": "ApipaymentDsServiceRoleDefaultPolicy528E42B0", - "Roles": [ - { - "Ref": "ApipaymentDsServiceRole0DAC58D6" - } - ] - } - }, - "ApipaymentDs95C7AC36": { - "Type": "AWS::AppSync::DataSource", - "Properties": { - "ApiId": { - "Fn::GetAtt": [ - "ApiF70053CD", - "ApiId" - ] - }, - "Name": "Payment", - "Type": "AMAZON_DYNAMODB", - "DynamoDBConfig": { - "AwsRegion": { - "Ref": "AWS::Region" - }, - "TableName": "PaymentTable" - }, - "ServiceRoleArn": { - "Fn::GetAtt": [ - "ApipaymentDsServiceRole0DAC58D6", - "Arn" - ] - } - } - }, - "ApiQuerygetCustomersResolver522EE433": { + "ApicustomerDsQuerygetCustomersResolverA74C8A2E": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -416,7 +219,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerResolver007520DC": { + "ApicustomerDsQuerygetCustomerResolver3649A130": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -437,7 +240,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationaddCustomerResolver53321A05": { + "ApicustomerDsMutationaddCustomerResolver4DE5B517": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -458,7 +261,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationsaveCustomerResolver85516C23": { + "ApicustomerDsMutationsaveCustomerResolver241DD231": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -479,7 +282,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationsaveCustomerWithFirstOrderResolver66DBDFD0": { + "ApicustomerDsMutationsaveCustomerWithFirstOrderResolver7DE2CBC8": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -500,7 +303,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationremoveCustomerResolver8435F803": { + "ApicustomerDsMutationremoveCustomerResolverAD3AE7F5": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -521,7 +324,105 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersEqResolver6DA88A11": { + "ApiorderDsServiceRoleCC2040C0": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "appsync.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "ApiorderDsServiceRoleDefaultPolicy3315FCF4": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyDocument": { + "Statement": [ + { + "Action": [ + "dynamodb:BatchGetItem", + "dynamodb:GetRecords", + "dynamodb:GetShardIterator", + "dynamodb:Query", + "dynamodb:GetItem", + "dynamodb:Scan", + "dynamodb:ConditionCheckItem", + "dynamodb:BatchWriteItem", + "dynamodb:PutItem", + "dynamodb:UpdateItem", + "dynamodb:DeleteItem" + ], + "Effect": "Allow", + "Resource": [ + { + "Fn::GetAtt": [ + "OrderTable416EB896", + "Arn" + ] + }, + { + "Fn::Join": [ + "", + [ + { + "Fn::GetAtt": [ + "OrderTable416EB896", + "Arn" + ] + }, + "/index/*" + ] + ] + } + ] + } + ], + "Version": "2012-10-17" + }, + "PolicyName": "ApiorderDsServiceRoleDefaultPolicy3315FCF4", + "Roles": [ + { + "Ref": "ApiorderDsServiceRoleCC2040C0" + } + ] + } + }, + "ApiorderDsB50C8AAD": { + "Type": "AWS::AppSync::DataSource", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "ApiF70053CD", + "ApiId" + ] + }, + "Name": "Order", + "Type": "AMAZON_DYNAMODB", + "DynamoDBConfig": { + "AwsRegion": { + "Ref": "AWS::Region" + }, + "TableName": { + "Ref": "OrderTable416EB896" + } + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "ApiorderDsServiceRoleCC2040C0", + "Arn" + ] + } + } + }, + "ApiorderDsQuerygetCustomerOrdersEqResolverEF9D5350": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -542,7 +443,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersEqResolverA8612570": { + "ApiorderDsQuerygetOrderCustomersEqResolverE58570FF": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -563,7 +464,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersLtResolver284B37E8": { + "ApiorderDsQuerygetCustomerOrdersLtResolver909F3D8F": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -584,7 +485,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersLtResolver618A88A9": { + "ApiorderDsQuerygetOrderCustomersLtResolver77468800": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -605,7 +506,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersLeResolverFB2A506A": { + "ApiorderDsQuerygetCustomerOrdersLeResolverF230A8BE": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -626,7 +527,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersLeResolverFAA205B9": { + "ApiorderDsQuerygetOrderCustomersLeResolver836A0389": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -647,7 +548,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersGtResolver13F90EA5": { + "ApiorderDsQuerygetCustomerOrdersGtResolverF01F806B": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -668,7 +569,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersGtResolver9487ECD9": { + "ApiorderDsQuerygetOrderCustomersGtResolver3197CCFE": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -689,7 +590,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersGeResolver2A181899": { + "ApiorderDsQuerygetCustomerOrdersGeResolver63CAD303": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -710,7 +611,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersGeResolver8441E608": { + "ApiorderDsQuerygetOrderCustomersGeResolver0B78B0B4": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -731,7 +632,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersFilterResolver96245084": { + "ApiorderDsQuerygetCustomerOrdersFilterResolverCD2B8747": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -752,7 +653,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetCustomerOrdersBetweenResolver97B6D708": { + "ApiorderDsQuerygetCustomerOrdersBetweenResolver7DEE368E": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -773,7 +674,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersFilterResolverF1015ABD": { + "ApiorderDsQuerygetOrderCustomersFilterResolver628CC68D": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -794,7 +695,7 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetOrderCustomersBetweenResolver92680CEC": { + "ApiorderDsQuerygetOrderCustomersBetweenResolver2048F3CB": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -815,7 +716,106 @@ "ApiSchema510EECD7" ] }, - "ApiQuerygetPaymentResolver29598AC3": { + "ApipaymentDsServiceRole0DAC58D6": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "appsync.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "ApipaymentDsServiceRoleDefaultPolicy528E42B0": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyDocument": { + "Statement": [ + { + "Action": [ + "dynamodb:BatchGetItem", + "dynamodb:GetRecords", + "dynamodb:GetShardIterator", + "dynamodb:Query", + "dynamodb:GetItem", + "dynamodb:Scan", + "dynamodb:ConditionCheckItem", + "dynamodb:BatchWriteItem", + "dynamodb:PutItem", + "dynamodb:UpdateItem", + "dynamodb:DeleteItem" + ], + "Effect": "Allow", + "Resource": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":dynamodb:", + { + "Ref": "AWS::Region" + }, + ":", + { + "Ref": "AWS::AccountId" + }, + ":table/PaymentTable" + ] + ] + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ], + "Version": "2012-10-17" + }, + "PolicyName": "ApipaymentDsServiceRoleDefaultPolicy528E42B0", + "Roles": [ + { + "Ref": "ApipaymentDsServiceRole0DAC58D6" + } + ] + } + }, + "ApipaymentDs95C7AC36": { + "Type": "AWS::AppSync::DataSource", + "Properties": { + "ApiId": { + "Fn::GetAtt": [ + "ApiF70053CD", + "ApiId" + ] + }, + "Name": "Payment", + "Type": "AMAZON_DYNAMODB", + "DynamoDBConfig": { + "AwsRegion": { + "Ref": "AWS::Region" + }, + "TableName": "PaymentTable" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "ApipaymentDsServiceRole0DAC58D6", + "Arn" + ] + } + } + }, + "ApipaymentDsQuerygetPaymentResolverD172BFC9": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -836,7 +836,7 @@ "ApiSchema510EECD7" ] }, - "ApiMutationsavePaymentResolver03088E76": { + "ApipaymentDsMutationsavePaymentResolverE09FE5BB": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": { @@ -896,7 +896,7 @@ } } }, - "ApiMutationdoPostOnAwsResolverEDACEA42": { + "ApidsMutationdoPostOnAwsResolver9583D8A3": { "Type": "AWS::AppSync::Resolver", "Properties": { "ApiId": {