简体   繁体   中英

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"

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 } } })
               )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM