MCPcopy Create free account
hub / github.com/hashintel/hash / getLatestEntityById

Function getLatestEntityById

apps/hash-api/src/graph/knowledge/primitive/entity.ts:192–260  ·  view source on GitHub ↗
(
  ...args: Parameters<GetLatestEntityByIdFunction<Properties>>
)

Source from the content-addressed store, hash-verified

190 * fault
191 */
192export const getLatestEntityById = async <
193 Properties extends TypeIdsAndPropertiesForEntity =
194 TypeIdsAndPropertiesForEntity,
195>(
196 ...args: Parameters<GetLatestEntityByIdFunction<Properties>>
197): ReturnType<GetLatestEntityByIdFunction<Properties>> => {
198 const [context, authentication, { entityId }] = args;
199
200 const [webId, entityUuid, draftId] = splitEntityId(entityId);
201
202 const allFilter: AllFilter["all"] = [
203 {
204 equal: [{ path: ["uuid"] }, { parameter: entityUuid }],
205 },
206 {
207 equal: [{ path: ["webId"] }, { parameter: webId }],
208 },
209 { equal: [{ path: ["archived"] }, { parameter: false }] },
210 ];
211
212 if (draftId) {
213 /**
214 * If the requested entityId includes a draftId, we know we're looking for a specific draft
215 */
216 allFilter.push({
217 equal: [{ path: ["draftId"] }, { parameter: draftId }],
218 });
219 } else {
220 /**
221 * If the entityId does NOT contain a draftId, EXCLUDE any draft versions of the entity.
222 * Otherwise, a request for an entityId unqualified by a draftId might return a live version
223 * and one or more draft versions, which is currently an error in the function.
224 *
225 * We could alternatively handle this and prioritise a specific version, e.g.
226 * - the live version
227 * - the latest version by the time the edition was created, whether that is a draft or live
228 * ...whether the prioritisation is fixed behavior or varied by parameter.
229 */
230 allFilter.push({
231 not: { exists: { path: ["draftId"] } },
232 });
233 }
234
235 const {
236 entities: [entity, ...unexpectedEntities],
237 } = await queryEntities<Properties>(context, authentication, {
238 filter: {
239 all: allFilter,
240 },
241 temporalAxes: currentTimeInstantTemporalAxes,
242 includeDrafts: !!draftId,
243 includePermissions: false,
244 });
245
246 if (unexpectedEntities.length > 0) {
247 const errorMessage = `Latest entity with entityId ${entityId} returned more than one result with ids: ${unexpectedEntities
248 .map((unexpectedEntity) => unexpectedEntity.metadata.recordId.entityId)
249 .join(", ")}`;

Callers 15

resolveEntityWebFunction · 0.90
processEntityChangeFunction · 0.90
acceptOrgInvitationFunction · 0.90
handleSwapBlockDataFunction · 0.90
handleUpdateEntityFunction · 0.90
updateEntityResolverFunction · 0.90
archiveEntityResolverFunction · 0.90
archiveEntitiesResolverFunction · 0.90
getLinkEntityRightEntityFunction · 0.90

Calls 4

splitEntityIdFunction · 0.90
pushMethod · 0.65
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected