Skip to content

feat(data-schema): accept null to disable queryField for secondary indexes #567

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

naedx
Copy link

@naedx naedx commented Mar 19, 2025

Problem

There was previously no way to disable the top-level query field that was automatically created when a secondary index is created. This PR adds support for passing null as in index(...).queryField(null) to disable the query field. This is consistent with the configuration described since Gen1

export const schema = a.schema({
  Customer: a
    .model({
      name: a.string(),
      phoneNumber: a.phone(),
      accountRepresentativeId: a.id().required(),
    })
    .secondaryIndexes(
        (index) => [
            index("accountRepresentativeId")
                 .sortKeys(["name"])
                 .queryField(null)            //<<< added support for null to be passed
        ]
    )
    .authorization(allow => [allow.publicApiKey()]),
});

Issue number, if available:

#566

Changes

Corresponding docs PR, if applicable:

Validation

Unit tests have been included to validate a similar behaviour to Gen1

  • Current (unchanged) behavior:
    • queryField('someName') results in a query field called 'someName'
    • queryField() not called results in a query field with default naming
  • Changed behavior:
    • queryField(null) results in the query field being set to null

Checklist

  • If this PR includes a functional change to the runtime or type-level behavior of the code, I have added or updated automated test coverage for this change. (see Testing Strategy README)
  • If this PR requires a docs update, I have linked to that docs PR above.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@naedx naedx requested a review from a team as a code owner March 19, 2025 20:07
Copy link

changeset-bot bot commented Mar 19, 2025

🦋 Changeset detected

Latest commit: 8e8af6f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/data-schema Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@naedx
Copy link
Author

naedx commented Mar 26, 2025

Note that the changes maintain the Gen1 behaviour which requires explicitly defining queryField: null otherwise the default queryField with the default name is created.

As such, the PR changes .queryField(string) to .queryField(string | null) where null has to be given explicitly to disable the field (like Gen1). This is backwards compatible with any existing user code since it would not change the behavior of existing usage (or non-usage) of the queryField method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant