From 5af093af04004faa310924f71d3b20b890dcdad0 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Fri, 4 Jul 2025 16:10:31 +0200 Subject: [PATCH 1/2] add first filter --- apps/events/src/components/playground.tsx | 1 + .../hypergraph-react/src/internal/types.ts | 1 + .../src/internal/use-query-public.tsx | 21 +++++++++++++------ packages/hypergraph-react/src/use-query.tsx | 5 +++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/events/src/components/playground.tsx b/apps/events/src/components/playground.tsx index 03c7b49f..cd266c40 100644 --- a/apps/events/src/components/playground.tsx +++ b/apps/events/src/components/playground.tsx @@ -17,6 +17,7 @@ export const Playground = () => { jobOffers: {}, }, }, + first: 2, }); const [isDeleting, setIsDeleting] = useState(false); const [isCreating, setIsCreating] = useState(false); diff --git a/packages/hypergraph-react/src/internal/types.ts b/packages/hypergraph-react/src/internal/types.ts index ee4c4f51..2c0e1972 100644 --- a/packages/hypergraph-react/src/internal/types.ts +++ b/packages/hypergraph-react/src/internal/types.ts @@ -5,4 +5,5 @@ export type QueryPublicParams = { enabled: boolean; // TODO: for multi-level nesting it should only allow the allowed properties instead of Record> include?: { [K in keyof Schema.Schema.Type]?: Record> } | undefined; + first?: number; }; diff --git a/packages/hypergraph-react/src/internal/use-query-public.tsx b/packages/hypergraph-react/src/internal/use-query-public.tsx index 40125c24..825e396e 100644 --- a/packages/hypergraph-react/src/internal/use-query-public.tsx +++ b/packages/hypergraph-react/src/internal/use-query-public.tsx @@ -10,10 +10,11 @@ import { GEO_API_TESTNET_ENDPOINT } from './constants.js'; import type { QueryPublicParams } from './types.js'; const entitiesQueryDocumentLevel0 = gql` -query entities($spaceId: UUID!, $typeIds: [UUID!]!) { +query entities($spaceId: UUID!, $typeIds: [UUID!]!, $first: Int) { entities( filter: { relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}}, + first: $first ) { id name @@ -26,8 +27,10 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!) { `; const entitiesQueryDocumentLevel1 = gql` -query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUID!]!) { - entities(filter: { +query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUID!]!, $first: Int) { + entities( + first: $first + filter: { relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}}) { id name @@ -53,8 +56,10 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUI `; const entitiesQueryDocumentLevel2 = gql` -query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUID!]!, $relationTypeIdsLevel2: [UUID!]!) { - entities(filter: { +query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUID!]!, $relationTypeIdsLevel2: [UUID!]!, $first: Int) { + entities( + first: $first + filter: { relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}}) { id name @@ -290,7 +295,7 @@ export const parseResult = ( }; export const useQueryPublic = (type: S, params?: QueryPublicParams) => { - const { enabled = true, include } = params ?? {}; + const { enabled = true, include, first = 100 } = params ?? {}; const { space } = useHypergraphSpaceInternal(); const mapping = useSelector(store, (state) => state.context.mapping); @@ -327,6 +332,7 @@ export const useQueryPublic = (type: S, params?: mappingEntry?.typeIds, relationTypeIdsLevel1, relationTypeIdsLevel2, + // TODO should `first` be in here? ], queryFn: async () => { let queryDocument = entitiesQueryDocumentLevel0; @@ -337,11 +343,14 @@ export const useQueryPublic = (type: S, params?: queryDocument = entitiesQueryDocumentLevel2; } + console.log('first', first); + const result = await request(GEO_API_TESTNET_ENDPOINT, queryDocument, { spaceId: space, typeIds: mappingEntry?.typeIds || [], relationTypeIdsLevel1, relationTypeIdsLevel2, + first, }); return result; }, diff --git a/packages/hypergraph-react/src/use-query.tsx b/packages/hypergraph-react/src/use-query.tsx index 97ed23a0..366ab742 100644 --- a/packages/hypergraph-react/src/use-query.tsx +++ b/packages/hypergraph-react/src/use-query.tsx @@ -10,6 +10,7 @@ type QueryParams = { // TODO: for multi-level nesting it should only allow the allowed properties instead of Record> include?: { [K in keyof Schema.Schema.Type]?: Record> } | undefined; space?: string; + first?: number; }; // @ts-expect-error TODO: remove this function @@ -144,8 +145,8 @@ const getDiff = ( const preparePublishDummy = () => undefined; export function useQuery(type: S, params: QueryParams) { - const { mode, filter, include, space } = params; - const publicResult = useQueryPublic(type, { enabled: mode === 'public', include }); + const { mode, filter, include, space, first } = params; + const publicResult = useQueryPublic(type, { enabled: mode === 'public', include, first }); const localResult = useQueryLocal(type, { enabled: mode === 'private', filter, include, space }); // const mapping = useSelector(store, (state) => state.context.mapping); // const generateUpdateOps = useGenerateUpdateOps(type, mode === 'merged'); From fc3ab37818bd83c8dbc518cffb4393fffd6f7e49 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Fri, 4 Jul 2025 16:14:47 +0200 Subject: [PATCH 2/2] cleanup --- packages/hypergraph-react/src/internal/types.ts | 2 +- packages/hypergraph-react/src/internal/use-query-public.tsx | 2 -- packages/hypergraph-react/src/use-query.tsx | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/hypergraph-react/src/internal/types.ts b/packages/hypergraph-react/src/internal/types.ts index 2c0e1972..f69f5589 100644 --- a/packages/hypergraph-react/src/internal/types.ts +++ b/packages/hypergraph-react/src/internal/types.ts @@ -5,5 +5,5 @@ export type QueryPublicParams = { enabled: boolean; // TODO: for multi-level nesting it should only allow the allowed properties instead of Record> include?: { [K in keyof Schema.Schema.Type]?: Record> } | undefined; - first?: number; + first?: number | undefined; }; diff --git a/packages/hypergraph-react/src/internal/use-query-public.tsx b/packages/hypergraph-react/src/internal/use-query-public.tsx index 825e396e..9efdf6d1 100644 --- a/packages/hypergraph-react/src/internal/use-query-public.tsx +++ b/packages/hypergraph-react/src/internal/use-query-public.tsx @@ -343,8 +343,6 @@ export const useQueryPublic = (type: S, params?: queryDocument = entitiesQueryDocumentLevel2; } - console.log('first', first); - const result = await request(GEO_API_TESTNET_ENDPOINT, queryDocument, { spaceId: space, typeIds: mappingEntry?.typeIds || [], diff --git a/packages/hypergraph-react/src/use-query.tsx b/packages/hypergraph-react/src/use-query.tsx index 366ab742..1ece799a 100644 --- a/packages/hypergraph-react/src/use-query.tsx +++ b/packages/hypergraph-react/src/use-query.tsx @@ -10,7 +10,7 @@ type QueryParams = { // TODO: for multi-level nesting it should only allow the allowed properties instead of Record> include?: { [K in keyof Schema.Schema.Type]?: Record> } | undefined; space?: string; - first?: number; + first?: number | undefined; }; // @ts-expect-error TODO: remove this function