Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS AppSync Lambda resolver fields

I have the following query:

query xxx {
   getSomething(id: "id") {
      field1
      field2
   }
}

Is there any way for me to get field1 and field2 in lambda? For example, to query only those fields in mysql, not get all of them just to be discarded by AppSync later.

I tried logging all the $context in the request mapper VTL file but they are not there. Any ideas? Seems quite stupid to not be able to do that. The only thing I get in lambda is the id argument.

Thanks, Mihai

like image 887
Mihai Blaga Avatar asked Nov 13 '18 09:11

Mihai Blaga


People also ask

What is resolver in AppSync?

PDF. Data sources and resolvers are how AWS AppSync translates GraphQL requests and fetches information from your AWS resources. AWS AppSync has support for automatic provisioning and connections with certain data source types.

How do I trigger lambda function from AppSync?

From the schema editor in the AWS AppSync console, on the right side, choose Attach Resolver for getPost(id:ID!): Post . Choose your Lambda data source. In the request mapping template section, choose Invoke And Forward Arguments. In the response mapping template section, choose Return Lambda Result.

How do you build a pipeline resolver?

Create A Pipeline ResolverIn the AWS AppSync console, go to the Schema page. We are going to wire a pipeline resolver to the signUp field on the Mutation type. In the Mutation type on the right side, choose Attach resolver next to the signUp field. On the Create Resolver page, click on the Switch to Pipeline button.

How do I query AWS AppSync?

In the AWS AppSync console choose the Queries tab on the left hand side. The pane on the right side enables you to click through the operations, including queries, mutations, and subscriptions that your schema has exposed. Choose the Mutation node to see a mutation.

What is Lambda resolver in AppSync?

A resolver defines how a GraphQL field gets its value, such as what database query should AppSync run when a query needs that field, or what HTTP request to send. AppSync supports several resolver types, and one of them is the Lambda resolver.

How do I connect to AWS Lambda with AppSync?

From the schema editor in the AWS AppSync console, on the right side choose Attach Resolver for getPost (id:ID!): Post . Choose your AWS Lambda data source.

What is the AWS AppSync resolver resource?

The AWS::AppSync::Resolver resource defines the logical GraphQL resolver that you attach to fields in a schema. Request and response templates for resolvers are written in Apache Velocity Template Language (VTL) format.

How do I connect GraphQL fields to my AWS Lambda data source?

Now that we’ve registered an AWS Lambda data source and a valid GraphQL schema, we can connect our GraphQL fields to our Lambda data source using resolvers. To create a resolver, we’ll need mapping templates.


Video Answer


2 Answers

AppSync now supports getting the GraphQL Info object. You can get the list of requested columns from the selectionSetList variable.

The layout of the Info object:

{
    "fieldName": "string",
    "parentTypeName": "string",
    "variables": { ... },
    "selectionSetList": ["string"],
    "selectionSetGraphQL": "string"
}

An example passing the selectionSetList property to a lambda resolver:

{
    "version" : "2017-02-28",
    "operation": "Invoke",
    "payload": {
        "arguments": $utils.toJson($ctx.args),
        "selectionSetList": $utils.toJson($ctx.info.selectionSetList),
        "selectionSetGraphQL": $utils.toJson($ctx.info.selectionSetGraphQL)
    }
}

Note: If you are trying to pass the selectionSetList then you need to specifically reference it (like in the example above). The list will not be available if the info object is passed in directly with something like $utils.toJson($ctx.info).

like image 185
Trisped Avatar answered Oct 16 '22 16:10

Trisped


It might not be the answer you want to hear, but as you've spotted AppSync simply doesn't make the graphql (fields, or otherwise) available to you.

The only two 'options" I can put to you are:

  • Design your query schema so that you can be more precise with your fetching (e.g. getThingFromTableA and getThingFromTableB rather than just getThing)
  • Use field resolvers for expensive to get fields, and employ nested objects if these are all from the same datasource (e.g. { cheapA, cheapB, expensiveA { expensiveTableAThingA, expensiveTableAThingB }, expensiveB }).

n.b. it isn't that uncommon, for example Apollo doesn't by default either.

like image 30
thomasmichaelwallace Avatar answered Oct 16 '22 17:10

thomasmichaelwallace