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

enhance: Union types construct class instances in ctrl.fetch() resolution value #3063

Merged
merged 1 commit into from
May 16, 2024

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented May 16, 2024

Motivation

ctrl.fetch() resolution should always be denormalized if possible. Previously, we would skip class instantiation (and other denorm things) with Polymorphic types, as they use a special normalized format to select the schema for denormalization.

Solution

Polymorphic (Union) types should still denormalize when handling passthrough (non-normalized) data

When denormalizing non-normalized (like return of ctrl.fetch), it is still expected to handle
all steps like constructing class instances if possible. However, to do this for Polymorphic
types we need to fallback to using part of the normalize process to find out which schema
to use for the remainder of denormalization.

This also removes extra console.warn that would appear when using useSuspense() or other hooks, even though the normalize+denormalize worked fine - since ctrl.fetch() always runs denormalize in 'passthrough' mode (without entities or normalized input).

export class Link extends FeedItem {
  readonly type = 'link' as const;
  readonly url: string = '';
  readonly title: string = '';
}
export class Post extends FeedItem {
  readonly type = 'post' as const;
  readonly content: string = '';
}
export const getFeed = new RestEndpoint({
  path: '/feed',
  schema: new schema.Array(
    {
      link: Link,
      post: Post,
    },
    'type',
  ),
});

Before

const feed = await ctrl.fetch(getFeed);
assert feed[0] is pojo

After

const feed = await ctrl.fetch(getFeed);
assert feed[0] instanceOf Post || feed[0] instanceOf Link

Copy link

changeset-bot bot commented May 16, 2024

🦋 Changeset detected

Latest commit: e218d8f

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

This PR includes changesets to release 8 packages
Name Type
@data-client/endpoint Patch
@data-client/graphql Patch
@data-client/rest Patch
example-benchmark Patch
normalizr-github-example Patch
normalizr-redux-example Patch
normalizr-relationships Patch
rdc-website Patch

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

Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.49%. Comparing base (a66d51e) to head (e218d8f).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3063   +/-   ##
=======================================
  Coverage   98.49%   98.49%           
=======================================
  Files         122      122           
  Lines        2196     2199    +3     
  Branches      449      451    +2     
=======================================
+ Hits         2163     2166    +3     
  Misses         21       21           
  Partials       12       12           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: e218d8f Previous: 4bc9145 Ratio
normalizeLong 432 ops/sec (±2.08%) 435 ops/sec (±1.81%) 1.01
infer All 9767 ops/sec (±0.66%) 9500 ops/sec (±1.31%) 0.97
denormalizeLong 345 ops/sec (±0.92%) 331 ops/sec (±2.76%) 0.96
denormalizeLong donotcache 892 ops/sec (±0.26%) 890 ops/sec (±0.57%) 1.00
denormalizeShort donotcache 500x 1345 ops/sec (±0.39%) 1356 ops/sec (±0.89%) 1.01
denormalizeShort 500x 973 ops/sec (±0.24%) 979 ops/sec (±0.44%) 1.01
denormalizeShort 500x withCache 4876 ops/sec (±0.35%) 5094 ops/sec (±0.37%) 1.04
denormalizeLong with mixin Entity 292 ops/sec (±0.51%) 293 ops/sec (±0.31%) 1.00
denormalizeLong withCache 6185 ops/sec (±0.25%) 6217 ops/sec (±0.17%) 1.01
denormalizeLong All withCache 6646 ops/sec (±0.17%) 6757 ops/sec (±0.15%) 1.02
denormalizeLong Query-sorted withCache 6503 ops/sec (±0.48%) 6620 ops/sec (±0.51%) 1.02
denormalizeLongAndShort withEntityCacheOnly 1521 ops/sec (±0.52%) 1483 ops/sec (±1.82%) 0.98
getResponse 5830 ops/sec (±0.68%) 6315 ops/sec (±1.08%) 1.08
getResponse (null) 6297913 ops/sec (±1.10%) 5367929 ops/sec (±0.53%) 0.85
getResponse (clear cache) 288 ops/sec (±0.47%) 278 ops/sec (±0.61%) 0.97
getSmallResponse 2660 ops/sec (±0.28%) 2669 ops/sec (±0.48%) 1.00
getSmallInferredResponse 2040 ops/sec (±0.25%) 2018 ops/sec (±0.52%) 0.99
getResponse Query-sorted 5406 ops/sec (±0.80%) 5570 ops/sec (±0.80%) 1.03
getResponse Collection 5786 ops/sec (±0.68%) 6532 ops/sec (±0.92%) 1.13
get Collection 4940 ops/sec (±1.20%) 5837 ops/sec (±1.12%) 1.18
setLong 438 ops/sec (±1.88%) 432 ops/sec (±2.32%) 0.99
setLongWithMerge 184 ops/sec (±1.81%) 187 ops/sec (±1.20%) 1.02
setLongWithSimpleMerge 194 ops/sec (±1.25%) 193 ops/sec (±1.82%) 0.99
setSmallResponse 500x 860 ops/sec (±1.34%) 857 ops/sec (±1.29%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@ntucker ntucker merged commit 2080c87 into master May 16, 2024
26 checks passed
@ntucker ntucker deleted the polymorphic-denorm branch May 16, 2024 21:21
@github-actions github-actions bot mentioned this pull request May 16, 2024
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.

None yet

1 participant