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

Using owner as a search filter #665

Open Jesmaster opened 2 years ago

Jesmaster commented 2 years ago

Before opening, please confirm:

How did you install the Amplify CLI?

No response

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

No response

Amplify CLI Version

9.1.0

What operating system are you using?

Ubunutu Mate

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

No manual changes made

Amplify Categories

api

Amplify Commands

push

Describe the bug

When creating/updating a model and doing "amplify push", filters and inputs are not created for the automatically generated fields (createdAt, updatedAt, _version, _deleted, owner...).

I have a need in my app to be able to filter by owner and to create a new record in a lambda function as a specific owner.

Before I was accomplishing this by manually going into AppSync Schema any time I push and manually update the filters/inputs I need to add the owner field. This worked but was a bit cumbersome as I need to re-do these changes every time I push schema updates.

I then added owner: ID to the model definitions for models I needed this for and that added the inputs/filters I need.

However, there is a strange behaivor when filtering by owner where doing a graphql "eq" comparsion returns no result but "match" or "beginsWith" does return the expected result.

Example: I have a searchable model called StudentInvitation

query MyQuery {
  searchStudentInvitations(filter: {owner: {eq: "5eee27fb-8cb0-43e0-897e-67256df46c53"}}) {
    total
  }
}
{
  "data": {
    "searchStudentInvitations": {
      "total": 0
    }
  }
}

changing "eq" to "match" gives me my actual total count of 7

query MyQuery {
  searchStudentInvitations(filter: {owner: {match: "5eee27fb-8cb0-43e0-897e-67256df46c53"}}) {
    total
  }
}
{
  "data": {
    "searchStudentInvitations": {
      "total": 7
    }
  }
}

I have another model Profile with the same issue

query MyQuery {
  listProfiles(filter: {owner: {eq: "72adabdc-4b67-458e-af29-8d72c9aa3f53"}}) {
    items {
      owner
      id
    }
  }
}
{
  "data": {
    "listProfiles": {
      "items": []
    }
  }
}

changing to beginsWith for list operations works (list doesn't have "match")

query MyQuery {
  listProfiles(filter: {owner: {beginsWith: "72adabdc-4b67-458e-af29-8d72c9aa3f53"}}) {
    items {
      owner
      id
    }
  }
}
{
  "data": {
    "listProfiles": {
      "items": [
        {
          "owner": "72adabdc-4b67-458e-af29-8d72c9aa3f53",
          "id": "0458b86c-85e8-412c-80af-412f0cdbcb2e"
        },
        {
          "owner": "72adabdc-4b67-458e-af29-8d72c9aa3f53",
          "id": "2b54e84c-f954-4871-bf64-4836d12003a6"
        }
      ]
    }
  }
}

Expected behavior

eq should work when filtering by owner

Reproduction steps

Create a model with owner: ID as one of the fields.

Create a record and don't explicitly set the owner so it is set automatically.

Attempt to do a graphql list or search using the owner and the eq operation

GraphQL schema(s)

No response

Log output

No response

Additional information

No response

alharris-at commented 2 years ago

Hi @Jesmaster, thank you for raising this issue! I see two separate concerns here. First, that generated model fields (owner, _createdAt, etc.) are not generated in the output graphql schema. I'll fork that and treat as a feature request, which makes sense to expose. The second is that 'eq' in searchable isn't working with owner field for you. With the release of Amplify CLI version 8.1.0 a change was made to owner attributes which appends cognito 'sub' field for added security. In most cases the transformation to include/not include is transparent to the customer, but search is one of the areas that doesn't hold true. https://docs.amplify.aws/cli/migration/identity-claim-changes/#what-are-the-breaking-changes

We'll reproduce on our end, and see if this seems to be related to a substring search issue, or if we're able to repro the 'eq' issue as well.

alharris-at commented 2 years ago

Feature request has been opened here https://github.com/aws-amplify/amplify-category-api/issues/683

ykethan commented 2 years ago

Hi, deployed the following schema.

type Student @model @auth(rules: [{ allow: owner, ownerField: "author" }]) @searchable {
  name: String
  author: String
}

performed a mutation and let AppSync auto assign owner.

mutation MyMutation {
  createStudent(input: {name: "test1"}) {
    author
    createdAt
    id
  }
}

Outputs for searchable and list queries

image image

Using format subID::ownername will work when using username as sign in method. image

marking this as bug for improvements.

wDutton commented 9 months ago

Has this been fixed/updated? Or is there another way to query just for user created items? It seems to me that this should be a no-brainer to have a way to filter/search items by the user who created them. Especially if users create an item, and want to view all their created items in a dashboard.

Tymyan1 commented 2 weeks ago

Very much would appreciate this, can't believe this is still not a thing and it took me a while to figure out why! For some reason the behaviour I've observed was that my query fetched the correct items on page refresh.... Anyway, a workable work-around seems to be (at least for my use case) to just have the field duplicated, once for auth and once for filtering, not amazing but oh well...