-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprepare-publish.ts
More file actions
132 lines (120 loc) · 4.09 KB
/
Copy pathprepare-publish.ts
File metadata and controls
132 lines (120 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
type EntityRelationParams,
Graph,
type Id,
type Op,
type PropertiesParam,
type RelationsParam,
} from '@graphprotocol/grc-20';
import type { Entity } from '@graphprotocol/hypergraph';
import { Type, store } from '@graphprotocol/hypergraph';
import request, { gql } from 'graphql-request';
export type PreparePublishParams<S extends Entity.AnyNoContext> = {
entity: Entity.Entity<S>;
publicSpace: string | Id.Id;
};
const entityToPublishQueryDocument = gql`
query entityToPublish($entityId: UUID!, $spaceId: UUID!) {
entity(id: $entityId) {
valuesList(filter: {spaceId: {is: $spaceId}}) {
propertyId
value
}
relationsList(filter: {spaceId: {is: $spaceId}}) {
id
}
}
}
`;
type EntityToPublishQueryResult = {
entity: {
valuesList: {
propertyId: string;
value: string;
}[];
relationsList: {
id: string;
}[];
};
} | null;
export const preparePublish = async <S extends Entity.AnyNoContext>({
entity,
publicSpace,
}: PreparePublishParams<S>) => {
const data = await request<EntityToPublishQueryResult>(Graph.TESTNET_API_ORIGIN, entityToPublishQueryDocument, {
entityId: entity.id,
spaceId: publicSpace,
});
const mapping = store.getSnapshot().context.mapping;
const typeName = entity.type;
const mappingEntry = mapping[typeName];
if (!mappingEntry) {
throw new Error(`Mapping entry for ${typeName} not found`);
}
const ops: Op[] = [];
const values: PropertiesParam = [];
const relations: RelationsParam = {};
const fields = entity.__schema.fields;
if (data?.entity === null) {
for (const [key, propertyId] of Object.entries(mappingEntry.properties || {})) {
let serializedValue: string = entity[key];
if (fields[key] === Type.Checkbox) {
serializedValue = Graph.serializeCheckbox(entity[key]);
} else if (fields[key] === Type.Date) {
serializedValue = Graph.serializeDate(entity[key]);
} else if (fields[key] === Type.Point) {
serializedValue = Graph.serializePoint(entity[key]);
} else if (fields[key] === Type.Number) {
serializedValue = Graph.serializeNumber(entity[key]);
}
values.push({ property: propertyId, value: serializedValue });
}
for (const [key, relationId] of Object.entries(mappingEntry.relations || {})) {
// @ts-expect-error - TODO: fix the types error
relations[relationId] = entity[key].map((relationEntity) => {
const newRelation: EntityRelationParams = { toEntity: relationEntity.id };
if (relationEntity._relation.id) {
newRelation.id = relationEntity._relation.id;
}
if (relationEntity._relation.position) {
newRelation.position = relationEntity._relation.position;
}
return newRelation;
});
}
const { ops: createOps } = Graph.createEntity({
id: entity.id,
types: mappingEntry.typeIds,
values,
relations,
});
ops.push(...createOps);
return { ops };
}
if (data?.entity) {
for (const [key, propertyId] of Object.entries(mappingEntry.properties || {})) {
let serializedValue: string = entity[key];
if (fields[key] === Type.Checkbox) {
serializedValue = Graph.serializeCheckbox(entity[key]);
} else if (fields[key] === Type.Date) {
serializedValue = Graph.serializeDate(entity[key]);
} else if (fields[key] === Type.Point) {
serializedValue = Graph.serializePoint(entity[key]);
} else if (fields[key] === Type.Number) {
serializedValue = Graph.serializeNumber(entity[key]);
}
const existingValue = data.entity.valuesList.find((value) => value.propertyId === propertyId);
if (serializedValue !== existingValue?.value) {
values.push({ property: propertyId, value: serializedValue });
}
}
// TODO: handle added or removed relations
// TODO: handle updated relations
// TODO: handle added or removed types
if (values.length > 0) {
const { ops: updateEntityOps } = Graph.updateEntity({ id: entity.id, values });
ops.push(...updateEntityOps);
}
}
return { ops };
};