8

When we request a GraphQL query, for instance,

query GetPost {
  singlePost(id: 123) {
    id
    title
  }
}

and we have configured a Lambda resolver in AWS AppSync, the request mapping template,

{
    "version": "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "resolve": "singlePost",
        "query": $utils.toJson($context.arguments)
    }
}

allows us to define the event object passed to the lambda handler.

For the above example, our Lambda handler would be invoked with an event event wherein event.payload.query.id == 123 or the like.

According to the docs the $context object comprises,

{
    "arguments" : { ... },
    "source" : { ... },
    "result" : { ... },
    "identity" : { ... },
    "request" : { ... }
}

That said, the documentation does not mention where I can access the requested fields of the GraphQL query.

For the former example, these fields would correspond to ["id", "title"].

In the case that I need to resolve some nested properties, e.g. a tags array, of an object through an expensive operation, e.g. a SQL join, it would be beneficial if I could check if this nested property is actually requested.

This question relates to How to get requested fields inside GraphQL resolver?, however, it differs from in the GraphQL implementation graphql-tools vs AppSync.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
bodokaiser
  • 15,122
  • 22
  • 97
  • 140

3 Answers3

8

This is actually not something that AppSync supports today, unfortunately. It is, however, a request we've heard from other customers, and I'll use this post as a +1 to prioritize it for a future release.

Jeff Bailey
  • 5,655
  • 1
  • 22
  • 30
  • 1
    Is there any update on this feature? It's something that would GREATLY reduce mapping template bloat in our application.. haven't seen any ongoing conversation about this feature. – LMulvey Sep 06 '19 at 15:41
  • 1
    https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#aws-appsync-resolver-context-reference-info – Yves M. Jan 28 '22 at 14:52
4

There is now a $context.info.selectionSetList field that includes the requested field names. The official docs provide an example and explain some special use cases around interfaces and aliases.

JWK
  • 670
  • 2
  • 6
  • 16
  • Note that when using `$utils.toJson()` on `context.info`, the values that `selectionSetList` return are not serialized by default. See also https://github.com/aws-amplify/amplify-cli/issues/4869 – Yves M. Jan 28 '22 at 15:01
0

The document has been updated, AWS added a new field info to the context, you can access the field name as below:

{
    "version" : "2017-02-28",
    "operation" : "Invoke",
    "payload": {
      "resolve": "$ctx.info.fieldName",
      "query": $utils.toJson($context.arguments)
    }
}

You can refer to the document here:

https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#aws-appsync-resolver-context-reference-info

Yves M.
  • 29,855
  • 23
  • 108
  • 144
bandaot
  • 25
  • 1
  • 2
    `info.fieldName` is the field on the `parent` that this resolver is being asked to resolve, not the array of field names being requested on the final object that the original question is asking for. – CodingWithSpike Dec 14 '20 at 20:33