ComMa915
ComMa915

Reputation: 103

Filtering data from AWS Amplify Graphql API

Graphql Schema :

type Media @model
{
  id: ID!
  title: String
  type: String
}

Sample Data :

{
      id: "some ID",
      title: "sample media1",
      type: "image/png",
}

{
      id: "some ID",
      title: "sample media2",
      type: "video/mp4",
}

I'd like to fetch only data that its type contains string "image".

type: "image/mp4"

I know that I can get some data based on title in this way.

import { API, graphqlOperation } from "aws-amplify"
import * as queries from "@src/graphql/queries"
...
let title = "sample media1"
const medias = await API.graphql(
                 graphqlOperation(queries.listMedias, { filter: { title: { eq: title } } })
               )

Is there any similar way to get data that one of its attributes contains a specific value?

"contains" not "exact same"

Upvotes: 0

Views: 2513

Answers (1)

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5718

As stated in AWS Amplify documentation you can use corresponding DynamoDB queries. One of this queries is contains, but it is dependent on the type of field that your are doing filtering. List of all operators supported by DynamoDB:

  • EQ
  • NE
  • LE
  • LT
  • GE
  • GT
  • NOT_NULL
  • NULL
  • CONTAINS
  • NOT_CONTAINS
  • BEGINS_WITH
  • IN
  • BETWEEN

So in your example:

import { API, graphqlOperation } from "aws-amplify"
import * as queries from "@src/graphql/queries"
...
let title = "sample media1"
const medias = await API.graphql(
                 graphqlOperation(queries.listMedias, { filter: { title: { contains: title } } })
               )

Upvotes: 1

Related Questions