Skip to content

Commit b939d6b

Browse files
authored
allow to specify and retrieve relation entity values from the knowledge graph (#550)
1 parent f35bf7d commit b939d6b

14 files changed

Lines changed: 454 additions & 126 deletions

File tree

.changeset/crazy-beans-spend.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
"@graphprotocol/hypergraph-react": patch
3+
"@graphprotocol/hypergraph": patch
4+
---
5+
6+
Extend Entity.Schema to allow querying for relation entity values
7+
8+
In the following example we define a Project entity and a Podcast entity with the relation `projects` to the Project entity. In the Type.Relation you now can define the properties of the relation entity as a second argument. In the mapping you also need to define the properties of the relation entity instead of just the property id of the relation.
9+
10+
```ts
11+
export const Project = Entity.Schema(
12+
{
13+
name: Type.String,
14+
},
15+
{
16+
types: [Id('69732974-c632-490d-81a3-12ea567b2a8e')],
17+
properties: {
18+
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
19+
},
20+
},
21+
);
22+
23+
export const Podcast = Entity.Schema(
24+
{
25+
name: Type.String,
26+
projects: Type.Relation(Project, {
27+
properties: {
28+
website: Type.optional(Type.String),
29+
},
30+
}),
31+
},
32+
{
33+
types: [Id('69732974-c632-490d-81a3-12ea567b2a8e')],
34+
properties: {
35+
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
36+
projects: {
37+
propertyId: Id('71931b5f-1d6a-462e-81d9-5b8e85fb5c4b'),
38+
properties: {
39+
website: Id('eed38e74-e679-46bf-8a42-ea3e4f8fb5fb'),
40+
},
41+
},
42+
},
43+
},
44+
);
45+
```

apps/events/src/routeTree.gen.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ import { Route as SpaceSpaceIdPlaygroundRouteImport } from './routes/space/$spac
2323
import { Route as SpaceSpaceIdEventsRouteImport } from './routes/space/$spaceId/events'
2424
import { Route as SpaceSpaceIdChatRouteImport } from './routes/space/$spaceId/chat'
2525

26+
const PodcastsLazyRouteImport = createFileRoute('/podcasts')()
2627
const PlaygroundLazyRouteImport = createFileRoute('/playground')()
2728
const LoginLazyRouteImport = createFileRoute('/login')()
2829

30+
const PodcastsLazyRoute = PodcastsLazyRouteImport.update({
31+
id: '/podcasts',
32+
path: '/podcasts',
33+
getParentRoute: () => rootRouteImport,
34+
} as any).lazy(() => import('./routes/podcasts.lazy').then((d) => d.Route))
2935
const PlaygroundLazyRoute = PlaygroundLazyRouteImport.update({
3036
id: '/playground',
3137
path: '/playground',
@@ -98,6 +104,7 @@ export interface FileRoutesByFullPath {
98104
'/authenticate-success': typeof AuthenticateSuccessRoute
99105
'/login': typeof LoginLazyRoute
100106
'/playground': typeof PlaygroundLazyRoute
107+
'/podcasts': typeof PodcastsLazyRoute
101108
'/account-inbox/$inboxId': typeof AccountInboxInboxIdRoute
102109
'/friends/$accountAddress': typeof FriendsAccountAddressRoute
103110
'/space/$spaceId': typeof SpaceSpaceIdRouteWithChildren
@@ -113,6 +120,7 @@ export interface FileRoutesByTo {
113120
'/authenticate-success': typeof AuthenticateSuccessRoute
114121
'/login': typeof LoginLazyRoute
115122
'/playground': typeof PlaygroundLazyRoute
123+
'/podcasts': typeof PodcastsLazyRoute
116124
'/account-inbox/$inboxId': typeof AccountInboxInboxIdRoute
117125
'/friends/$accountAddress': typeof FriendsAccountAddressRoute
118126
'/space/$spaceId/chat': typeof SpaceSpaceIdChatRoute
@@ -128,6 +136,7 @@ export interface FileRoutesById {
128136
'/authenticate-success': typeof AuthenticateSuccessRoute
129137
'/login': typeof LoginLazyRoute
130138
'/playground': typeof PlaygroundLazyRoute
139+
'/podcasts': typeof PodcastsLazyRoute
131140
'/account-inbox/$inboxId': typeof AccountInboxInboxIdRoute
132141
'/friends/$accountAddress': typeof FriendsAccountAddressRoute
133142
'/space/$spaceId': typeof SpaceSpaceIdRouteWithChildren
@@ -145,6 +154,7 @@ export interface FileRouteTypes {
145154
| '/authenticate-success'
146155
| '/login'
147156
| '/playground'
157+
| '/podcasts'
148158
| '/account-inbox/$inboxId'
149159
| '/friends/$accountAddress'
150160
| '/space/$spaceId'
@@ -160,6 +170,7 @@ export interface FileRouteTypes {
160170
| '/authenticate-success'
161171
| '/login'
162172
| '/playground'
173+
| '/podcasts'
163174
| '/account-inbox/$inboxId'
164175
| '/friends/$accountAddress'
165176
| '/space/$spaceId/chat'
@@ -174,6 +185,7 @@ export interface FileRouteTypes {
174185
| '/authenticate-success'
175186
| '/login'
176187
| '/playground'
188+
| '/podcasts'
177189
| '/account-inbox/$inboxId'
178190
| '/friends/$accountAddress'
179191
| '/space/$spaceId'
@@ -190,13 +202,21 @@ export interface RootRouteChildren {
190202
AuthenticateSuccessRoute: typeof AuthenticateSuccessRoute
191203
LoginLazyRoute: typeof LoginLazyRoute
192204
PlaygroundLazyRoute: typeof PlaygroundLazyRoute
205+
PodcastsLazyRoute: typeof PodcastsLazyRoute
193206
AccountInboxInboxIdRoute: typeof AccountInboxInboxIdRoute
194207
FriendsAccountAddressRoute: typeof FriendsAccountAddressRoute
195208
SpaceSpaceIdRoute: typeof SpaceSpaceIdRouteWithChildren
196209
}
197210

198211
declare module '@tanstack/react-router' {
199212
interface FileRoutesByPath {
213+
'/podcasts': {
214+
id: '/podcasts'
215+
path: '/podcasts'
216+
fullPath: '/podcasts'
217+
preLoaderRoute: typeof PodcastsLazyRouteImport
218+
parentRoute: typeof rootRouteImport
219+
}
200220
'/playground': {
201221
id: '/playground'
202222
path: '/playground'
@@ -318,6 +338,7 @@ const rootRouteChildren: RootRouteChildren = {
318338
AuthenticateSuccessRoute: AuthenticateSuccessRoute,
319339
LoginLazyRoute: LoginLazyRoute,
320340
PlaygroundLazyRoute: PlaygroundLazyRoute,
341+
PodcastsLazyRoute: PodcastsLazyRoute,
321342
AccountInboxInboxIdRoute: AccountInboxInboxIdRoute,
322343
FriendsAccountAddressRoute: FriendsAccountAddressRoute,
323344
SpaceSpaceIdRoute: SpaceSpaceIdRouteWithChildren,

apps/events/src/routes/__root.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export const Route = createRootRoute({
3333
<nav className="ml-auto flex gap-4 sm:gap-6">
3434
{authenticated ? (
3535
<div className="flex items-center gap-4">
36+
<Link className="text-xs" to="/podcasts">
37+
Podcasts
38+
</Link>
3639
<Link className="text-xs" to="/playground">
3740
Playground
3841
</Link>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useEntities } from '@graphprotocol/hypergraph-react';
2+
import { createLazyFileRoute } from '@tanstack/react-router';
3+
import { Podcast } from '@/schema';
4+
5+
export const Route = createLazyFileRoute('/podcasts')({
6+
component: RouteComponent,
7+
});
8+
9+
function RouteComponent() {
10+
const space = '530a70d9-d5ee-410a-850a-cf70e4be2ee5';
11+
12+
const { data, isLoading, isError } = useEntities(Podcast, {
13+
mode: 'public',
14+
first: 100,
15+
space: space,
16+
include: {
17+
projects: {},
18+
},
19+
});
20+
console.log({ data, isLoading, isError });
21+
return (
22+
<>
23+
<h1>Podcasts</h1>
24+
{isLoading && <div>Loading...</div>}
25+
{isError && <div>Error</div>}
26+
{data?.map((podcast) => (
27+
<div key={podcast.id}>
28+
<h2>{podcast.name}</h2>
29+
{podcast.projects.map((project) => (
30+
<div key={project._relation.id}>
31+
<h3>- {project.name}</h3>
32+
<div>--{project._relation.website}</div>
33+
</div>
34+
))}
35+
</div>
36+
))}
37+
<pre className="text-xs">{JSON.stringify(data, null, 2)}</pre>
38+
</>
39+
);
40+
}

apps/events/src/schema.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,42 @@ export const Project = Entity.Schema(
111111
name: Type.String,
112112
description: Type.optional(Type.String),
113113
x: Type.optional(Type.String),
114-
avatar: Type.Relation(Image),
115114
},
116115
{
117116
types: [Id('484a18c5-030a-499c-b0f2-ef588ff16d50')],
118117
properties: {
119118
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
120119
description: Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
121120
x: Id('0d625978-4b3c-4b57-a86f-de45c997c73c'),
122-
avatar: Id('1155beff-fad5-49b7-a2e0-da4777b8792c'),
121+
},
122+
},
123+
);
124+
125+
export const Podcast = Entity.Schema(
126+
{
127+
name: Type.String,
128+
description: Type.optional(Type.String),
129+
dateFounded: Type.Date,
130+
rssFeedUrl: Type.optional(Type.String),
131+
projects: Type.Relation(Project, {
132+
properties: {
133+
website: Type.optional(Type.String),
134+
},
135+
}),
136+
},
137+
{
138+
types: [Id('69732974-c632-490d-81a3-12ea567b2a8e')],
139+
properties: {
140+
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
141+
description: Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
142+
dateFounded: Id('41aa3d98-47b6-4a97-b7ec-427e575b910e'),
143+
rssFeedUrl: Id('4dd1a486-c1ad-48c6-b261-e4c8edf7ac65'),
144+
projects: {
145+
propertyId: Id('71931b5f-1d6a-462e-81d9-5b8e85fb5c4b'),
146+
properties: {
147+
website: Id('eed38e74-e679-46bf-8a42-ea3e4f8fb5fb'),
148+
},
149+
},
123150
},
124151
},
125152
);

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

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)