@@ -6,6 +6,7 @@ import type * as ParseResult from 'effect/ParseResult';
66import * as Schema from 'effect/Schema' ;
77import * as SchemaAST from 'effect/SchemaAST' ;
88import { request } from 'graphql-request' ;
9+ import { normalizeSpaceIds } from './internal/normalize-space-ids.js' ;
910import type { InvalidRelationEntity , RelationsListWithNodes } from '../utils/convert-relations.js' ;
1011import type { RelationTypeIdInfo } from '../utils/get-relation-type-ids.js' ;
1112import { buildRelationsSelection } from '../utils/relation-query-helpers.js' ;
@@ -26,15 +27,18 @@ export type FindManyPublicParams<S extends Schema.Schema.AnyNoContext> = SpaceSe
2627 }
2728 | undefined ;
2829 backlinksTotalCountsTypeId1 ?: string | undefined ;
30+ includeSpaceIds ?: boolean | undefined ;
2931 logInvalidResults ?: boolean | undefined ;
3032} ;
3133
3234const buildEntitiesQuery = (
3335 relationInfoLevel1 : RelationTypeIdInfo [ ] ,
3436 useOrderBy : boolean ,
3537 spaceSelection : SpaceSelection ,
38+ includeSpaceIds : boolean ,
3639) => {
3740 const level1Relations = buildRelationsSelection ( relationInfoLevel1 , spaceSelection . mode ) ;
41+ const spaceIdsSelection = includeSpaceIds ? '\n spaceIds' : '' ;
3842
3943 const queryName = useOrderBy ? 'entitiesOrderedByProperty' : 'entities' ;
4044 const variableDefinitions = [
@@ -86,7 +90,7 @@ query ${queryName}(${variableDefinitions}) {
8690 offset: $offset
8791 ) {
8892 id
89- name
93+ name${ spaceIdsSelection }
9094 valuesList${ valuesListFilter } {
9195 propertyId
9296 string
@@ -123,6 +127,7 @@ export type EntityQueryResult = {
123127 entities : ( {
124128 id : string ;
125129 name : string ;
130+ spaceIds : readonly ( string | null ) [ ] | null ;
126131 valuesList : ValuesList ;
127132 backlinksTotalCountsTypeId1 : {
128133 totalCount : number ;
@@ -139,7 +144,9 @@ export const parseResult = <S extends Schema.Schema.AnyNoContext>(
139144 queryData : EntityQueryResult ,
140145 type : S ,
141146 relationInfoLevel1 : RelationTypeIdInfo [ ] ,
147+ options ?: { includeSpaceIds ?: boolean } ,
142148) => {
149+ const includeSpaceIds = options ?. includeSpaceIds ?? false ;
143150 const schemaWithId = Utils . addIdSchemaField ( type ) ;
144151 const decode = Schema . decodeUnknownEither ( schemaWithId ) ;
145152 const data : ( Entity . Entity < S > & { backlinksTotalCountsTypeId1 ?: number } ) [ ] = [ ] ;
@@ -192,14 +199,20 @@ export const parseResult = <S extends Schema.Schema.AnyNoContext>(
192199 } ) ;
193200
194201 if ( Either . isRight ( decodeResult ) ) {
202+ const normalizedSpaceIds = includeSpaceIds ? normalizeSpaceIds ( queryEntity . spaceIds ) : undefined ;
195203 // injecting the schema to the entity to be able to access it in the preparePublish function
196204 data . push ( {
197205 ...decodeResult . right ,
206+ ...( includeSpaceIds ? { spaceIds : normalizedSpaceIds } : { } ) ,
198207 __schema : type ,
199208 backlinksTotalCountsTypeId1 : queryEntity . backlinksTotalCountsTypeId1 ?. totalCount ,
200209 } ) ;
201210 } else {
202- invalidEntities . push ( { raw : rawEntity , error : decodeResult . left } ) ;
211+ const normalizedSpaceIds = includeSpaceIds ? normalizeSpaceIds ( queryEntity . spaceIds ) : undefined ;
212+ invalidEntities . push ( {
213+ raw : includeSpaceIds ? { ...rawEntity , spaceIds : normalizedSpaceIds } : rawEntity ,
214+ error : decodeResult . left ,
215+ } ) ;
203216 }
204217 }
205218 return { data, invalidEntities, invalidRelationEntities } ;
@@ -218,6 +231,7 @@ export const findManyPublic = async <S extends Schema.Schema.AnyNoContext>(
218231 offset = 0 ,
219232 orderBy,
220233 backlinksTotalCountsTypeId1,
234+ includeSpaceIds = false ,
221235 logInvalidResults = true ,
222236 } = params ?? { } ;
223237
@@ -259,7 +273,7 @@ export const findManyPublic = async <S extends Schema.Schema.AnyNoContext>(
259273 const spaceSelection = normalizeSpaceSelection ( space , spaces ) ;
260274
261275 // Build the query dynamically with aliases for each relation type ID
262- const queryDocument = buildEntitiesQuery ( relationTypeIds , Boolean ( orderBy ) , spaceSelection ) ;
276+ const queryDocument = buildEntitiesQuery ( relationTypeIds , Boolean ( orderBy ) , spaceSelection , includeSpaceIds ) ;
263277
264278 const filterParams = filter ? Utils . translateFilterToGraphql ( filter , type ) : { } ;
265279
@@ -290,7 +304,9 @@ export const findManyPublic = async <S extends Schema.Schema.AnyNoContext>(
290304
291305 const result = await request < EntityQueryResult > ( `${ Graph . TESTNET_API_ORIGIN } /graphql` , queryDocument , queryVariables ) ;
292306
293- const { data, invalidEntities, invalidRelationEntities } = parseResult ( result , type , relationTypeIds ) ;
307+ const { data, invalidEntities, invalidRelationEntities } = parseResult ( result , type , relationTypeIds , {
308+ includeSpaceIds,
309+ } ) ;
294310 if ( logInvalidResults ) {
295311 if ( invalidEntities . length > 0 ) {
296312 console . warn ( 'Entities where decoding failed were dropped' , invalidEntities ) ;
0 commit comments