Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appsync - How to return response from multiple functions in pipleline resolvers

Tags:

aws-appsync

input CarInput{ 
    name: String
    brand: String
}

type Car{   
    id: ID  
    name: String    
    brand: String
}

type Vehicle{   
    id: ID  
    carId: Id
}    

type CustomResponse{
    createdCar : Car
    allCars: [Car]
}

type Mutation {
    createCar(input: CarInput): CustomResponse
}

Attached Pipeline resolver for createCar

Before mapping template

{}

1 - CreateCarFunction

Request Mapping Template

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload":{ 
  "body":$util.toJson($context.args.input),
  "resource":"/car",
  "httpmethod":"POST"
  }
}

Response Mapping Template

$context.result.body

2 - CreateVehicleFunction

Request Mapping Template

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload":{ 
  "body":$util.toJson($ctx.prev.result.id),
  "resource":"/vehicle",
  "httpmethod":"POST"
  }
}

Response Mapping Template

$context.result.body

3 - GetCarsFunction

Request Mapping Template

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {  
  "body":"",
  "resource":"/getCars" ,
  "httpmethod":"GET"
  }
}

Response Mapping Template

$context.result.body

After mapping template

 $util.toJson($ctx.result)

The datasource is Lamda. I would be getting three different response from CreateCarFunction,CreateVehicleFunction and GetCarsFunction .

mutation{createCar(input:
{
 name: "A6"
 brand: "Audi"
})
 {
   createdCar  
   {
     id
     name
     brand
   }

   allCars
   {
     id
     name
     brand
   }   
 }

I want a response like this

{
  "data": {
    "createCar": {
      "createdCar  ": {
                        id : "2"
                        name : "A6"
                        brand : "Audi"
                      },
      "allCars":  [
                     {
                        id : "1"
                        name : "S"
                        brand : "Benz"
                     },
                     {
                        id : "2"
                        name : "A6"
                        brand : "Audi"
                     }
                 ]

          }
}

So I need the response of CreateCarFunction and GetCarsFunction so that CustomResponse type is populated as the output. How do I achieve this?

like image 930
Senthil Avatar asked Jan 21 '26 09:01

Senthil


1 Answers

Thank you for including all the relevant details for this question. You can pass data from one Function to the next by adding the output (in the Response mapping template) to $ctx.stash. The $ctx.stash is a mutable object which is persisted throughout the lifetime of the pipeline resolver.

Modify your response mapping template for each function to something like

CreateCarFunction

$util.qr($ctx.stash.put("createdCar", $ctx.result.body))
$context.result.body

GetCarsFunction

$util.qr($ctx.stash.put("allCars", $ctx.result.body))
$context.result.body

(The first line adds the result to the stash. The second line returns the body of the request as before.)

Then in your After mapping template, you can deserialize the results with

$util.toJson($ctx.stash)
like image 188
Aaron_H Avatar answered Jan 23 '26 20:01

Aaron_H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!