Skip to content

Commit 283aaf0

Browse files
committed
fix fetching nested relations
1 parent 7f54e9b commit 283aaf0

File tree

4 files changed

+34
-60
lines changed

4 files changed

+34
-60
lines changed

apps/events/src/components/create-events.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const createEvents = async ({
4747
});
4848
ops.push(...createCompanyTypeOps);
4949

50+
console.log('companyTypeId', companyTypeId);
51+
5052
const { ops: createEventTypeOps, id: eventTypeId } = Graph.createEntity({
5153
name: 'My Test Event',
5254
types: mapping.Event.typeIds,

apps/events/src/components/playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const Playground = () => {
4747
const { success, cid, txResult } = await createEntity(
4848
{
4949
name: 'Test Event 42 by Nik',
50-
sponsors: ['347676a1-7cef-47dc-b6a7-c94fc6237dcd'],
50+
sponsors: [],
5151
},
5252
{ walletClient },
5353
);

apps/events/src/mapping.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ import type { Mapping } from '@graphprotocol/hypergraph';
33

44
export const mapping: Mapping = {
55
Event: {
6-
typeIds: [Id.Id('407d9e8a-c703-4fb4-830d-98c758c8564e')],
6+
typeIds: [Id.Id('7f9562d4-034d-4385-bf5c-f02cdebba47a')],
77
properties: {
88
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
99
},
1010
relations: {
11-
sponsors: Id.Id('a7ac80a6-d3d9-4b04-9b9f-ead1723af09f'),
11+
sponsors: Id.Id('6860bfac-f703-4289-b789-972d0aaf3abe'),
1212
},
1313
},
1414
Company: {
15-
typeIds: [Id.Id('b0220a78-9205-4e5e-9bf1-c03ee0791e23')],
15+
typeIds: [Id.Id('6c504df5-1a8f-43d1-bf2d-1ef9fa5b08b5')],
1616
properties: {
1717
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
1818
},
1919
relations: {
20-
jobOffers: Id.Id('7ca8063c-3664-479b-912d-1b3b86af2bf4'),
20+
jobOffers: Id.Id('1203064e-9741-4235-89d4-97f4b22eddfb'),
2121
},
2222
},
2323
JobOffer: {
24-
typeIds: [Id.Id('99e1733b-661d-4edb-a253-98ff4b7747d0')],
24+
typeIds: [Id.Id('f60585af-71b6-4674-9a26-b74ca6c1cceb')],
2525
properties: {
2626
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
27-
salary: Id.Id('5ecfb4e5-09eb-437d-9c3c-e9e7395d52aa'),
27+
salary: Id.Id('baa36ac9-78ac-4cf7-8394-6b2d3006bebe'),
2828
},
2929
},
3030

packages/hypergraph-react/src/internal/use-query-public.tsx

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!) {
1414
entities(
1515
filter: {
1616
relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}},
17-
first: 100
1817
) {
1918
id
2019
name
21-
valuesList {
20+
valuesList(filter: {spaceId: {is: $spaceId}}) {
2221
propertyId
2322
value
2423
}
@@ -37,21 +36,17 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUI
3736
value
3837
}
3938
relationsList(
40-
filter: {spaceId: {is: $spaceId}, toEntity: {relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $relationTypeIdsLevel1}}}}},
41-
first: 100
39+
filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel1}},
4240
) {
4341
toEntity {
4442
id
4543
name
46-
valuesList {
44+
valuesList(filter: {spaceId: {is: $spaceId}}) {
4745
propertyId
4846
value
4947
}
5048
}
51-
type {
52-
id
53-
name
54-
}
49+
typeId
5550
}
5651
}
5752
}
@@ -60,27 +55,26 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUI
6055
const entitiesQueryDocumentLevel2 = gql`
6156
query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUID!]!, $relationTypeIdsLevel2: [UUID!]!) {
6257
entities(filter: {
63-
relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}},
64-
first: 100
65-
) {
58+
relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $typeIds}}}, spaceIds: {in: [$spaceId]}}) {
6659
id
6760
name
6861
valuesList(filter: {spaceId: {is: $spaceId}}) {
6962
propertyId
7063
value
7164
}
7265
relationsList(
73-
filter: {spaceId: {is: $spaceId}, toEntity: {relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $relationTypeIdsLevel1}}}}}
66+
filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel1}},
7467
) {
7568
toEntity {
7669
id
7770
name
78-
valuesList {
71+
valuesList(filter: {spaceId: {is: $spaceId}}) {
7972
propertyId
8073
value
8174
}
8275
relationsList(
83-
filter: {spaceId: {is: $spaceId}, toEntity: {relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $relationTypeIdsLevel2}}}}}
76+
filter: {spaceId: {is: $spaceId}, typeId:{ in: $relationTypeIdsLevel2}},
77+
# filter: {spaceId: {is: $spaceId}, toEntity: {relations: {some: {typeId: {is: "8f151ba4-de20-4e3c-9cb4-99ddf96f48f1"}, toEntityId: {in: $relationTypeIdsLevel2}}}}}
8478
) {
8579
toEntity {
8680
id
@@ -90,16 +84,10 @@ query entities($spaceId: UUID!, $typeIds: [UUID!]!, $relationTypeIdsLevel1: [UUI
9084
value
9185
}
9286
}
93-
type {
94-
id
95-
name
96-
}
87+
typeId
9788
}
9889
}
99-
type {
100-
id
101-
name
102-
}
90+
typeId
10391
}
10492
}
10593
}
@@ -114,36 +102,26 @@ type EntityQueryResult = {
114102
value: string;
115103
}[];
116104
relationsList: {
117-
to: {
105+
toEntity: {
118106
id: string;
119107
name: string;
120108
valuesList: {
121109
propertyId: string;
122110
value: string;
123111
}[];
124112
relationsList: {
125-
to: {
113+
toEntity: {
126114
id: string;
127115
name: string;
128116
valuesList: {
129117
propertyId: string;
130118
value: string;
131119
}[];
132120
};
133-
type: {
134-
id: string;
135-
entity: {
136-
name: string;
137-
};
138-
};
121+
typeId: string;
139122
}[];
140123
};
141-
type: {
142-
id: string;
143-
entity: {
144-
name: string;
145-
};
146-
};
124+
typeId: string;
147125
}[];
148126
}[];
149127
};
@@ -160,13 +138,8 @@ type RecursiveQueryEntity = {
160138
value: string;
161139
}[];
162140
relationsList?: {
163-
to: RecursiveQueryEntity;
164-
type: {
165-
id: string;
166-
entity: {
167-
name: string;
168-
};
169-
};
141+
toEntity: RecursiveQueryEntity;
142+
typeId: string;
170143
}[];
171144
};
172145

@@ -202,7 +175,7 @@ const convertRelations = <S extends Entity.AnyNoContext>(
202175
const rawEntity: Record<string, string | boolean | number | unknown[] | URL | Date> = {};
203176

204177
for (const [key, relationId] of Object.entries(mappingEntry?.relations ?? {})) {
205-
const properties = (queryEntity.relationsList ?? []).filter((a) => a.type?.id === relationId);
178+
const properties = (queryEntity.relationsList ?? []).filter((a) => a.typeId === relationId);
206179
if (properties.length === 0) {
207180
rawEntity[key] = [] as unknown[];
208181
continue;
@@ -230,30 +203,29 @@ const convertRelations = <S extends Entity.AnyNoContext>(
230203
}
231204

232205
const newRelationEntities = properties.map((propertyEntry) => {
206+
// @ts-expect-error TODO: properly access the type.name
207+
const type = field.value;
208+
233209
let rawEntity: Record<string, string | boolean | number | unknown[] | URL | Date> = {
234-
id: propertyEntry.to.id,
235-
name: propertyEntry.to.name,
236-
type: propertyEntry.type.id,
210+
id: propertyEntry.toEntity.id,
211+
name: propertyEntry.toEntity.name,
237212
// TODO: should be determined by the actual value
238213
__deleted: false,
239214
// TODO: should be determined by the actual value
240215
__version: '',
241216
};
242217

243-
// @ts-expect-error TODO: properly access the type.name
244-
const type = field.value;
245-
246218
// take the mappingEntry and assign the attributes to the rawEntity
247219
for (const [key, value] of Object.entries(relationMappingEntry?.properties ?? {})) {
248-
const property = propertyEntry.to.valuesList?.find((a) => a.propertyId === value);
220+
const property = propertyEntry.toEntity.valuesList?.find((a) => a.propertyId === value);
249221
if (property) {
250222
rawEntity[key] = convertPropertyValue(property, key, type);
251223
}
252224
}
253225

254226
rawEntity = {
255227
...rawEntity,
256-
...convertRelations(propertyEntry.to, type, relationMappingEntry, mapping),
228+
...convertRelations(propertyEntry.toEntity, type, relationMappingEntry, mapping),
257229
};
258230

259231
return rawEntity;

0 commit comments

Comments
 (0)