-
Notifications
You must be signed in to change notification settings - Fork 13
add useEntitiesPublicInfinite hook #552
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@graphprotocol/hypergraph-react": patch | ||
| --- | ||
|
|
||
| add useEntitiesPublicInfinite hook] | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||||||||||||||||||||||||||||
| import { useEntitiesPublicInfinite } from '@graphprotocol/hypergraph-react'; | ||||||||||||||||||||||||||||||||||||
| import { createLazyFileRoute } from '@tanstack/react-router'; | ||||||||||||||||||||||||||||||||||||
| import { useEffect } from 'react'; | ||||||||||||||||||||||||||||||||||||
| import { Podcast } from '@/schema'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| export const Route = createLazyFileRoute('/podcasts-infinite')({ | ||||||||||||||||||||||||||||||||||||
| component: RouteComponent, | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| function RouteComponent() { | ||||||||||||||||||||||||||||||||||||
| const space = 'e252f9e1-d3ad-4460-8bf1-54f93b02f220'; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const { data, isLoading, isError, fetchNextPage } = useEntitiesPublicInfinite(Podcast, { | ||||||||||||||||||||||||||||||||||||
| first: 2, | ||||||||||||||||||||||||||||||||||||
| offset: 0, | ||||||||||||||||||||||||||||||||||||
| space: space, | ||||||||||||||||||||||||||||||||||||
| include: { | ||||||||||||||||||||||||||||||||||||
| projects: {}, | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||
| fetchNextPage(); | ||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||||||||||||||||||
| fetchNextPage(); | ||||||||||||||||||||||||||||||||||||
| }, 1000); | ||||||||||||||||||||||||||||||||||||
| }, 1000); | ||||||||||||||||||||||||||||||||||||
| }, [fetchNextPage]); | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+29
|
||||||||||||||||||||||||||||||||||||
| setTimeout(() => { | |
| fetchNextPage(); | |
| setTimeout(() => { | |
| fetchNextPage(); | |
| }, 1000); | |
| }, 1000); | |
| }, [fetchNextPage]); | |
| const timer1 = setTimeout(() => { | |
| fetchNextPage(); | |
| const timer2 = setTimeout(() => { | |
| fetchNextPage(); | |
| }, 1000); | |
| }, 1000); | |
| return () => { | |
| clearTimeout(timer1); | |
| }; | |
| }, []); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,7 @@ function RouteComponent() { | |
|
|
||
| const { data, isLoading, isError } = useEntities(Podcast, { | ||
| mode: 'public', | ||
| first: 6, | ||
| offset: 0, | ||
| first: 100, | ||
|
||
| space: space, | ||
| include: { | ||
| projects: {}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||||
| import { Constants, Entity, Utils } from '@graphprotocol/hypergraph'; | ||||||||
| import { useInfiniteQuery as useInfiniteQueryTanstack } from '@tanstack/react-query'; | ||||||||
| import * as Option from 'effect/Option'; | ||||||||
| import type * as Schema from 'effect/Schema'; | ||||||||
| import * as SchemaAST from 'effect/SchemaAST'; | ||||||||
| import type { QueryPublicParams } from '../internal/types.js'; | ||||||||
| import { useHypergraphSpaceInternal } from '../internal/use-hypergraph-space-internal.js'; | ||||||||
|
|
||||||||
| export const useEntitiesPublicInfinite = <S extends Schema.Schema.AnyNoContext>( | ||||||||
| type: S, | ||||||||
| params?: QueryPublicParams<S>, | ||||||||
| ) => { | ||||||||
| const { enabled = true, filter, include, space: spaceFromParams, first = 2, offset = 0 } = params ?? {}; | ||||||||
| const { space: spaceFromContext } = useHypergraphSpaceInternal(); | ||||||||
| const space = spaceFromParams ?? spaceFromContext; | ||||||||
|
|
||||||||
| // constructing the relation type ids for the query | ||||||||
| const relationTypeIds = Utils.getRelationTypeIds(type, include); | ||||||||
|
|
||||||||
| const typeIds = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(type.ast as SchemaAST.TypeLiteral).pipe( | ||||||||
| Option.getOrElse(() => []), | ||||||||
| ); | ||||||||
|
|
||||||||
| const result = useInfiniteQueryTanstack({ | ||||||||
| queryKey: [ | ||||||||
| 'hypergraph-public-entities', | ||||||||
| space, | ||||||||
| typeIds, | ||||||||
| relationTypeIds.level1, | ||||||||
| relationTypeIds.level2, | ||||||||
| filter, | ||||||||
|
||||||||
| filter, | |
| filter, | |
| first, |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getNextPageParam implementation has a bug. It calculates the next offset as offset + pages.length * first, but this doesn't account for the initial offset correctly when paginating.
For example:
- If
offset = 0andfirst = 2, after fetching page 1 (pages.length = 1), the next offset would be0 + 1 * 2 = 2✓ - But if
offset = 10andfirst = 2, after fetching page 1, the next offset would be10 + 1 * 2 = 12, which skips records 10-11
The calculation should be based on the initial page param, not the initial offset. Consider: pageParam + first or track cumulative records fetched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changeset description has a typo - there's an extra closing bracket
]at the end of "add useEntitiesPublicInfinite hook]".