AWS AppSync access parent resolver properties in nested resolver (interpolation problem?)

280 views Asked by At

I have a GraphQL API which works like this:

mutation {
  customer(id: "123") {
    someMutation(new: "data") {
      id name email # from customer
    }
  }
}

We use nested resolver style because we have a large schema, and it helps keep things clean.

This means we need to resolve "args" from the someMutation, and inherit the ID from the parent resolver.

AWS AppSync docs claims that you can do this with the $context.source.id field, but there are so far as I can tell zero documented options. We have tried this Velocity Template:

{
  "version": "2018-05-29",
  "method": "POST",
  "params": {
    "headers": {
      "Content-Type": "application/json"
    },
    "query": {
      "command_name": "set_email",
      "new": $util.toJson($context.arguments.new),
    }
  },
  "resourcePath": $util.toJson("/customers/$context.source.id")
}

Scant little documentation exists (except this "resolver template mapping guide") about interpolation, or string concatenation, it is pretty inadequate.

According to the "Resolver mapping template context reference" $context.source should "A map that contains the resolution of the parent field."

The failure mode here is that my downstream HTTP resolver is receiving the literal string "/customers/$context.source.id" rather than the interpolated variable.

Try as I might I cannot figure a way to get an interpolated value with or without any $util...() helpers for JSONification string concatenation, any combination of quoting, etc.

1

There are 1 answers

0
Lee Hambley On

So, I figured this out in the end. The parent resolver wasn't responding with the {id: '123'...} data.

I found out that AppSync has a test console, which helped me verify that, yes, indeed my VTL template was working correctly with the expected payload.

enter image description here

What I found incredibly unintuitive, however was that unlike most template languages which would interpolate an empty variable to "" (empty string), VTL templates seem to behave as though you had no interpolation what so ever leading me to question the interpolation syntax in general.

See the screenshot from the AppSync test console below.

There should also be a way to write tests in that use the AWS CLI/SDK but I didn't bother looking into it, ergonomics are bad for that, needing us to configure the CI with a test account on AWS, and we test our application logic at a higher level, anyway.