Skip to content

Commit 2da4379

Browse files
authored
query public entities (#230)
1 parent 2667165 commit 2da4379

File tree

6 files changed

+70
-94
lines changed

6 files changed

+70
-94
lines changed

apps/events/src/mapping.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ import { Id } from '@graphprotocol/grc-20';
22
import type { Mapping } from '@graphprotocol/hypergraph-react';
33

44
export const mapping: Mapping = {
5+
RelationEntry: {
6+
typeIds: [Id.Id('8f151ba4-de20-4e3c-9cb4-99ddf96f48f1')],
7+
properties: {
8+
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
9+
},
10+
},
511
Event: {
612
typeIds: [Id.Id('4d876b81-787e-41fc-ab5d-075d4da66a3f')],
713
properties: {
814
name: Id.Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
9-
// description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
15+
description: Id.Id('9b1f76ff-9711-404c-861e-59dc3fa7d037'),
16+
},
17+
relations: {
18+
any: Id.Id('8f151ba4-de20-4e3c-9cb4-99ddf96f48f1'),
1019
},
1120
},
1221
// Todo2: {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const Route = createLazyFileRoute('/playground')({
99

1010
function RouteComponent() {
1111
return (
12-
<HypergraphSpaceProvider space="aa84b08d-779a-495c-93f1-44e667baf6d7" mapping={mapping}>
12+
<HypergraphSpaceProvider space="93952dae-46e3-4682-b290-80028ca95403" mapping={mapping}>
1313
<div className="flex flex-col gap-4 max-w-(--breakpoint-sm) mx-auto py-8">
1414
<h1 className="text-2xl font-bold">Playground</h1>
1515
<Playground />

apps/events/src/schema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ export class Todo2 extends Entity.Class<Todo2>('Todo2')({
2020
website: Type.Url,
2121
}) {}
2222

23+
export class RelationEntry extends Entity.Class<RelationEntry>('RelationEntry')({
24+
name: Type.Text,
25+
}) {}
26+
2327
export class Event extends Entity.Class<Event>('Event')({
2428
name: Type.Text,
25-
// description: Type.Text,
29+
description: Type.Text,
2630
// publishDate: Type.Text,
31+
any: Type.Relation(RelationEntry),
2732
}) {}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
// const ENDPOINT = 'https://kg-staging.service.pinax.network/graphql';
2-
export const KG_ENDPOINT = 'https://kg.thegraph.com/graphql';
3-
4-
export const GEO_ENDPOINT = 'https://hypergraph.up.railway.app/graphql';
1+
export const GEO_API_MAINNET_ENDPOINT = 'https://hypergraph-v2.up.railway.app//graphql';
2+
export const GEO_API_TESTNET_ENDPOINT = 'https://hypergraph-v2-testnet.up.railway.app/graphql';

packages/hypergraph-react/src/internal/generate-delete-ops.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Op } from '@graphprotocol/grc-20';
22
import { gql, request } from 'graphql-request';
3-
import { GEO_ENDPOINT } from './constants.js';
3+
import { GEO_API_TESTNET_ENDPOINT } from './constants.js';
44

55
const deleteEntityQueryDocument = gql`
66
query entity($entityId: String!) {
@@ -45,7 +45,7 @@ type DeleteEntityResult = {
4545
};
4646

4747
export const generateDeleteOps = async ({ id, space }: { id: string; space: string }) => {
48-
const result = await request<DeleteEntityResult>(GEO_ENDPOINT, deleteEntityQueryDocument, {
48+
const result = await request<DeleteEntityResult>(GEO_API_TESTNET_ENDPOINT, deleteEntityQueryDocument, {
4949
entityId: id,
5050
});
5151
if (result.entity === null) {

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

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,27 @@ import { gql, request } from 'graphql-request';
66
import { useMemo } from 'react';
77
import { useHypergraph } from '../HypergraphSpaceContext.js';
88
import type { Mapping, MappingEntry } from '../types.js';
9-
import { GEO_ENDPOINT } from './constants.js';
9+
import { GEO_API_TESTNET_ENDPOINT } from './constants.js';
1010
import type { QueryPublicParams } from './types.js';
1111

1212
const entitiesQueryDocument = gql`
13-
query entities($spaceId: String!, $typeId: String!, $relationTypeIds: [String!]!) {
14-
entities(
15-
filter: {
16-
currentVersion: {
17-
version: {
18-
versionTypes: {some: {type: {entityId: {equalTo: $typeId}}}}
19-
versionSpaces: {some: {spaceId: {equalTo: $spaceId}}}
20-
}
21-
}
13+
query entities($spaceId: String!) {
14+
entities(spaceId: $spaceId, filter: {}) {
15+
id
16+
name
17+
values {
18+
propertyId
19+
value
2220
}
23-
) {
24-
nodes {
25-
id
26-
name
27-
currentVersion {
28-
versionId
29-
version {
30-
triples {
31-
nodes {
32-
attributeId
33-
textValue
34-
booleanValue
35-
numberValue
36-
valueType
37-
unitOption
38-
}
39-
}
40-
relationsByFromVersionId(filter: {typeOfId: {in: $relationTypeIds}}) {
41-
nodes {
42-
toEntity {
43-
nodeId
44-
id
45-
name
46-
}
47-
typeOf {
48-
id
49-
name
50-
}
51-
}
52-
}
21+
relations {
22+
to {
23+
id
24+
name
25+
}
26+
type {
27+
id
28+
entity {
29+
name
5330
}
5431
}
5532
}
@@ -59,39 +36,25 @@ query entities($spaceId: String!, $typeId: String!, $relationTypeIds: [String!]!
5936

6037
type EntityQueryResult = {
6138
entities: {
62-
nodes: {
63-
id: string;
64-
name: string;
65-
currentVersion: {
66-
versionId: string;
67-
version: {
68-
triples: {
69-
nodes: {
70-
attributeId: string;
71-
textValue: string;
72-
booleanValue: boolean;
73-
numberValue: number;
74-
valueType: 'TEXT' | 'CHECKBOX' | 'POINT' | 'URL' | 'TIME' | 'NUMBER';
75-
unitOption: unknown;
76-
}[];
77-
};
78-
relationsByFromVersionId: {
79-
nodes: {
80-
typeOf: {
81-
name: string;
82-
id: string;
83-
};
84-
toEntity: {
85-
nodeId: string;
86-
id: string;
87-
name: string;
88-
};
89-
}[];
90-
};
39+
id: string;
40+
name: string;
41+
values: {
42+
propertyId: string;
43+
value: string;
44+
}[];
45+
relations: {
46+
to: {
47+
id: string;
48+
name: string;
49+
};
50+
type: {
51+
id: string;
52+
entity: {
53+
name: string;
9154
};
9255
};
9356
}[];
94-
};
57+
}[];
9558
};
9659

9760
export const parseResult = <S extends Entity.AnyNoContext>(
@@ -104,33 +67,33 @@ export const parseResult = <S extends Entity.AnyNoContext>(
10467
const data: Entity.Entity<S>[] = [];
10568
const invalidEntities: Record<string, unknown>[] = [];
10669

107-
for (const queryEntity of queryData.entities.nodes) {
108-
const queryEntityVersion = queryEntity.currentVersion.version;
70+
for (const queryEntity of queryData.entities) {
10971
const rawEntity: Record<string, string | boolean | number | unknown[] | URL | Date> = {
11072
id: queryEntity.id,
11173
};
74+
11275
// take the mappingEntry and assign the attributes to the rawEntity
11376
for (const [key, value] of Object.entries(mappingEntry?.properties ?? {})) {
114-
const property = queryEntityVersion.triples.nodes.find((a) => a.attributeId === value);
77+
const property = queryEntity.values.find((a) => a.propertyId === value);
11578
if (property) {
11679
if (type.fields[key] === Type.Checkbox) {
117-
rawEntity[key] = property.booleanValue;
80+
rawEntity[key] = Boolean(property.value);
11881
} else if (type.fields[key] === Type.Point) {
119-
rawEntity[key] = property.textValue;
82+
rawEntity[key] = property.value;
12083
} else if (type.fields[key] === Type.Url) {
121-
rawEntity[key] = property.textValue;
84+
rawEntity[key] = property.value;
12285
} else if (type.fields[key] === Type.Date) {
123-
rawEntity[key] = property.textValue;
86+
rawEntity[key] = property.value;
12487
} else if (type.fields[key] === Type.Number) {
125-
rawEntity[key] = Number(property.textValue);
88+
rawEntity[key] = Number(property.value);
12689
} else {
127-
rawEntity[key] = property.textValue;
90+
rawEntity[key] = property.value;
12891
}
12992
}
13093
}
13194

13295
for (const [key, relationId] of Object.entries(mappingEntry?.relations ?? {})) {
133-
const properties = queryEntityVersion.relationsByFromVersionId.nodes.filter((a) => a.typeOf.id === relationId);
96+
const properties = queryEntity.relations.filter((a) => a.type.id === relationId);
13497
if (properties.length === 0) {
13598
rawEntity[key] = [] as unknown[];
13699
continue;
@@ -158,9 +121,9 @@ export const parseResult = <S extends Entity.AnyNoContext>(
158121
}
159122

160123
const newRelationEntities = properties.map((property) => ({
161-
id: property.toEntity.id,
162-
name: property.toEntity.name,
163-
type: relationMappingEntry.typeIds[0],
124+
id: property.to.id,
125+
name: property.to.name,
126+
type: property.type.id,
164127
// TODO: should be determined by the actual value
165128
__deleted: false,
166129
// TODO: should be determined by the actual value
@@ -181,7 +144,8 @@ export const parseResult = <S extends Entity.AnyNoContext>(
181144
const decodeResult = decode({
182145
...rawEntity,
183146
__deleted: false,
184-
__version: queryEntity.currentVersion.versionId,
147+
// __version: queryEntity.currentVersion.versionId,
148+
__version: '',
185149
});
186150

187151
if (Either.isRight(decodeResult)) {
@@ -215,7 +179,7 @@ export const useQueryPublic = <S extends Entity.AnyNoContext>(type: S, params?:
215179
const result = useQueryTanstack({
216180
queryKey: [`entities:geo:${typeName}`],
217181
queryFn: async () => {
218-
const result = await request<EntityQueryResult>(GEO_ENDPOINT, entitiesQueryDocument, {
182+
const result = await request<EntityQueryResult>(GEO_API_TESTNET_ENDPOINT, entitiesQueryDocument, {
219183
spaceId: space,
220184
typeId: mappingEntry?.typeIds[0],
221185
relationTypeIds,

0 commit comments

Comments
 (0)