Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AmplifyOutputs.json ignores Multiple Secondary Index Queries using the same SecondaryIndex Field #2815

Open
ChristopherGabba opened this issue Aug 28, 2024 · 3 comments · Fixed by aws-amplify/amplify-codegen#879
Labels
bug Something isn't working Gen 2 pending-release

Comments

@ChristopherGabba
Copy link

ChristopherGabba commented Aug 28, 2024

Environment information

System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M2 Pro
  Memory: 158.91 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.7.0 - /opt/homebrew/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 10.8.2 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.2.0
  @aws-amplify/backend-auth: 1.1.3
  @aws-amplify/backend-cli: 1.2.5
  @aws-amplify/backend-data: 1.1.3
  @aws-amplify/backend-deployer: 1.1.0
  @aws-amplify/backend-function: 1.3.4
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.0
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.3.0
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.5
  @aws-amplify/platform-core: 1.0.7
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.0
  @aws-amplify/schema-generator: 1.2.1
  aws-amplify: 6.5.3
  aws-cdk: 2.154.1
  aws-cdk-lib: 2.154.1
  typescript: 5.3.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Data packages

├─┬ @aws-amplify/[email protected]
│ └─┬ @aws-amplify/[email protected]
│   └── @aws-amplify/[email protected]
└─┬ @aws-amplify/[email protected]
  └─┬ @aws-amplify/[email protected]
    └── @aws-amplify/[email protected]

Description

I am trying to make multiple queries using the same SecondaryIndex but different sortKeys:

User: a
    .model({
      id: a.id().required(),
      birthdate: a.string().required(),
      email: a.email().required(),
      firstName: a.string().required(),
      lastName: a.string().required(),
      username: a.string().required(),
      phoneNumber: a.hasOne("PhoneNumber", "userId"),
      pushToken: a.string(),
      profileImageS3Path: a.string(),
      profileImageBlurhash: a.string(),
      status: a.ref("UserStatus").required(),
    })
    .secondaryIndexes((index) => [
      index("status").queryField("listActiveUsersByFirstname").sortKeys(["firstName"]), // HERE
      index("status").queryField("listActiveUsersByLastname").sortKeys(["lastName"]), // HERE
      index("status").queryField("listActiveUsersByUsername").sortKeys(["username"]), // HERE
      index("email").name("byEmail").queryField("listUsersByEmail"),
    ])
    .authorization((allow) => [allow.publicApiKey()]),

But when I run npx ampx sandbox or I push the commit to the the backend console, the amplify_outputs.json file just ignores the second two secondary indexes:

"attributes": [
            {
              "type": "model",
              "properties": {}
            },
            {
              "type": "key",
              "properties": {
                "fields": [
                  "id"
                ]
              }
            },
            {
              "type": "key",
              "properties": {
                "name": "byEmail",
                "queryField": "listUsersByEmail",
                "fields": [
                  "email"
                ]
              }
            },
            {
              "type": "key",
              "properties": {
                "name": "usersByStatusAndFirstName",
                "queryField": "listActiveUsersByFirstname",
                "fields": [
                  "status",
                  "firstName"
                ]
              }
            },
            {
              "type": "auth",
              "properties": {
                "rules": [
                  {
                    "allow": "public",
                    "provider": "apiKey",
                    "operations": [
                      "create",
                      "update",
                      "delete",
                      "read"
                    ]
                  }
                ]
              }
            }
          ],
          "primaryKeyInfo": {
            "isCustomPrimaryKey": false,
            "primaryKeyFieldName": "id",
            "sortKeyFieldNames": []
          }
        },

As you can see, it only made the listActiveUsersByFirstName. I can confirm as well when I try to run these other two queries, I get a function does not exist type of error.

I've tried making one secondaryIndex query like so:

    .secondaryIndexes((index) => [
      index("status")
        .queryField("listActiveUsersByName")
        .sortKeys(["firstName", "lastName", "username"]),
      index("email").name("byEmail").queryField("listUsersByEmail"),
    ])

But the query doesn't seem to be working:

const results = await client.models.User.listActiveUsersByName(
          {
            status: UserStatus.ACTIVE,
            firstNameLastNameUsername: {
              beginsWith: {
                firstName: capitalizeFirstLetter(word),
                lastName: capitalizeFirstLetter(word),
                username: capitalizeFirstLetter(word),
              },
            },
          },
          { selectionSet: userSelectionSet },
        )

I'm trying to get results for each category (firstname, lastname, and username) individually, not combined.

@AnilMaktala
Copy link
Member

Hi @ChristopherGabba, Thank you for bringing this issue to our attention. We were able to reproduce it using the schema details provided. As a result, we will be marking this as a bug for the team to further investigate.

schema:

const schema = a.schema({
User: a
    .model({
      id: a.id().required(),
      birthdate: a.string().required(),
      email: a.email().required(),
      firstName: a.string().required(),
      lastName: a.string().required(),
      username: a.string().required(),

      pushToken: a.string(),
      profileImageS3Path: a.string(),
      profileImageBlurhash: a.string(),
      status: a.string().required(),
    })
    .secondaryIndexes((index) => [
      index("status")
        .queryField("listActiveUsersByFirstname")
        .sortKeys(["firstName"]), // HERE
      index("status")
        .queryField("listActiveUsersByLastname")
        .sortKeys(["lastName"]), // HERE
      index("status")
        .queryField("listActiveUsersByUsername")
        .sortKeys(["username"]), // HERE
      index("email").name("byEmail").queryField("listUsersByEmail"),
    ])
    .authorization((allow) => [allow.publicApiKey()]),
});

generated introspection schema:

"User": {
            "name": "User",
            "fields": {
                "id": {
                    "name": "id",
                    "isArray": false,
                    "type": "ID",
                    "isRequired": true,
                    "attributes": []
                },
                "birthdate": {
                    "name": "birthdate",
                    "isArray": false,
                    "type": "String",
                    "isRequired": true,
                    "attributes": []
                },
                "email": {
                    "name": "email",
                    "isArray": false,
                    "type": "AWSEmail",
                    "isRequired": true,
                    "attributes": []
                },
                "firstName": {
                    "name": "firstName",
                    "isArray": false,
                    "type": "String",
                    "isRequired": true,
                    "attributes": []
                },
                "lastName": {
                    "name": "lastName",
                    "isArray": false,
                    "type": "String",
                    "isRequired": true,
                    "attributes": []
                },
                "username": {
                    "name": "username",
                    "isArray": false,
                    "type": "String",
                    "isRequired": true,
                    "attributes": []
                },
                "pushToken": {
                    "name": "pushToken",
                    "isArray": false,
                    "type": "String",
                    "isRequired": false,
                    "attributes": []
                },
                "profileImageS3Path": {
                    "name": "profileImageS3Path",
                    "isArray": false,
                    "type": "String",
                    "isRequired": false,
                    "attributes": []
                },
                "profileImageBlurhash": {
                    "name": "profileImageBlurhash",
                    "isArray": false,
                    "type": "String",
                    "isRequired": false,
                    "attributes": []
                },
                "status": {
                    "name": "status",
                    "isArray": false,
                    "type": "String",
                    "isRequired": true,
                    "attributes": []
                },
                "createdAt": {
                    "name": "createdAt",
                    "isArray": false,
                    "type": "AWSDateTime",
                    "isRequired": false,
                    "attributes": [],
                    "isReadOnly": true
                },
                "updatedAt": {
                    "name": "updatedAt",
                    "isArray": false,
                    "type": "AWSDateTime",
                    "isRequired": false,
                    "attributes": [],
                    "isReadOnly": true
                }
            },
            "syncable": true,
            "pluralName": "Users",
            "attributes": [
                {
                    "type": "model",
                    "properties": {}
                },
                {
                    "type": "key",
                    "properties": {
                        "fields": [
                            "id"
                        ]
                    }
                },
                {
                    "type": "key",
                    "properties": {
                        "name": "byEmail",
                        "queryField": "listUsersByEmail",
                        "fields": [
                            "email"
                        ]
                    }
                },
                {
                    "type": "key",
                    "properties": {
                        "name": "usersByStatusAndFirstName",
                        "queryField": "listActiveUsersByFirstname",
                        "fields": [
                            "status",
                            "firstName"
                        ]
                    }
                },
                {
                    "type": "auth",
                    "properties": {
                        "rules": [
                            {
                                "allow": "public",
                                "provider": "apiKey",
                                "operations": [
                                    "create",
                                    "update",
                                    "delete",
                                    "read"
                                ]
                            }
                        ]
                    }
                }
            ],
            "primaryKeyInfo": {
                "isCustomPrimaryKey": false,
                "primaryKeyFieldName": "id",
                "sortKeyFieldNames": []
            }
        }

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

@sundersc
Copy link
Contributor

The fix aws-amplify/amplify-codegen#879 is pending codegen release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Gen 2 pending-release
Projects
None yet
3 participants