Skip to content

Commit 6267464

Browse files
committed
fix types
1 parent 61ceb22 commit 6267464

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/hypergraph-react/src/internal/convert-relations.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,31 @@ type RecursiveQueryEntity = {
2626
}[];
2727
};
2828

29+
type RawEntityValue = string | boolean | number | unknown[] | Date | { id: string };
30+
type RawEntity = Record<string, RawEntityValue>;
31+
type NestedRawEntity = RawEntity & { _relation: { id: string } };
32+
2933
export const convertRelations = <S extends Schema.Schema.AnyNoContext>(
3034
queryEntity: RecursiveQueryEntity,
3135
ast: SchemaAST.TypeLiteral,
3236
) => {
33-
const rawEntity: Record<string, string | boolean | number | unknown[] | Date> = {};
37+
const rawEntity: RawEntity = {};
3438

3539
for (const prop of ast.propertySignatures) {
3640
const result = SchemaAST.getAnnotation<string>(Constants.PropertyIdSymbol)(prop.type);
3741

3842
if (Utils.isRelation(prop.type) && Option.isSome(result)) {
3943
rawEntity[String(prop.name)] = [];
4044

41-
const relationTransformation = prop.type.rest?.[0]?.type;
45+
if (!SchemaAST.isTupleType(prop.type)) {
46+
continue;
47+
}
48+
const relationType = prop.type;
49+
const relationTransformation = relationType.rest[0]?.type;
50+
if (!relationTransformation || !SchemaAST.isTypeLiteral(relationTransformation)) {
51+
continue;
52+
}
53+
4254
const typeIds: string[] = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(relationTransformation).pipe(
4355
Option.getOrElse(() => []),
4456
);
@@ -51,9 +63,7 @@ export const convertRelations = <S extends Schema.Schema.AnyNoContext>(
5163
);
5264
if (allRelationsWithTheCorrectPropertyTypeId) {
5365
for (const relationEntry of allRelationsWithTheCorrectPropertyTypeId) {
54-
let nestedRawEntity:
55-
| Record<string, string | boolean | number | unknown[] | Date>
56-
| { _relation: { id: string } } = {
66+
let nestedRawEntity: NestedRawEntity = {
5767
id: relationEntry.toEntity.id,
5868
_relation: {
5969
id: relationEntry.id,

packages/hypergraph-react/src/internal/get-relation-type-ids.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ export const getRelationTypeIds = (
1818
if (!Utils.isRelation(prop.type)) continue;
1919

2020
const result = SchemaAST.getAnnotation<string>(Constants.PropertyIdSymbol)(prop.type);
21-
if (Option.isSome(result) && include?.[prop.name]) {
21+
if (Option.isSome(result) && include?.[String(prop.name)]) {
2222
relationTypeIdsLevel1.push(result.value);
23-
const relationTransformation = prop.type.rest?.[0]?.type;
23+
if (!SchemaAST.isTupleType(prop.type)) {
24+
continue;
25+
}
26+
const relationTransformation = prop.type.rest[0]?.type;
27+
if (!relationTransformation || !SchemaAST.isTypeLiteral(relationTransformation)) {
28+
continue;
29+
}
2430
const typeIds2: string[] = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(
2531
relationTransformation,
2632
).pipe(Option.getOrElse(() => []));
@@ -31,7 +37,7 @@ export const getRelationTypeIds = (
3137
if (!Utils.isRelation(nestedProp.type)) continue;
3238

3339
const nestedResult = SchemaAST.getAnnotation<string>(Constants.PropertyIdSymbol)(nestedProp.type);
34-
if (Option.isSome(nestedResult) && include?.[prop.name][nestedProp.name]) {
40+
if (Option.isSome(nestedResult) && include?.[String(prop.name)]?.[String(nestedProp.name)]) {
3541
relationTypeIdsLevel2.push(nestedResult.value);
3642
}
3743
}

0 commit comments

Comments
 (0)