Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appsync & GraphQL: how to filter a list by nested value

I have an Appsync API generated by Amplify from a basic schema. On the Article model, a category field is nested within a metadata field. I want to build a Query that provides a list of Articles filtered by category. It is not clear to me how to filter on a nested value... I have seen similar questions but the analogous answer has not worked.

AWS GraphQL Transform Schema

type Article @model {
  id: ID!
  title: String!
  description: String!
  text: String!
  metadata: ArticleMetadata!
}

type ArticleMetadata {
  category: Category!
  lastModified: String!
  creationDate: String!
}

enum Category {
  javascript
  java
  ruby
  python
  haskell
}

Generated List Query

export const listArticles = `query ListArticles(
  $filter: ModelArticleFilterInput
  $limit: Int
  $nextToken: String
) {
  listArticles(filter: $filter, limit: $limit, nextToken: $nextToken) {
    items {
      id
      title
      description
      text
      metadata {
        category
        lastModified
        creationDate
      }
    }
    nextToken
  }
}
`;

Failing filter query

query listArticlesByCategory($category: String!) {
  listArticles(filter: {category: {eq: $category}}) { 
    items {
      title
      description
      text
      metadata {
        category
        creationDate
        lastModified
      }
    }
  }
}

The Appsync console error states that the category in filter: {category: ... } is an unknown field.

like image 591
Kwhitejr Avatar asked Mar 13 '19 00:03

Kwhitejr


People also ask

What is an AppSync?

AWS AppSync allows your applications to access exactly the data they need. Create a flexible API to securely access, manipulate, and combine data from multiple sources. Pay only for requests to your API and for real-time messages delivered to connected clients.

Is AppSync a GraphQL server?

AWS AppSync provides a robust, scalable GraphQL interface for application developers to combine data from multiple sources, including Amazon DynamoDB, AWS Lambda, and HTTP APIs.

Is AppSync an API gateway?

AppSync is a serverless GraphQL service. In simple terms, it's just an API gateway based on the GraphQL specification you can pay on-demand. GraphQL offers a friendly query language on top of HTTP, making it possible to fetch multiple different resources in one request, lowering network latency.


1 Answers

By default the Amplify codegen only will operate on top-level filters. You can extend this to include filters for the attributes nested in ArticleMetadata.

You will need to augment the ModelArticleFilterInput type to include the category field. Assuming that the metadata field in the article table is backed by a DynamoDB map, you can filter based on the map value. You will need to modify the listArticles resolver's Request Mapping Template VTL to add the filter expression that contains something like metadata.category = :category when there is a value for cateogry in the filter argument.

like image 69
Aaron_H Avatar answered Sep 24 '22 06:09

Aaron_H