Umer Farooq
Umer Farooq

Reputation: 1

Filtering by sub field in Amplify GraphQL with DynamoDB

I'm trying to implement a custom resolver in Amplify GraphQL to filter cocktails based on the difficulty field stored in a DynamoDB table. Here's my current setup:

Schema (schema.graphql):

input AMPLIFY { globalAuthRule: AuthRule = { allow: public } }
type Cocktail @model {
  cocktailName: String! @primaryKey
  EN: CocktailDetails
  ES: CocktailDetails
}
type SpiritsInput {
  name: String!
  quantity: String!
}
type OccasionTagsInputs {
  tag: String!
}
type MethodInputs {
  step: String!
}
type IngredientInput {
  name: String!
  quantity: String!
}
type EquipmentInput {
  name: String!
}
type CuisineTypeInput {
  type: String!
}
type SeasonTagsInput {
  tag: String!
}
type ImageInput {
  url: String!
}
type CocktailDetails {
  cocktailTitle: String
  cocktailDescription: String
  cocktail_url: String
  spirit: [SpiritsInput!]
  occasion: [OccasionTagsInputs!]
  method: [MethodInputs!]
  alcoholVol: Float
  alc: Boolean
  foodPairing: String
  metaData: String
  difficulty: String!  # Ensure difficulty is not nullable
  ingredients: [IngredientInput!]
  preparationTime: String
  equipment: [EquipmentInput!]
  cuisineType: [CuisineTypeInput!]
  seasonTags: [SeasonTagsInput!]
  images: [ImageInput!]
  created_date: String
  updated_date: String
  user: String
}

type Query {
  getCocktailByDifficulty(difficulty: String!): [Cocktail]
}
req.vtl:

$util.log("Scan filter expression: " + $scanFilter.expression)

$set( $scanFilter = { "expression": "EN.difficulty = :difficulty", "expressionValues": { ":difficulty": { "S": $ctx.args.difficulty } } } )
$util.log("Scan request: " + $util.toJson({ "version": "2018-05-29", "operation": "Scan", "filter": $scanFilter }))
$util.toJson({ "version": "2018-05-29", "operation": "Scan", "filter": $scanFilter })

res.vtl:

$util.toJson($output.Payload)

and when i try to run the query by passing the difficulty in query parameter it returns null value i have inserted demo data in the dynamo db

Upvotes: 0

Views: 72

Answers (0)

Related Questions