Skip to content

Commit 55b9ffd

Browse files
committed
add public delete
1 parent 8fe6d9a commit 55b9ffd

4 files changed

Lines changed: 110 additions & 9 deletions

File tree

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { useQuery } from '@graphprotocol/hypergraph-react';
1+
import { getSmartAccountWalletClient } from '@/lib/smart-account';
2+
import { _useDeleteEntityPublic, useQuery } from '@graphprotocol/hypergraph-react';
23
import { Event } from '../schema';
4+
import { Button } from './ui/button';
35

46
export const Playground = () => {
5-
const {
6-
data: entityData,
7-
isLoading,
8-
isError,
9-
} = useQuery(Event, {
7+
const { data, isLoading, isError } = useQuery(Event, {
108
mode: 'public',
119
include: {
1210
sponsors: {
@@ -15,13 +13,38 @@ export const Playground = () => {
1513
},
1614
});
1715

18-
console.log({ isLoading, isError, entityData });
16+
const deleteEntity = _useDeleteEntityPublic(Event, {
17+
space: '1c954768-7e14-4f0f-9396-0fe9dcd55fe8',
18+
});
19+
20+
console.log({ isLoading, isError, data });
1921

2022
return (
2123
<div>
2224
{isLoading && <div>Loading...</div>}
2325
{isError && <div>Error</div>}
24-
<pre className="text-xs">{JSON.stringify(entityData, null, 2)}</pre>
26+
{data?.map((event) => (
27+
<div key={event.id}>
28+
<h2>{event.name}</h2>
29+
<Button
30+
onClick={async () => {
31+
const walletClient = await getSmartAccountWalletClient();
32+
if (!walletClient) {
33+
throw new Error('Wallet client not found');
34+
}
35+
const { cid, txResult, success } = await deleteEntity({
36+
id: event.id,
37+
// @ts-expect-error - TODO: fix the types error
38+
walletClient,
39+
});
40+
console.log({ cid, txResult, success });
41+
}}
42+
>
43+
Delete
44+
</Button>
45+
<pre className="text-xs">{JSON.stringify(event, null, 2)}</pre>
46+
</div>
47+
))}
2548
</div>
2649
);
2750
};

apps/events/src/lib/smart-account.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export const getSmartAccountWalletClient = async () => {
88
// return await grc20getSmartAccountWalletClient({
99
// privateKey,
1010
// });
11-
console.log('privateKey', privateKey);
1211
return await getWalletClient({
1312
privateKey,
1413
});

packages/hypergraph-react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {
2323
useUpdateEntity,
2424
} from './HypergraphSpaceContext.js';
2525
export { generateDeleteOps as _generateDeleteOps } from './internal/generate-delete-ops.js';
26+
export { useDeleteEntityPublic as _useDeleteEntityPublic } from './internal/use-delete-entity-public.js';
2627
export { useGenerateCreateOps as _useGenerateCreateOps } from './internal/use-generate-create-ops.js';
2728
export { useQueryPublic as _useQueryPublic } from './internal/use-query-public.js';
2829
export { publishOps } from './publish-ops.js';
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20';
2+
import type { Entity } from '@graphprotocol/hypergraph';
3+
import { useQueryClient } from '@tanstack/react-query';
4+
import request, { gql } from 'graphql-request';
5+
import { publishOps } from '../publish-ops.js';
6+
import { GEO_API_TESTNET_ENDPOINT } from './constants.js';
7+
8+
type DeleteEntityPublicParams = {
9+
space: string;
10+
};
11+
12+
const deleteEntityQueryDocument = gql`
13+
query entityToDelete($entityId: String!, $spaceId: String!) {
14+
entity(id: $entityId, spaceId: $spaceId) {
15+
values {
16+
propertyId
17+
}
18+
relations {
19+
id
20+
}
21+
}
22+
}
23+
`;
24+
25+
type EntityToDeleteQueryResult = {
26+
entity: {
27+
values: {
28+
propertyId: string;
29+
}[];
30+
relations: {
31+
id: string;
32+
}[];
33+
};
34+
} | null;
35+
36+
export const useDeleteEntityPublic = <S extends Entity.AnyNoContext>(type: S, { space }: DeleteEntityPublicParams) => {
37+
const queryClient = useQueryClient();
38+
39+
return async ({ id, walletClient }: { id: string; walletClient: GeoSmartAccount }) => {
40+
const result = await request<EntityToDeleteQueryResult>(GEO_API_TESTNET_ENDPOINT, deleteEntityQueryDocument, {
41+
spaceId: space,
42+
entityId: id,
43+
});
44+
if (!result) {
45+
return { success: false, error: 'Entity not found' };
46+
}
47+
const { values, relations } = result.entity;
48+
const ops: Op[] = [];
49+
const { ops: unsetEntityValuesOps } = Graph.unsetEntityValues({
50+
id,
51+
properties: values.map(({ propertyId }) => propertyId),
52+
});
53+
ops.push(...unsetEntityValuesOps);
54+
for (const relation of relations) {
55+
const { ops: deleteRelationOps } = Graph.deleteRelation({ id: relation.id });
56+
ops.push(...deleteRelationOps);
57+
}
58+
59+
const { cid, txResult } = await publishOps({
60+
ops,
61+
space,
62+
name: `Delete entity ${id}`,
63+
walletClient,
64+
network: 'TESTNET',
65+
});
66+
await new Promise((resolve) => setTimeout(resolve, 2000));
67+
queryClient.invalidateQueries({
68+
queryKey: [
69+
'hypergraph-public-entities',
70+
// @ts-expect-error - TODO: find a better way to access the type.name
71+
type.name,
72+
space,
73+
],
74+
});
75+
76+
return { success: true, cid, txResult };
77+
};
78+
};

0 commit comments

Comments
 (0)