aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

Filtering query with secondary key ignores filter #11486

Closed jmarshall9120 closed 1 year ago

jmarshall9120 commented 1 year ago

Before opening, please confirm:

JavaScript Framework

Vue

Amplify APIs

GraphQL API

Amplify Categories

auth, api, hosting

Environment information

``` # Put output below this line System: OS: Windows 10 10.0.22000 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 14.97 GB / 31.74 GB Binaries: Node: 16.17.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: 114.0.5735.110 Edge: Spartan (44.22000.120.0), Chromium (114.0.1823.43) Internet Explorer: 11.0.22000.120 npmPackages: @aws-amplify/ui-vue: ^3.1.18 => 3.1.18 @mdi/font: 7.0.96 => 7.0.96 @vitejs/plugin-vue: ^3.0.3 => 3.2.0 aws-amplify: ^5.2.4 => 5.2.4 core-js: ^3.29.0 => 3.30.2 eslint: ^8.37.0 => 8.41.0 eslint-plugin-vue: ^9.3.0 => 9.14.1 pinia: ^2.0.0 => 2.1.3 roboto-fontface: * => 0.10.0 sass: ^1.62.1 => 1.62.1 vite: ^4.2.0 => 4.3.9 vite-plugin-vuetify: ^1.0.0 => 1.0.2 vue: ^3.2.0 => 3.3.4 vue-router: ^4.0.0 => 4.2.1 vue3-easy-data-table: ^1.5.42 => 1.5.42 vuetify: ^3.0.0 => 3.3.1 webfontloader: ^1.0.0 => 1.6.28 npmGlobalPackages: @aws-amplify/cli: 11.1.1 @vue/cli-service-global: 4.5.17 @vue/cli: 4.5.11 browserify: 17.0.0 gulp-cli: 2.3.0 gulp-exec: 5.0.0 npm-install-all: 1.1.21 pnpm: 7.9.3 sass: 1.32.4 webpack-cli: 4.5.0 ```

Describe the bug

Running a query with a secondary key as shown [here] (https://docs.amplify.aws/guides/api-graphql/query-with-sorting/q/platform/js/) results in a query with sorted results as expected, but when a filter is added it ignores the filter. Oddly the query seems formed correctly on the call, however it returns all results unfiltered.

Querying the same query, with same parameters from the app sync dashboard, the filter works as expected.

Expected behavior

The filter to be applied whether querying from the front end of an application, or using the app sync console.

Reproduction steps

To reproduce create the below models and run the code from some front end. You'll need to insert some dummy data.

# api/myApi/schema.graphql

# I've included all fields in my Lease model, in case I'm causing this issue, or some edge case is.  
# To reproduce easily just remove everything except 'id' and 'ledger_enteries'.
type Lease @model {
  id: ID!
  tenents: [Tenent] @manyToMany(relationName: "TenentLease")
  units: [Unit] @manyToMany(relationName: "UnitLease")
  start_date: AWSDateTime
  end_date: AWSDateTime
  isActive: Boolean
  isLockAssigned: Boolean
  isLeaseComplete: Boolean
  ledger_enteries: [RentalLedgerEntry] @hasMany
}

type RentalLedgerEntry @model {
  id: ID!
  date: AWSDate
  type: String
    @index(
      name: "rentalLedgerEntryByDate"
      queryField: "rentalLedgerByDate"
      sortKeyFields: ["date", "createdAt"]
    )
  entry_type: LedgerEntryType
  amount: Float
  description: String
  leaseId: Lease @belongsTo
  createdAt: AWSDateTime
}
// call to the api
import { listRentalLedgerEntries, rentalLedgerByDate } from '@/graphql/queries'

const response = await API.graphql({
    query: rentalLedgerByDate,
    variables: {
      type: 'RentalLedgerEntry',
      sortDirection: 'DESC',
      filters: { leaseLedger_enteriesId: { eq: 'db25f4ed-6aa0-4a0d-bf7d-34d5642eff74' } }
}

// 'db25f4ed-6aa0-4a0d-bf7d-34d5642eff74' is a leaseId from inserted data the doesn't key 
//  to any data in the RentalLedgerEnteries model. 

payload of http post:

{
    "query": "query RentalLedgerByDate($type: String!, $dateCreatedAt: ModelRentalLedgerEntryRentalLedgerEntryByDateCompositeKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelRentalLedgerEntryFilterInput, $limit: Int, $nextToken: String) {\n  rentalLedgerByDate(\n    type: $type\n    dateCreatedAt: $dateCreatedAt\n    sortDirection: $sortDirection\n    filter: $filter\n    limit: $limit\n    nextToken: $nextToken\n  ) {\n    items {\n      id\n      date\n      type\n      entry_type\n      amount\n      description\n      leaseId {\n        id\n        start_date\n        end_date\n        isActive\n        isLockAssigned\n        isLeaseComplete\n        createdAt\n        updatedAt\n        _version\n        _deleted\n        _lastChangedAt\n      }\n      createdAt\n      updatedAt\n      _version\n      _deleted\n      _lastChangedAt\n      leaseLedger_enteriesId\n    }\n    nextToken\n    startedAt\n  }\n}\n",
    "variables": {
        "type": "RentalLedgerEntry",
        "sortDirection": "DESC",
        "filters": {
            "leaseLedger_enteriesId": {
                "eq": "db25f4ed-6aa0-4a0d-bf7d-34d5642eff74"
            }
        }
    }
}

http response

{
    "data": {
        "rentalLedgerByDate": {
            "items": [
                {
                    "id": "8e5f9350-9eaa-4044-9552-66fb67ce5ffa",
                    "date": "2023-06-04",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 95.0,
                    "description": "This Top??",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:27:45.701Z",
                    "updatedAt": "2023-06-04T22:51:49.381Z",
                    "_version": 7,
                    "_deleted": null,
                    "_lastChangedAt": 1685919121425,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "01570aa1-a429-4a57-b296-dbbb2e824841",
                    "date": "2023-06-04",
                    "type": "RentalLedgerEntry",
                    "entry_type": "CREDIT",
                    "amount": 15.0,
                    "description": "Late Fee",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T17:21:19.906Z",
                    "updatedAt": "2023-06-04T17:21:19.906Z",
                    "_version": 2,
                    "_deleted": null,
                    "_lastChangedAt": 1685909188380,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "0a5f8a46-43ef-4a7d-a777-b897b48474d3",
                    "date": "2023-06-03",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 100.5,
                    "description": "jjjjj",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:11:53.192Z",
                    "updatedAt": "2023-06-11T21:20:02.576Z",
                    "_version": 12,
                    "_deleted": null,
                    "_lastChangedAt": 1686518402611,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "5b5f722f-a29c-4af0-8f39-f49217d4c5e4",
                    "date": "2023-06-03",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 222.22,
                    "description": "overpaid",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:09:48.776Z",
                    "updatedAt": "2023-06-11T16:46:03.189Z",
                    "_version": 3,
                    "_deleted": null,
                    "_lastChangedAt": 1686501963221,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "bba9e135-e4a9-40bd-ac21-4d3ff608ea28",
                    "date": "2023-06-03",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 99.0,
                    "description": "Payment",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T17:19:14.733Z",
                    "updatedAt": "2023-06-04T23:36:43.732Z",
                    "_version": 9,
                    "_deleted": true,
                    "_lastChangedAt": 1686520701205,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "5c4a7a75-f3c2-4f42-8372-334149048178",
                    "date": "2023-06-03",
                    "type": "RentalLedgerEntry",
                    "entry_type": "CREDIT",
                    "amount": 105.0,
                    "description": "Monthly Charge",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T01:31:35.901Z",
                    "updatedAt": "2023-06-04T01:31:35.901Z",
                    "_version": 2,
                    "_deleted": null,
                    "_lastChangedAt": 1685909210373,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "6234b561-1dbc-4216-938c-421df2f86399",
                    "date": "2023-06-02",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 25.05,
                    "description": "Something new",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-05T01:48:37.586Z",
                    "updatedAt": "2023-06-05T01:48:37.586Z",
                    "_version": 1,
                    "_deleted": null,
                    "_lastChangedAt": 1685929717590,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "9707595e-6d7a-4107-bdeb-f69d1484e9f0",
                    "date": "2023-06-02",
                    "type": "RentalLedgerEntry",
                    "entry_type": "CREDIT",
                    "amount": 1000.0,
                    "description": "Good Feels....",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:15:46.841Z",
                    "updatedAt": "2023-06-11T18:21:15.109Z",
                    "_version": 3,
                    "_deleted": null,
                    "_lastChangedAt": 1686507675138,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "46d8bc77-12d2-4ab0-9248-42a6abf9d024",
                    "date": "2023-06-02",
                    "type": "RentalLedgerEntry",
                    "entry_type": "CREDIT",
                    "amount": 5000.0,
                    "description": "kkkkk",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:13:13.879Z",
                    "updatedAt": "2023-06-11T19:53:07.399Z",
                    "_version": 3,
                    "_deleted": null,
                    "_lastChangedAt": 1686513187430,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "ceddea9e-c717-4238-b9af-97d6adb7ff92",
                    "date": "2023-06-02",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 250.55,
                    "description": "ddd",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:10:26.454Z",
                    "updatedAt": "2023-06-11T21:22:53.625Z",
                    "_version": 4,
                    "_deleted": null,
                    "_lastChangedAt": 1686518573656,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "88b2031f-4e01-4085-aa5f-c50d4237ba5a",
                    "date": "2023-06-02",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 1111.55,
                    "description": "Over Payment",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:01:39.286Z",
                    "updatedAt": "2023-06-11T22:00:14.354Z",
                    "_version": 6,
                    "_deleted": true,
                    "_lastChangedAt": 1686520814386,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                },
                {
                    "id": "feb10b09-93ab-4a2b-aca2-820fb35ff25d",
                    "date": "2023-05-31",
                    "type": "RentalLedgerEntry",
                    "entry_type": "DEBIT",
                    "amount": 200.25,
                    "description": "Try Again...",
                    "leaseId": {
                        "id": "b42fea1d-36b7-4553-b9e6-4ecd20a51582",
                        "start_date": "2023-01-01T08:00:00.000Z",
                        "end_date": "1970-01-01T00:00:00.000Z",
                        "isActive": true,
                        "isLockAssigned": false,
                        "isLeaseComplete": false,
                        "createdAt": "2023-05-30T18:29:54.870Z",
                        "updatedAt": "2023-05-30T18:29:54.870Z",
                        "_version": 1,
                        "_deleted": null,
                        "_lastChangedAt": 1685471394892
                    },
                    "createdAt": "2023-06-04T21:16:45.006Z",
                    "updatedAt": "2023-06-11T17:50:32.822Z",
                    "_version": 5,
                    "_deleted": null,
                    "_lastChangedAt": 1686505832852,
                    "leaseLedger_enteriesId": "b42fea1d-36b7-4553-b9e6-4ecd20a51582"
                }
            ],
            "nextToken": null,
            "startedAt": null
        }
    }
}

This is dummy data, so no risk.

I get an opposite result through appsync:

image

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

jmarshall9120 commented 1 year ago

Okay, of course after I give up I get it sorted. I called the API with 'filters' instead of 'filter'. Usually I though passing unrecognized parameters caused an error. I will have to test further.

david-mcafee commented 1 year ago

@jmarshall9120 - closing this, as this was a user error. If you have any further questions or concerns, please reply to this ticket. Thank you!