@@ -12,6 +12,7 @@ export type RelationTypeIdInfo = {
1212 listField : RelationListField ;
1313 includeNodes : boolean ;
1414 includeTotalCount : boolean ;
15+ targetTypeIds ?: readonly string [ ] ;
1516 relationSpaces ?: RelationSpacesOverride ;
1617 valueSpaces ?: RelationSpacesOverride ;
1718 children ?: RelationTypeIdInfo [ ] ;
@@ -23,6 +24,35 @@ const isRelationIncludeBranch = (value: unknown): value is RelationIncludeBranch
2324const hasTotalCountFlag = ( include : Record < string , unknown > | undefined , key : string ) =>
2425 Boolean ( include ?. [ `${ key } TotalCount` ] ) ;
2526
27+ const getRelationTupleType = ( ast : SchemaAST . AST ) : SchemaAST . TupleType | undefined => {
28+ if ( SchemaAST . isTupleType ( ast ) ) {
29+ return ast ;
30+ }
31+ if ( SchemaAST . isUnion ( ast ) ) {
32+ for ( const member of ast . types ) {
33+ if ( SchemaAST . isTupleType ( member ) ) {
34+ return member ;
35+ }
36+ }
37+ }
38+ return undefined ;
39+ } ;
40+
41+ const getRelationTargetTypeIds = ( relationType : SchemaAST . AST ) => {
42+ const tupleType = getRelationTupleType ( relationType ) ;
43+ if ( ! tupleType ) {
44+ return undefined ;
45+ }
46+ const relationTransformation = tupleType . rest [ 0 ] ?. type ;
47+ if ( ! relationTransformation || ! SchemaAST . isTypeLiteral ( relationTransformation ) ) {
48+ return undefined ;
49+ }
50+ const typeIds = SchemaAST . getAnnotation < string [ ] > ( Constants . TypeIdsSymbol ) ( relationTransformation ) . pipe (
51+ Option . getOrElse ( ( ) => [ ] ) ,
52+ ) ;
53+ return typeIds . length > 0 ? ( typeIds as readonly string [ ] ) : undefined ;
54+ } ;
55+
2656export const getRelationTypeIds = < S extends Schema . Schema . AnyNoContext > (
2757 type : S ,
2858 include : EntityInclude < S > | undefined ,
@@ -53,12 +83,15 @@ export const getRelationTypeIds = <S extends Schema.Schema.AnyNoContext>(
5383 const relationSpaces = includeBranch ?. _config ?. relationSpaces ;
5484 const valueSpaces = includeBranch ?. _config ?. valueSpaces ;
5585
86+ const targetTypeIds = getRelationTargetTypeIds ( prop . type ) ;
87+
5688 const level1InfoBase : RelationTypeIdInfo = {
5789 typeId : result . value ,
5890 propertyName,
5991 listField,
6092 includeNodes,
6193 includeTotalCount,
94+ ...( targetTypeIds ? { targetTypeIds } : { } ) ,
6295 } ;
6396 const level1Info : RelationTypeIdInfo =
6497 relationSpaces === undefined && valueSpaces === undefined
@@ -70,19 +103,17 @@ export const getRelationTypeIds = <S extends Schema.Schema.AnyNoContext>(
70103 } ;
71104 const nestedRelations : RelationTypeIdInfo [ ] = [ ] ;
72105
73- if ( ! SchemaAST . isTupleType ( prop . type ) ) {
106+ const relationTuple = getRelationTupleType ( prop . type ) ;
107+ if ( ! relationTuple ) {
74108 relationInfo . push ( level1Info ) ;
75109 continue ;
76110 }
77- const relationTransformation = prop . type . rest [ 0 ] ?. type ;
111+ const relationTransformation = relationTuple . rest [ 0 ] ?. type ;
78112 if ( ! relationTransformation || ! SchemaAST . isTypeLiteral ( relationTransformation ) ) {
79113 relationInfo . push ( level1Info ) ;
80114 continue ;
81115 }
82- const typeIds2 : string [ ] = SchemaAST . getAnnotation < string [ ] > ( Constants . TypeIdsSymbol ) (
83- relationTransformation ,
84- ) . pipe ( Option . getOrElse ( ( ) => [ ] ) ) ;
85- if ( typeIds2 . length === 0 ) {
116+ if ( ! targetTypeIds || targetTypeIds . length === 0 ) {
86117 relationInfo . push ( level1Info ) ;
87118 continue ;
88119 }
@@ -109,12 +140,15 @@ export const getRelationTypeIds = <S extends Schema.Schema.AnyNoContext>(
109140 const nestedListField : RelationListField = nestedIsBacklink ? 'backlinks' : 'relations' ;
110141 const nestedRelationSpaces = nestedIncludeBranch ?. _config ?. relationSpaces ;
111142 const nestedValueSpaces = nestedIncludeBranch ?. _config ?. valueSpaces ;
143+ const nestedTargetTypeIds = getRelationTargetTypeIds ( nestedProp . type ) ;
144+
112145 const nestedInfoBase : RelationTypeIdInfo = {
113146 typeId : nestedResult . value ,
114147 propertyName : nestedPropertyName ,
115148 listField : nestedListField ,
116149 includeNodes : nestedIncludeNodes ,
117150 includeTotalCount : nestedIncludeTotalCount ,
151+ ...( nestedTargetTypeIds ? { targetTypeIds : nestedTargetTypeIds } : { } ) ,
118152 } ;
119153 const nestedInfo : RelationTypeIdInfo =
120154 nestedRelationSpaces === undefined && nestedValueSpaces === undefined
0 commit comments