11import { Graph } from '@graphprotocol/grc-20' ;
2- import { type Entity , type Mapping , store } from '@graphprotocol/hypergraph' ;
2+ import { Constants , type Entity } from '@graphprotocol/hypergraph' ;
33import { useQuery as useQueryTanstack } from '@tanstack/react-query' ;
4- import { useSelector } from '@xstate/store/react' ;
54import * as Either from 'effect/Either' ;
5+ import * as Option from 'effect/Option' ;
66import * as Schema from 'effect/Schema' ;
7+ import * as SchemaAST from 'effect/SchemaAST' ;
78import { gql , request } from 'graphql-request' ;
89import { useMemo } from 'react' ;
910import { convertPropertyValue } from './convert-property-value.js' ;
1011import { convertRelations } from './convert-relations.js' ;
12+ import { getRelationTypeIds } from './get-relation-type-ids.js' ;
1113import { useHypergraphSpaceInternal } from './use-hypergraph-space-internal.js' ;
1214
1315const entityQueryDocumentLevel0 = gql `
@@ -47,6 +49,7 @@ query entity($id: UUID!, $spaceId: UUID!, $relationTypeIdsLevel1: [UUID!]!) {
4749 relationsList(
4850 filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel1}},
4951 ) {
52+ id
5053 toEntity {
5154 id
5255 name
@@ -83,6 +86,7 @@ query entity($id: UUID!, $spaceId: UUID!, $relationTypeIdsLevel1: [UUID!]!, $rel
8386 relationsList(
8487 filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel1}},
8588 ) {
89+ id
8690 toEntity {
8791 id
8892 name
@@ -97,6 +101,7 @@ query entity($id: UUID!, $spaceId: UUID!, $relationTypeIdsLevel1: [UUID!]!, $rel
97101 relationsList(
98102 filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel2}},
99103 ) {
104+ id
100105 toEntity {
101106 id
102107 name
@@ -131,6 +136,7 @@ type EntityQueryResult = {
131136 point : string ;
132137 } [ ] ;
133138 relationsList ?: {
139+ id : string ;
134140 toEntity : {
135141 id : string ;
136142 name : string ;
@@ -143,6 +149,7 @@ type EntityQueryResult = {
143149 point : string ;
144150 } [ ] ;
145151 relationsList ?: {
152+ id : string ;
146153 toEntity : {
147154 id : string ;
148155 name : string ;
@@ -163,41 +170,51 @@ type EntityQueryResult = {
163170 } | null ;
164171} ;
165172
166- export const parseResult = < S extends Schema . Schema . AnyNoContext > (
167- queryData : EntityQueryResult ,
168- type : S ,
169- mappingEntry : Mapping . MappingEntry ,
170- mapping : Mapping . Mapping ,
171- ) => {
173+ export const parseResult = < S extends Schema . Schema . AnyNoContext > ( queryData : EntityQueryResult , type : S ) => {
172174 if ( ! queryData . entity ) {
173175 return { data : null , invalidEntity : null } ;
174176 }
175177
176- const decode = Schema . decodeUnknownEither ( type ) ;
178+ const schemaWithId = Schema . extend ( Schema . Struct ( { id : Schema . String } ) ) ( type ) ;
179+ const decode = Schema . decodeUnknownEither ( schemaWithId ) ;
177180 const queryEntity = queryData . entity ;
178181 let rawEntity : Record < string , string | boolean | number | unknown [ ] | Date > = {
179182 id : queryEntity . id ,
180183 } ;
181184
182- // take the mappingEntry and assign the attributes to the rawEntity
183- for ( const [ key , value ] of Object . entries ( mappingEntry ?. properties ?? { } ) ) {
184- const property = queryEntity . valuesList . find ( ( a ) => a . propertyId === value ) ;
185- if ( property ) {
186- rawEntity [ key ] = convertPropertyValue ( property , type ) ;
185+ const ast = type . ast as SchemaAST . TypeLiteral ;
186+
187+ for ( const prop of ast . propertySignatures ) {
188+ const propType = prop . isOptional ? prop . type . types [ 0 ] : prop . type ;
189+ const result = SchemaAST . getAnnotation < string > ( Constants . PropertyIdSymbol ) ( propType ) ;
190+
191+ if ( Option . isSome ( result ) ) {
192+ const value = queryEntity . valuesList . find ( ( a ) => a . propertyId === result . value ) ;
193+ if ( value ) {
194+ const rawValue = convertPropertyValue ( value , propType ) ;
195+ if ( rawValue ) {
196+ rawEntity [ String ( prop . name ) ] = rawValue ;
197+ }
198+ }
187199 }
188200 }
189201
190202 rawEntity = {
191203 ...rawEntity ,
192- ...convertRelations ( queryEntity , type , mappingEntry , mapping ) ,
204+ ...convertRelations ( queryEntity , ast ) ,
193205 } ;
194206
207+ console . log ( 'rawEntity' , rawEntity ) ;
208+
195209 const decodeResult = decode ( {
196210 ...rawEntity ,
197211 __deleted : false ,
212+ // __version: queryEntity.currentVersion.versionId,
198213 __version : '' ,
199214 } ) ;
200215
216+ console . log ( 'decodeResult' , decodeResult ) ;
217+
201218 if ( Either . isRight ( decodeResult ) ) {
202219 return {
203220 data : { ...decodeResult . right , __schema : type } as Entity . Entity < S > ,
@@ -220,61 +237,42 @@ export const useEntityPublic = <S extends Schema.Schema.AnyNoContext>(type: S, p
220237 const { id, enabled = true , space : spaceFromParams , include } = params ;
221238 const { space : spaceFromContext } = useHypergraphSpaceInternal ( ) ;
222239 const space = spaceFromParams ?? spaceFromContext ;
223- const mapping = useSelector ( store , ( state ) => state . context . mapping ) ;
224-
225- // @ts -expect-error TODO should use the actual type instead of the name in the mapping
226- const typeName = type . name ;
227- const mappingEntry = mapping ?. [ typeName ] ;
228- if ( enabled && ! mappingEntry ) {
229- throw new Error ( `Mapping entry for ${ typeName } not found` ) ;
230- }
231240
232241 // constructing the relation type ids for the query
233- const relationTypeIdsLevel1 : string [ ] = [ ] ;
234- const relationTypeIdsLevel2 : string [ ] = [ ] ;
235- for ( const key in mappingEntry ?. relations ?? { } ) {
236- if ( include ?. [ key ] && mappingEntry ?. relations ?. [ key ] ) {
237- relationTypeIdsLevel1 . push ( mappingEntry ?. relations ?. [ key ] ) ;
238- const field = type . fields [ key ] ;
239- // @ts -expect-error TODO find a better way to access the relation type name
240- const typeName2 = field . value . name ;
241- const mappingEntry2 = mapping [ typeName2 ] ;
242- for ( const key2 in mappingEntry2 ?. relations ?? { } ) {
243- if ( include ?. [ key ] [ key2 ] && mappingEntry2 ?. relations ?. [ key2 ] ) {
244- relationTypeIdsLevel2 . push ( mappingEntry2 ?. relations ?. [ key2 ] ) ;
245- }
246- }
247- }
248- }
242+ const relationTypeIds = getRelationTypeIds ( type , include ) ;
243+
244+ const typeIds = SchemaAST . getAnnotation < string [ ] > ( Constants . TypeIdsSymbol ) ( type . ast as SchemaAST . TypeLiteral ) . pipe (
245+ Option . getOrElse ( ( ) => [ ] ) ,
246+ ) ;
249247
250248 const result = useQueryTanstack ( {
251- queryKey : [ 'hypergraph-public-entity' , typeName , id , space , relationTypeIdsLevel1 , relationTypeIdsLevel2 , include ] ,
249+ queryKey : [ 'hypergraph-public-entity' , id , typeIds , space , relationTypeIds . level1 , relationTypeIds . level2 , include ] ,
252250 queryFn : async ( ) => {
253251 let queryDocument = entityQueryDocumentLevel0 ;
254- if ( relationTypeIdsLevel1 . length > 0 ) {
252+ if ( relationTypeIds . level1 . length > 0 ) {
255253 queryDocument = entityQueryDocumentLevel1 ;
256254 }
257- if ( relationTypeIdsLevel2 . length > 0 ) {
255+ if ( relationTypeIds . level2 . length > 0 ) {
258256 queryDocument = entityQueryDocumentLevel2 ;
259257 }
260258
261259 const result = await request < EntityQueryResult > ( `${ Graph . TESTNET_API_ORIGIN } /graphql` , queryDocument , {
262260 id,
263261 spaceId : space ,
264- relationTypeIdsLevel1,
265- relationTypeIdsLevel2,
262+ relationTypeIdsLevel1 : relationTypeIds . level1 ,
263+ relationTypeIdsLevel2 : relationTypeIds . level2 ,
266264 } ) ;
267265 return result ;
268266 } ,
269267 enabled : enabled && ! ! id && ! ! space ,
270268 } ) ;
271269
272270 const { data, invalidEntity } = useMemo ( ( ) => {
273- if ( result . data && mappingEntry ) {
274- return parseResult ( result . data , type , mappingEntry , mapping ) ;
271+ if ( result . data ) {
272+ return parseResult ( result . data , type ) ;
275273 }
276274 return { data : null , invalidEntity : null } ;
277- } , [ result . data , type , mappingEntry , mapping ] ) ;
275+ } , [ result . data , type ] ) ;
278276
279277 return { ...result , data, invalidEntity } ;
280278} ;
0 commit comments