Skip to content

feat: optimize custom selection set type#647

Merged
pranavosu merged 10 commits intomainfrom
feat/optimize-selection-sets/main
Feb 19, 2026
Merged

feat: optimize custom selection set type#647
pranavosu merged 10 commits intomainfrom
feat/optimize-selection-sets/main

Conversation

@pranavosu
Copy link
Contributor

@pranavosu pranavosu commented Jan 5, 2026

Problem

Issue number, if available:

#424

Changes

Corresponding docs PR, if applicable: N/A

Validation

Test project: https://github.com/pranavosu/selection-set-repro

prereq: valid AWS creds

test commands: npx ampx sandbox -> npm run build -> npm run start

run without overrides ("@aws-amplify/data-schema": "1.22.0",) and then with the @amplify/data-schema package overrides (0.0.0-optimize-selection-sets-20260108232711).

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.

@pranavosu pranavosu requested review from a team as code owners January 5, 2026 06:09
@changeset-bot
Copy link

changeset-bot bot commented Jan 5, 2026

🦋 Changeset detected

Latest commit: 3733d95

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

@oe-bayram
Copy link

We applied these changes as a patch to our project, but we still get the error message from #424

TS2590: Expression produces a union type that is too complex to represent.

@pranavosu
Copy link
Contributor Author

pranavosu commented Jan 12, 2026

We applied these changes as a patch to our project, but we still get the error message from #424

TS2590: Expression produces a union type that is too complex to represent.

Are you sure all instances of @amplify/data-schema are updated? You might need to add overrides. I'd think if you want to test this use - "@aws-amplify/data-schema":"0.0.0-optimize-selection-sets-20260108232711" as an override for your project.

Copy link
Member

@bobbor bobbor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried my best to understand what is actually going on here.

I left a bunch of comments. If what I wrote makes no sense I would understand that too :D

@oe-bayram
Copy link

I’ve installed the snapshot version via npm overrides as suggested.
This resolves the TS2590 error from #424, however it introduces a new issue:

The SelectionSet type no longer resolves correctly and now collapses to any.

import { SelectionSet } from 'aws-amplify/api';

export type User = SelectionSet<
  Schema['User']['type'],
  [
    'id',
    'firstName',
    'lastName',
    'email',
    'workingContracts.*',
    'workingContracts.workingTimeModelID',
  ]
>;

Before applying the snapshot, this produced the expected strongly typed result (but triggered TS2590).
With the optimized version, the compiler error is gone, but User is now inferred as any.

When using the same selection set for querying, the returned data is correctly typed:

const { data } = await client.models.User.list({
  selectionSet: UserSelectionSet,
});

@oe-bayram
Copy link

I’ve installed the snapshot version via npm overrides as suggested. This resolves the TS2590 error from #424, however it introduces a new issue:

The SelectionSet type no longer resolves correctly and now collapses to any.

import { SelectionSet } from 'aws-amplify/api';

export type User = SelectionSet<
  Schema['User']['type'],
  [
    'id',
    'firstName',
    'lastName',
    'email',
    'workingContracts.*',
    'workingContracts.workingTimeModelID',
  ]
>;

Before applying the snapshot, this produced the expected strongly typed result (but triggered TS2590). With the optimized version, the compiler error is gone, but User is now inferred as any.

When using the same selection set for querying, the returned data is correctly typed:

const { data } = await client.models.User.list({
  selectionSet: UserSelectionSet,
});

Thanks — I found the reason why my SelectionSet alias was resolving to any.

The first generic parameter of SelectionSet<…> must be the model definition (the thing that includes __meta / flatModel), not the plain model “type” shape. In my case I incorrectly used:

type User = SelectionSet<
  Schema['User']['type'], 
  typeof UserSelectionSet
>

Since Schema['User']['type'] does NOT extend ClientSchemaByEntityTypeBaseShape['models'][string], the conditional type falls back to any.

Switching it to the model definition fixes it:

type User = SelectionSet<
  Schema['User'], 
  typeof UserSelectionSet
>

@pranavosu
Copy link
Contributor Author

Thanks for the feedback @oe-bayram! Appreciate it.

@bobbor bobbor force-pushed the feat/optimize-selection-sets/main branch from dd23112 to 54c5624 Compare January 29, 2026 14:31
@pranavosu pranavosu merged commit c42c6d1 into main Feb 19, 2026
23 checks passed
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.

4 participants