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

GEN2 - Unable to query with sorting #13527

Closed
grfortun opened this issue Jun 12, 2024 · 7 comments
Closed

GEN2 - Unable to query with sorting #13527

grfortun opened this issue Jun 12, 2024 · 7 comments
Assignees
Labels
Gen 2 Issues related to Gen 2 Amplify projects GraphQL Related to GraphQL API issues question General question transferred This issue was transferred from another Amplify project

Comments

@grfortun
Copy link

grfortun commented Jun 12, 2024

Environment information

System:
  OS: macOS 14.5
  CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  Memory: 178.72 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 20.13.0 - ~/.nvm/versions/node/v20.13.0/bin/node
  Yarn: undefined - undefined
  npm: 10.5.2 - ~/.nvm/versions/node/v20.13.0/bin/npm
  pnpm: 9.3.0 - ~/.nvm/versions/node/v20.13.0/bin/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.3
  @aws-amplify/backend-cli: 0.16.0
  aws-amplify: 6.3.6
  aws-cdk: 2.145.0
  aws-cdk-lib: 2.145.0
  typescript: 5.4.5
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

I am query with sorting by date but facing issues with using the sortDirection property. Following the Gen 2 documentation:
[+] https://docs.amplify.aws/react/build-a-backend/data/data-modeling/secondary-index/

Schema:

const schema = a.schema({
  Todo: a
    .model({
      content: a.string(),
      accountRepresentativeId: a.id().required(),
      responseTime: a.string(),
    }).authorization(allow => [allow.owner()])
    .secondaryIndexes((index) => [
      index("accountRepresentativeId")
        .name("repByDate")
        .queryField("listByDate")
        .sortKeys(["responseTime"]),
    ])
});

export type Schema = ClientSchema<typeof schema>;

Query:

const {
      data,
      errors
    } = await client.models.Todo.listByDate({
      accountRepresentativeId: '1',
      sortDirection: 'ASC'
    })

Error:

When running my application the query works fine and sorts the data accordingly but I am unable to build my application as I am faced with the following error:

Type error: Object literal may only specify known properties, and 'sortDirection' does not exist in type 'IndexQueryInput<{ Todo: ClientModel<..., SchemaMetadata<ModelSchema<{ types: { Todo: ModelType<SetTypeSubArg<SetTypeSubArg<{ fields: { name: ModelField<Nullable<string>, never, undefined>; phoneNumber: ModelField<Nullable<string>, never, undefined>; accountRepresentativeId: ModelField<...>; responseTime: ModelField<...'.

  24 |     } = await client.models.Todo.listByDate({
  25 |       accountRepresentativeId: '1',
> 26 |       sortDirection: 'ASC'
     |       ^
  27 |       
  28 |     
  29 |     })
@grfortun grfortun added the pending-triage Issue is pending triage label Jun 12, 2024
@ykethan
Copy link
Member

ykethan commented Jun 12, 2024

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂

@ykethan ykethan transferred this issue from aws-amplify/amplify-backend Jun 12, 2024
@ykethan ykethan added transferred This issue was transferred from another Amplify project Gen 2 Issues related to Gen 2 Amplify projects labels Jun 12, 2024
@AnilMaktala AnilMaktala self-assigned this Jun 12, 2024
@AnilMaktala
Copy link
Member

Hi @grfortun, Thanks for bringing this up. We can reproduce the issue and will mark it as a bug for the team to further evaluate.
image

@AnilMaktala AnilMaktala added bug Something isn't working and removed pending-triage Issue is pending triage labels Jun 12, 2024
@AnilMaktala AnilMaktala removed their assignment Jun 12, 2024
@david-mcafee
Copy link
Contributor

@grfortun - Thanks for creating this issue! The reason you're seeing this type error is because you are providing sortDirection via the input. The correct format for your query would be as follows, providing sortDirection as an options argument:

const { data, errors } = await client.models.Todo.listByDate(
    {
        accountRepresentativeId: '1',
    },
    {
        sortDirection: 'ASC',
    },
);

I just checked this myself, and I'm seeing the options params suggested correctly in my IDE. Can you confirm that you are seeing this as well? Thank you!

@david-mcafee david-mcafee added pending-response bug Something isn't working and removed bug Something isn't working labels Jun 20, 2024
@david-mcafee david-mcafee transferred this issue from aws-amplify/amplify-category-api Jun 20, 2024
@david-mcafee david-mcafee removed the bug Something isn't working label Jun 20, 2024
@cwomack cwomack added question General question GraphQL Related to GraphQL API issues labels Jun 24, 2024
@chrisbonifacio
Copy link
Member

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

@anishniroula1
Copy link

@david-mcafee When ever I create a new secondary indexes or update secondary indexes, it erases the data from Database. Is that suppose to happen?

this is my schema:

Favourite: a.model({
    id: a.id(),
    userId: a.id().required(),
    createdAt: a.string().required(),
    professionalUserId: a.id(),
    businessId: a.id(),
    user: a.belongsTo('User', 'userId'),
    professionalUser: a.belongsTo('ProfessionalUser', 'professionalUserId'),
    business: a.belongsTo('Business', 'businessId'),
  }).authorization((allow) => [allow.authenticated()]).secondaryIndexes((index) => [
        index("userId").queryField("userFavouriteList").sortKeys(["createdAt", "businessId"]),

@github-actions github-actions bot added the pending-maintainer-response Issue is pending a response from the Amplify team. label Nov 11, 2024
@chrisbonifacio
Copy link
Member

@david-mcafee When ever I create a new secondary indexes or update secondary indexes, it erases the data from Database. Is that suppose to happen?

this is my schema:


Favourite: a.model({

    id: a.id(),

    userId: a.id().required(),

    createdAt: a.string().required(),

    professionalUserId: a.id(),

    businessId: a.id(),

    user: a.belongsTo('User', 'userId'),

    professionalUser: a.belongsTo('ProfessionalUser', 'professionalUserId'),

    business: a.belongsTo('Business', 'businessId'),

  }).authorization((allow) => [allow.authenticated()]).secondaryIndexes((index) => [

        index("userId").queryField("userFavouriteList").sortKeys(["createdAt", "businessId"]),

Yes, that is expected behavior for sandbox environments. Those changes are destructive.

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending a response from the Amplify team. label Nov 15, 2024
@anishniroula1
Copy link

thanks for the response and clarification. I was worried this might happen in prod also.

@github-actions github-actions bot added the pending-maintainer-response Issue is pending a response from the Amplify team. label Nov 15, 2024
@chrisbonifacio chrisbonifacio removed the pending-maintainer-response Issue is pending a response from the Amplify team. label Nov 16, 2024
@chrisbonifacio chrisbonifacio self-assigned this Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gen 2 Issues related to Gen 2 Amplify projects GraphQL Related to GraphQL API issues question General question transferred This issue was transferred from another Amplify project
Projects
None yet
Development

No branches or pull requests

7 participants