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

feat: Queries can take object schemas #3165

Merged
merged 3 commits into from
Jul 26, 2024
Merged

feat: Queries can take object schemas #3165

merged 3 commits into from
Jul 26, 2024

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jul 26, 2024

Motivation

Client-side joins should all be doable in Queries.

This enables joins across arbitrary elements (not just nested entities).

For instance, with our crypto live price demo, we need to fallback to Stats when loading a page until the websocket Ticker has updated. This ensures SSR and initial page loads work.

Solution

class Ticker extends Entity {
  product_id = '';
  price = 0;

  pk(): string {
    return this.product_id;
  }
}
class Stats extends Entity {
  product_id = '';
  last = 0;

  pk(): string {
    return this.product_id;
  }
}
const queryPrice = new schema.Query(
  { ticker: Ticker, stats: Stats },
  ({ ticker, stats }) => ticker?.price ?? stats?.last,
);

This allows simple lookups for the price:

const price = useQuery(queryPrice, { product_id });

Copy link

changeset-bot bot commented Jul 26, 2024

🦋 Changeset detected

Latest commit: f379c07

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

This PR includes changesets to release 10 packages
Name Type
@data-client/normalizr Patch
@data-client/endpoint Patch
@data-client/react Patch
@data-client/core Patch
@data-client/graphql Patch
@data-client/rest Patch
example-benchmark Patch
normalizr-github-example Patch
normalizr-redux-example Patch
normalizr-relationships 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
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: f379c07 Previous: dbe7cfa Ratio
normalizeLong 498 ops/sec (±0.85%) 519 ops/sec (±0.50%) 1.04
infer All 9182 ops/sec (±0.20%) 9508 ops/sec (±2.06%) 1.04
denormalizeLong 342 ops/sec (±0.80%) 331 ops/sec (±3.22%) 0.97
denormalizeLong donotcache 853 ops/sec (±1.75%) 891 ops/sec (±0.13%) 1.04
denormalizeShort donotcache 500x 1342 ops/sec (±0.66%) 1365 ops/sec (±0.19%) 1.02
denormalizeShort 500x 966 ops/sec (±0.28%) 979 ops/sec (±0.27%) 1.01
denormalizeShort 500x withCache 4771 ops/sec (±1.29%) 5003 ops/sec (±0.21%) 1.05
denormalizeLong with mixin Entity 294 ops/sec (±0.48%) 301 ops/sec (±0.38%) 1.02
denormalizeLong withCache 6021 ops/sec (±0.26%) 6244 ops/sec (±0.21%) 1.04
denormalizeLong All withCache 6477 ops/sec (±0.15%) 6736 ops/sec (±0.12%) 1.04
denormalizeLong Query-sorted withCache 6337 ops/sec (±0.46%) 6557 ops/sec (±0.57%) 1.03
denormalizeLongAndShort withEntityCacheOnly 1427 ops/sec (±0.49%) 1523 ops/sec (±0.54%) 1.07
getResponse 5614 ops/sec (±0.38%) 6071 ops/sec (±1.15%) 1.08
getResponse (null) 5411353 ops/sec (±0.79%) 5793714 ops/sec (±0.50%) 1.07
getResponse (clear cache) 295 ops/sec (±0.38%) 296 ops/sec (±0.44%) 1.00
getSmallResponse 2654 ops/sec (±0.14%) 2673 ops/sec (±0.17%) 1.01
getSmallInferredResponse 2094 ops/sec (±0.16%) 2047 ops/sec (±0.23%) 0.98
getResponse Query-sorted 5494 ops/sec (±0.47%) 5461 ops/sec (±0.42%) 0.99
getResponse Collection 5876 ops/sec (±1.24%) 6183 ops/sec (±1.08%) 1.05
get Collection 4881 ops/sec (±1.66%) 5285 ops/sec (±1.07%) 1.08
setLong 500 ops/sec (±0.93%) 492 ops/sec (±0.31%) 0.98
setLongWithMerge 196 ops/sec (±0.42%) 198 ops/sec (±0.34%) 1.01
setLongWithSimpleMerge 208 ops/sec (±0.40%) 216 ops/sec (±0.58%) 1.04
setSmallResponse 500x 899 ops/sec (±0.50%) 921 ops/sec (±0.13%) 1.02

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

@ntucker ntucker merged commit 3fa9eb9 into master Jul 26, 2024
24 checks passed
@ntucker ntucker deleted the object-query branch July 26, 2024 11:33
@github-actions github-actions bot mentioned this pull request Jul 26, 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.

1 participant