Skip to content

Commit 083edfd

Browse files
authored
graphql alias per relation + backlinks support (#557)
1 parent 678d6fa commit 083edfd

File tree

14 files changed

+359
-1176
lines changed

14 files changed

+359
-1176
lines changed

.changeset/plain-turkeys-matter.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@graphprotocol/hypergraph-react": patch
3+
"@graphprotocol/hypergraph": patch
4+
---
5+
6+
add Type.Backlink
7+

apps/events/src/routes/podcasts.lazy.tsx

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Entity } from '@graphprotocol/hypergraph';
21
import { useEntities } from '@graphprotocol/hypergraph-react';
32
import { createLazyFileRoute } from '@tanstack/react-router';
4-
import { useEffect } from 'react';
53
import { Podcast } from '@/schema';
64

75
export const Route = createLazyFileRoute('/podcasts')({
@@ -11,25 +9,43 @@ export const Route = createLazyFileRoute('/podcasts')({
119
function RouteComponent() {
1210
const space = 'e252f9e1-d3ad-4460-8bf1-54f93b02f220';
1311

14-
useEffect(() => {
15-
setTimeout(async () => {
16-
const result = await Entity.findOnePublic(Podcast, {
17-
id: 'f5d27d3e-3a51-452d-bac2-702574381633',
18-
space: space,
19-
include: {
20-
listenOn: {},
21-
},
22-
});
23-
console.log('findOnePublic result:', result);
24-
}, 1000);
25-
}, []);
12+
// useEffect(() => {
13+
// setTimeout(async () => {
14+
// const result = await Entity.searchManyPublic(Podcast, {
15+
// query: 'Joe',
16+
// space: space,
17+
// // include: {
18+
// // listenOn: {},
19+
// // },
20+
// });
21+
// console.log('searchManyPublic result:', result);
22+
// }, 1000);
23+
// }, []);
24+
25+
// const { data: podcast } = useEntity(Podcast, {
26+
// id: 'f5d27d3e-3a51-452d-bac2-702574381633',
27+
// mode: 'public',
28+
// space: space,
29+
// include: {
30+
// listenOn: {},
31+
// hosts: {
32+
// avatar: {},
33+
// },
34+
// episodes: {},
35+
// },
36+
// });
37+
// console.log({ podcast });
2638

2739
const { data, isLoading, isError } = useEntities(Podcast, {
2840
mode: 'public',
29-
first: 100,
41+
first: 10,
3042
space: space,
3143
include: {
3244
listenOn: {},
45+
hosts: {
46+
avatar: {},
47+
},
48+
episodes: {},
3349
},
3450
orderBy: { property: 'dateFounded', direction: 'asc' },
3551
backlinksTotalCountsTypeId1: '972d201a-d780-4568-9e01-543f67b26bee',

apps/events/src/schema.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,39 @@ export const GenericEntity = Entity.Schema(
162162
},
163163
);
164164

165+
export const Episode2 = Entity.Schema(
166+
{
167+
name: Type.String,
168+
description: Type.optional(Type.String),
169+
airDate: Type.Date,
170+
avatar: Type.Relation(Image),
171+
duration: Type.optional(Type.Number), // in seconds
172+
audioUrl: Type.optional(Type.String),
173+
listenOn: Type.Relation(GenericEntity, {
174+
properties: {
175+
website: Type.optional(Type.String),
176+
},
177+
}),
178+
},
179+
{
180+
types: [Id('972d201a-d780-4568-9e01-543f67b26bee')],
181+
properties: {
182+
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
183+
description: Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
184+
airDate: Id('77999397-f78d-44a7-bbc5-d93a617af47c'),
185+
duration: Id('76996acc-d10f-4cd5-9ac9-4a705b8e03b4'),
186+
audioUrl: Id('87f919d5-560b-408c-be8d-318e2c5c098b'),
187+
avatar: Id('1155beff-fad5-49b7-a2e0-da4777b8792c'),
188+
listenOn: {
189+
propertyId: Id('1367bac7-dcea-4b80-86ad-a4a4cdd7c2cb'),
190+
properties: {
191+
website: Id('eed38e74-e679-46bf-8a42-ea3e4f8fb5fb'),
192+
},
193+
},
194+
},
195+
},
196+
);
197+
165198
export const Podcast = Entity.Schema(
166199
{
167200
name: Type.String,
@@ -176,6 +209,7 @@ export const Podcast = Entity.Schema(
176209
website: Type.optional(Type.String),
177210
},
178211
}),
212+
episodes: Type.Backlink(Episode2),
179213
},
180214
{
181215
types: [Id('4c81561d-1f95-4131-9cdd-dd20ab831ba2')],
@@ -193,6 +227,7 @@ export const Podcast = Entity.Schema(
193227
website: Id('eed38e74-e679-46bf-8a42-ea3e4f8fb5fb'),
194228
},
195229
},
230+
episodes: Id('f1873bbc-381f-4604-abad-76fed4f6d73f'),
196231
},
197232
},
198233
);

packages/hypergraph-react/src/hooks/use-entities-public-infinite.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Constants, Entity, Utils } from '@graphprotocol/hypergraph';
1+
import { Constants, Entity } from '@graphprotocol/hypergraph';
22
import { useInfiniteQuery as useInfiniteQueryTanstack } from '@tanstack/react-query';
33
import * as Option from 'effect/Option';
44
import type * as Schema from 'effect/Schema';
@@ -13,24 +13,12 @@ export const useEntitiesPublicInfinite = <S extends Schema.Schema.AnyNoContext>(
1313
const { enabled = true, filter, include, space: spaceFromParams, first = 2, offset = 0 } = params ?? {};
1414
const { space: spaceFromContext } = useHypergraphSpaceInternal();
1515
const space = spaceFromParams ?? spaceFromContext;
16-
17-
// constructing the relation type ids for the query
18-
const relationTypeIds = Utils.getRelationTypeIds(type, include);
19-
2016
const typeIds = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(type.ast as SchemaAST.TypeLiteral).pipe(
2117
Option.getOrElse(() => []),
2218
);
2319

2420
const result = useInfiniteQueryTanstack({
25-
queryKey: [
26-
'hypergraph-public-entities',
27-
space,
28-
typeIds,
29-
relationTypeIds.level1,
30-
relationTypeIds.level2,
31-
filter,
32-
'infinite',
33-
],
21+
queryKey: ['hypergraph-public-entities', space, typeIds, include, filter, 'infinite'],
3422
queryFn: async ({ pageParam }) => {
3523
return Entity.findManyPublic(type, { filter, include, space, first, offset: pageParam });
3624
},

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Constants, Entity, Utils } from '@graphprotocol/hypergraph';
1+
import { Constants, Entity } from '@graphprotocol/hypergraph';
22
import { useQuery as useQueryTanstack } from '@tanstack/react-query';
33
import * as Option from 'effect/Option';
44
import type * as Schema from 'effect/Schema';
@@ -19,10 +19,6 @@ export const useEntitiesPublic = <S extends Schema.Schema.AnyNoContext>(type: S,
1919
} = params ?? {};
2020
const { space: spaceFromContext } = useHypergraphSpaceInternal();
2121
const space = spaceFromParams ?? spaceFromContext;
22-
23-
// constructing the relation type ids for the query
24-
const relationTypeIds = Utils.getRelationTypeIds(type, include);
25-
2622
const typeIds = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(type.ast as SchemaAST.TypeLiteral).pipe(
2723
Option.getOrElse(() => []),
2824
);
@@ -32,8 +28,7 @@ export const useEntitiesPublic = <S extends Schema.Schema.AnyNoContext>(type: S,
3228
'hypergraph-public-entities',
3329
space,
3430
typeIds,
35-
relationTypeIds.level1,
36-
relationTypeIds.level2,
31+
include,
3732
filter,
3833
first,
3934
offset,

0 commit comments

Comments
 (0)