aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 77 forks source link

Compiled schema makes enum filters accept integers instead of string #34

Open ragingsquirrel3 opened 2 years ago

ragingsquirrel3 commented 2 years ago

Before opening, please confirm:

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v12.19.1

Amplify CLI Version

8.0.2

What operating system are you using?

mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

no manual changes made to reproduce

Amplify Categories

api

Amplify Commands

add

Describe the bug

When I create a GraphQL backend using a schema with a enums, the compiled schema includes filters which can be used to run queries with filters. For enum fields, the compiled schema for the filter type of "eq" and other operations is an Int. It's not clear how I can filter for items where e.g. size == 'SMALL' as the request will only accept an integer value.

a schema like

enum Size {
  SMALL
  MEDIUM
  LARGE
}

type Blog @model {
  id: ID!
  name: String!
  size: Size
}

compiles with a listBlogs operation that accepts ModelBlogFilterInput and the size argument is of type ModelInputSize and all the eq, le, etc... operators are of type Int. I could not query it in the appsync console using any int. When I manually changed the compiled schema to Strings for eq I was able to filter by "SMALL."

This arose while investigating amplify-flutter issue https://github.com/aws-amplify/amplify-flutter/issues/1567 and I found I could not do the expected query in the appsync console so was not clear how to fix in amplify-flutter.

Expected behavior

The ModelInputSize operation should have String for all the eq, ge, etc... operators. Or there should be some way to filter by a string.

Reproduction steps

  1. create backend amplify init
  2. amplify add api, use gql, api key, for schema use, see below
  3. amplify push --yes
  4. In appsync console, create a blog with size "SMALL"
  5. Ensure you can query it in appsync console by querying the listBlogs with no filters and see the blog you made, with the size value set to "SMALL."
  6. Try to filter by size

You will have to pass an int.

Expected: you can pass a string

GraphQL schema(s)

```graphql input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } type FileMeta { name: String! } type S3Object { bucket: String! region: String! key: String! meta: FileMeta } enum Size { SMALL MEDIUM LARGE } type Blog @model { id: ID! name: String! createdAt: AWSDateTime file: S3Object files: [S3Object] size: Size posts: [Post] @hasMany(indexName: "byBlog", fields: ["id"]) } type Post @model { id: ID! title: String! rating: Int! created: AWSDateTime blogID: ID! @index(name: "byBlog") blog: Blog @belongsTo(fields: ["blogID"]) comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"]) } type Comment @model { id: ID! postID: ID! @index(name: "byPost") post: Post @belongsTo(fields: ["postID"]) content: String! } ```

Log output

``` # Put your logs below this line ```

Additional information

No response

josefaidt commented 2 years ago

Hey @ragingsquirrel3 :wave: thanks for raising this and by providing those reproduction steps and details! I was able to successfully reproduce this behavior and observed the input type is created as follows:

input ModelSizeInput {
  ne: Int
  eq: Int
  le: Int
  lt: Int
  ge: Int
  gt: Int
  between: [Int]
}

Marking as a bug 🙂