Can't create custom queries and resolvers in nested fields on GraphQL/AppSync Amplify

0

Hello! I am trying to custom a query with graphQL amplify without success. I am able to use the default queries and mutations but I am not being able to create a query type Query to filter nested fields. For example, I want to get the Books that have difficulty = Beginner.

My schema in schema.graphql:

type Books @model {
    bookName: String! @primaryKey
    EN: BookDetails
    ES: BookDetails
}

type BookDetails {
    bookName: String
    bookDescription: String
    difficulty: String
}

My query in schema.graphql:

type Query {
  getBookByDifficulty(difficulty: String!): [Book]
}

I am unable to put this query to work with custom resolvers. I wrote req.vtl and res.vtl files, put them on the resolvers folder in the amplify backend folder but it doesn't work. I can see the query on the console, but it always returns null.

Query.getCocktailByDifficulty.req.vtl in amplify/backend/api/{API_NAME}/resolvers/:

#set($tableName = "Cocktail-4oiehkpc4bhpbiqxd6oj63bxqe-dev")

#set($queryExpression = {
  "expression": "EN.difficulty = :difficulty",
  "expressionValues": {
    ":difficulty": $util.dynamodb.toDynamoDBJson($ctx.args.difficulty)
  }
})

{
  "version": "2018-05-29",
  "operation": "Query",
  "index": "yourGSIName",  
  "query": $queryExpression
}

Query.getCocktailByDifficulty.res.vtl amplify/backend/api/{API_NAME}/resolvers/:

#if($ctx.result.items)
  #set($transformedItems = [])
  #foreach($item in $ctx.result.items)
    #set($transformedItem = {
      "id": $item.id,
      "name": $item.name,
      "EN": {
        "difficulty": $item.EN.difficulty
      }
    })
    $util.qr($transformedItems.add($transformedItem))
  #end
  $util.toJson($transformedItems)
#else
  $util.error("No items found", "ItemNotFoundError")
#end

Please please heeeelp! I don't find any useful documentation on the internet.

Thank you

gefragt vor 2 Monaten102 Aufrufe
Keine Antworten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen