@@ -9,7 +9,22 @@ import { useHypergraphSpace } from '../HypergraphSpaceContext.js';
99import { GEO_API_TESTNET_ENDPOINT } from './constants.js' ;
1010import type { QueryPublicParams } from './types.js' ;
1111
12- const entitiesQueryDocument = gql `
12+ const entitiesQueryDocumentLevel0 = gql `
13+ query entities($spaceId: String!, $typeIds: [String!]!) {
14+ entities(spaceId: $spaceId, filter: {
15+ types: { in: $typeIds }
16+ }) {
17+ id
18+ name
19+ values {
20+ propertyId
21+ value
22+ }
23+ }
24+ }
25+ ` ;
26+
27+ const entitiesQueryDocumentLevel1 = gql `
1328query entities($spaceId: String!, $typeIds: [String!]!, $relationTypeIdsLevel1: [String!]!) {
1429 entities(spaceId: $spaceId, filter: {
1530 types: { in: $typeIds }
@@ -30,26 +45,49 @@ query entities($spaceId: String!, $typeIds: [String!]!, $relationTypeIdsLevel1:
3045 propertyId
3146 value
3247 }
33- relations {
48+ }
49+ type {
50+ id
51+ entity {
52+ name
53+ }
54+ }
55+ }
56+ }
57+ }
58+ ` ;
59+
60+ const entitiesQueryDocumentLevel2 = gql `
61+ query entities($spaceId: String!, $typeIds: [String!]!, $relationTypeIdsLevel1: [String!]!, $relationTypeIdsLevel2: [String!]!) {
62+ entities(spaceId: $spaceId, filter: {
63+ types: { in: $typeIds }
64+ }) {
65+ id
66+ name
67+ values {
68+ propertyId
69+ value
70+ }
71+ relations(filter: {
72+ type: { in: $relationTypeIdsLevel1 }
73+ }) {
74+ to {
75+ id
76+ name
77+ values {
78+ propertyId
79+ value
80+ }
81+ relations(filter: {
82+ type: { in: $relationTypeIdsLevel2 }
83+ }) {
3484 to {
3585 id
3686 name
3787 values {
3888 propertyId
3989 value
4090 }
41- relations {
42- to {
43- id
44- name
45- }
46- type {
47- id
48- entity {
49- name
50- }
51- }
52- }
5391 }
5492 type {
5593 id
@@ -94,34 +132,6 @@ type EntityQueryResult = {
94132 propertyId : string ;
95133 value : string ;
96134 } [ ] ;
97- relations : {
98- to : {
99- id : string ;
100- name : string ;
101- values : {
102- propertyId : string ;
103- value : string ;
104- } [ ] ;
105- relations : {
106- to : {
107- id : string ;
108- name : string ;
109- } ;
110- type : {
111- id : string ;
112- entity : {
113- name : string ;
114- } ;
115- } ;
116- } [ ] ;
117- } ;
118- type : {
119- id : string ;
120- entity : {
121- name : string ;
122- } ;
123- } ;
124- } [ ] ;
125135 } ;
126136 type : {
127137 id : string ;
@@ -322,20 +332,47 @@ export const useQueryPublic = <S extends Entity.AnyNoContext>(type: S, params?:
322332 throw new Error ( `Mapping entry for ${ typeName } not found` ) ;
323333 }
324334
335+ // constructing the relation type ids for the query
325336 const relationTypeIdsLevel1 : string [ ] = [ ] ;
337+ const relationTypeIdsLevel2 : string [ ] = [ ] ;
326338 for ( const key in mappingEntry ?. relations ?? { } ) {
327339 if ( include ?. [ key ] && mappingEntry ?. relations ?. [ key ] ) {
328340 relationTypeIdsLevel1 . push ( mappingEntry ?. relations ?. [ key ] ) ;
341+ const field = type . fields [ key ] ;
342+ // @ts -expect-error TODO find a better way to access the relation type name
343+ const typeName2 = field . value . name ;
344+ const mappingEntry2 = mapping [ typeName2 ] ;
345+ for ( const key2 in mappingEntry2 ?. relations ?? { } ) {
346+ if ( include ?. [ key ] [ key2 ] && mappingEntry2 ?. relations ?. [ key2 ] ) {
347+ relationTypeIdsLevel2 . push ( mappingEntry2 ?. relations ?. [ key2 ] ) ;
348+ }
349+ }
329350 }
330351 }
331352
332353 const result = useQueryTanstack ( {
333- queryKey : [ 'hypergraph-public-entities' , typeName , space , mappingEntry ?. typeIds ] ,
354+ queryKey : [
355+ 'hypergraph-public-entities' ,
356+ typeName ,
357+ space ,
358+ mappingEntry ?. typeIds ,
359+ relationTypeIdsLevel1 ,
360+ relationTypeIdsLevel2 ,
361+ ] ,
334362 queryFn : async ( ) => {
335- const result = await request < EntityQueryResult > ( GEO_API_TESTNET_ENDPOINT , entitiesQueryDocument , {
363+ let queryDocument = entitiesQueryDocumentLevel0 ;
364+ if ( relationTypeIdsLevel1 . length > 0 ) {
365+ queryDocument = entitiesQueryDocumentLevel1 ;
366+ }
367+ if ( relationTypeIdsLevel2 . length > 0 ) {
368+ queryDocument = entitiesQueryDocumentLevel2 ;
369+ }
370+
371+ const result = await request < EntityQueryResult > ( GEO_API_TESTNET_ENDPOINT , queryDocument , {
336372 spaceId : space ,
337373 typeIds : mappingEntry ?. typeIds || [ ] ,
338374 relationTypeIdsLevel1,
375+ relationTypeIdsLevel2,
339376 } ) ;
340377 return result ;
341378 } ,
0 commit comments