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

r/aws_appsync_resolver: Support direct resolver #14710

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 14 additions & 8 deletions aws/resource_aws_appsync_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func resourceAwsAppsyncResolver() *schema.Resource {
},
"request_template": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"response_template": {
Type: schema.TypeString,
Required: true, // documentation bug, the api returns 400 if this is not specified.
Optional: true,
},
"kind": {
Type: schema.TypeString,
Expand Down Expand Up @@ -110,12 +110,10 @@ func resourceAwsAppsyncResolverCreate(d *schema.ResourceData, meta interface{})
conn := meta.(*AWSClient).appsyncconn

input := &appsync.CreateResolverInput{
ApiId: aws.String(d.Get("api_id").(string)),
TypeName: aws.String(d.Get("type").(string)),
FieldName: aws.String(d.Get("field").(string)),
RequestMappingTemplate: aws.String(d.Get("request_template").(string)),
ResponseMappingTemplate: aws.String(d.Get("response_template").(string)),
Kind: aws.String(d.Get("kind").(string)),
ApiId: aws.String(d.Get("api_id").(string)),
TypeName: aws.String(d.Get("type").(string)),
FieldName: aws.String(d.Get("field").(string)),
Kind: aws.String(d.Get("kind").(string)),
}

if v, ok := d.GetOk("data_source"); ok {
Expand All @@ -129,6 +127,14 @@ func resourceAwsAppsyncResolverCreate(d *schema.ResourceData, meta interface{})
}
}

if v, ok := d.GetOk("request_template"); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we will also need to do the same in Update 👍

input.RequestMappingTemplate = aws.String(v.(string))
}

if v, ok := d.GetOk("response_template"); ok {
input.ResponseMappingTemplate = aws.String(v.(string))
}

if v, ok := d.GetOk("caching_config"); ok {
input.CachingConfig = expandAppsyncResolverCachingConfig(v.([]interface{}))
}
Expand Down
73 changes: 73 additions & 0 deletions aws/resource_aws_appsync_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,32 @@ func TestAccAwsAppsyncResolver_DataSource(t *testing.T) {
})
}

func TestAccAwsAppsyncResolver_DataSource_lambda(t *testing.T) {
var resolver appsync.Resolver
rName := fmt.Sprintf("tfacctest%d", acctest.RandInt())
resourceName := "aws_appsync_resolver.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSAppSync(t) },
bflad marked this conversation as resolved.
Show resolved Hide resolved
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsAppsyncResolverDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppsyncResolver_DataSource_lambda(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAppsyncResolverExists(resourceName, &resolver),
resource.TestCheckResourceAttr(resourceName, "data_source", rName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAwsAppsyncResolver_RequestTemplate(t *testing.T) {
var resolver1, resolver2 appsync.Resolver
rName := fmt.Sprintf("tfacctest%d", acctest.RandInt())
Expand Down Expand Up @@ -471,6 +497,53 @@ EOF
`, rName, dataSource)
}

func testAccAppsyncResolver_DataSource_lambda(rName string) string {
return testAccAppsyncDatasourceConfig_base_Lambda(rName) + fmt.Sprintf(`
resource "aws_appsync_graphql_api" "test" {
authentication_type = "API_KEY"
name = %q

schema = <<EOF
type Mutation {
putPost(id: ID!, title: String!): Post
}

type Post {
id: ID!
title: String!
}

type Query {
singlePost(id: ID!): Post
}

schema {
query: Query
mutation: Mutation
}
EOF
}

resource "aws_appsync_datasource" "test" {
api_id = "${aws_appsync_graphql_api.test.id}"
name = %q
service_role_arn = "${aws_iam_role.test.arn}"
type = "AWS_LAMBDA"

lambda_config {
function_arn = "${aws_lambda_function.test.arn}"
}
}

resource "aws_appsync_resolver" "test" {
api_id = aws_appsync_graphql_api.test.id
field = "singlePost"
type = "Query"
data_source = aws_appsync_datasource.test.name
}
`, rName, rName)
}

func testAccAppsyncResolver_RequestTemplate(rName, resourcePath string) string {
return fmt.Sprintf(`
resource "aws_appsync_graphql_api" "test" {
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/appsync_resolver.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ The following arguments are supported:
* `api_id` - (Required) The API ID for the GraphQL API.
* `type` - (Required) The type name from the schema defined in the GraphQL API.
* `field` - (Required) The field name from the schema defined in the GraphQL API.
* `request_template` - (Required) The request mapping template for UNIT resolver or 'before mapping template' for PIPELINE resolver.
* `response_template` - (Required) The response mapping template for UNIT resolver or 'after mapping template' for PIPELINE resolver.
* `request_template` - (Optional) The request mapping template for UNIT resolver or 'before mapping template' for PIPELINE resolver.
* `response_template` - (Optional) The response mapping template for UNIT resolver or 'after mapping template' for PIPELINE resolver.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually might be worth stating that these are only really option if type = "AWS_LAMBDA" (although there's no code-level check for that atm -- not sure if needed since it should just fail on AWS side during apply, but nice if we want feedback within terraform). Also might be nice to add an example to the code blocks on this page for "Direct to lambda resolver" or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree here - we should probably note the requirement for non-Lambda resolvers. 👍

bflad marked this conversation as resolved.
Show resolved Hide resolved
* `data_source` - (Optional) The DataSource name.
* `kind` - (Optional) The resolver type. Valid values are `UNIT` and `PIPELINE`.
* `pipeline_config` - (Optional) The PipelineConfig.
Expand Down