Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access requested fields in resolver mapping template

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.

like image 748
bodokaiser Avatar asked Dec 31 '22 20:12

bodokaiser


2 Answers

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.

like image 103
Jeff Bailey Avatar answered Jan 05 '23 18:01

Jeff Bailey


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.

like image 45
JWK Avatar answered Jan 05 '23 17:01

JWK