Skip to content

Commit 8f409f5

Browse files
committed
feat: add deleteEntity
1 parent 579d899 commit 8f409f5

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

.changeset/whole-years-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphprotocol/grc-20": minor
3+
---
4+
5+
add Graph.deleteEntity

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
"dependencies": {
3232
"@bufbuild/protobuf": "^1.9.0",
3333
"@changesets/cli": "^2.27.12",
34+
"effect": "^3.13.6",
3435
"ethers": "^5.7.2",
3536
"fflate": "^0.8.2",
36-
"effect": "^3.13.6",
37+
"graphql-request": "^7.1.2",
3738
"image-size": "^2.0.0",
3839
"permissionless": "^0.2.35",
3940
"position-strings": "^2.0.1",

pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/graph/delete-entity.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { Id } from '../id.js';
3+
import { deleteEntity } from './delete-entity.js';
4+
5+
describe('deleteEntity', () => {
6+
beforeEach(() => {
7+
// mock graphql request
8+
vi.mock('graphql-request', () => ({
9+
request: vi.fn().mockResolvedValue({
10+
entity: {
11+
attributes: [
12+
{
13+
attribute: Id("X2gz1QCwFju9FwE8MUgbFi"),
14+
},
15+
],
16+
relations: [
17+
{
18+
id: Id("U4WBHFMVfWXM18vGn25Fv3"),
19+
},
20+
],
21+
},
22+
}),
23+
gql: vi.fn(),
24+
}));
25+
});
26+
27+
it('should delete an entity from the graph', async () => {
28+
const { ops } = await deleteEntity({ id: Id("CffgzbUGPbAadPhAvi9HVy"), space: Id("3JqLpajfPwQJMRT3drhvPu") });
29+
expect(ops).toEqual([
30+
{
31+
type: 'DELETE_TRIPLE',
32+
triple: {
33+
attribute: 'X2gz1QCwFju9FwE8MUgbFi',
34+
entity: 'CffgzbUGPbAadPhAvi9HVy'
35+
}
36+
},
37+
{
38+
type: 'DELETE_RELATION',
39+
relation: { id: 'U4WBHFMVfWXM18vGn25Fv3' }
40+
}
41+
])
42+
})
43+
})

src/graph/delete-entity.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { gql, request } from 'graphql-request';
2+
import { Relation } from '../relation.js';
3+
import { Triple } from '../triple.js';
4+
import type { Op } from '../types.js';
5+
6+
const ENDPOINT = 'https://kg.thegraph.com/graphql';
7+
8+
const deleteEntityQueryDocument = gql`
9+
query deleteEntity($spaceId: String!, $id: String!) {
10+
entity(spaceId: $spaceId, id: $id) {
11+
attributes {
12+
attribute
13+
}
14+
relations {
15+
id
16+
}
17+
}
18+
}
19+
`;
20+
21+
type DeleteEntityResult = {
22+
entity: {
23+
attributes: {
24+
attribute: string;
25+
}[];
26+
relations: {
27+
id: string;
28+
}[];
29+
} | null;
30+
};
31+
32+
/**
33+
* Deletes an entity from the graph.
34+
*
35+
* @example
36+
* ```ts
37+
* const { ops } = await deleteEntity({ id: entityId, space: spaceId });
38+
* ```
39+
*
40+
* @param id - The id of the entity to delete.
41+
* @param space - The space of the entity to delete.
42+
* @returns The operations to delete the entity.
43+
*/
44+
export const deleteEntity = async ({ id, space }: { id: string; space: string }) => {
45+
const result = await request<DeleteEntityResult>(ENDPOINT, deleteEntityQueryDocument, {
46+
id,
47+
spaceId: space,
48+
});
49+
if (result.entity === null) {
50+
throw new Error('Entity not found');
51+
}
52+
const ops: Op[] = [];
53+
for (const attribute of result.entity.attributes) {
54+
ops.push(Triple.remove({ attributeId: attribute.attribute, entityId: id }));
55+
}
56+
for (const relation of result.entity.relations) {
57+
ops.push(Relation.remove(relation.id));
58+
}
59+
return { ops };
60+
};

src/graph/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './create-image.js';
33
export * from './create-property.js';
44
export * from './create-space.js';
55
export * from './create-type.js';
6+
export * from './delete-entity.js';

0 commit comments

Comments
 (0)