Skip to content

Commit

Permalink
H-1910: Improve bulk ignoring/accepting drafts + fixed runtime error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benwerner01 authored Jan 18, 2024
1 parent 1a557b3 commit 71a1dbb
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 192 deletions.
21 changes: 21 additions & 0 deletions apps/hash-api/src/graph/knowledge/primitive/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,33 @@ export const archiveEntity: ImpureGraphFunction<
await graphApi.updateEntity(actorId, {
entityId: entity.metadata.recordId.entityId,
archived: true,
/**
* @todo: these fields shouldn't be required when archiving an entity
*
* @see https://app.asana.com/0/1201095311341924/1203285029221330/f
* */
draft: entity.metadata.draft,
entityTypeId: entity.metadata.entityTypeId,
properties: entity.properties,
});
};

export const unarchiveEntity: ImpureGraphFunction<
{
entity: Entity;
},
Promise<void>
> = async ({ graphApi }, { actorId }, params) => {
const { entity } = params;
await graphApi.updateEntity(actorId, {
entityId: entity.metadata.recordId.entityId,
/**
* @todo: these fields shouldn't be required when archiving an entity
*
* @see https://app.asana.com/0/1201095311341924/1203285029221330/f
* */
archived: false,
draft: entity.metadata.draft,
entityTypeId: entity.metadata.entityTypeId,
properties: entity.properties,
});
Expand Down
4 changes: 4 additions & 0 deletions apps/hash-api/src/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
addEntityEditorResolver,
addEntityOwnerResolver,
addEntityViewerResolver,
archiveEntitiesResolver,
archiveEntityResolver,
createEntityResolver,
getEntityAuthorizationRelationshipsResolver,
Expand All @@ -41,6 +42,7 @@ import {
removeEntityOwnerResolver,
removeEntityViewerResolver,
structuralQueryEntitiesResolver,
updateEntitiesResolver,
updateEntityResolver,
} from "./knowledge/entity/entity";
import { createFileFromUrl } from "./knowledge/file/create-file-from-url";
Expand Down Expand Up @@ -152,7 +154,9 @@ export const resolvers: Omit<Resolvers, "Query" | "Mutation"> & {
// Knowledge
createEntity: loggedInAndSignedUpMiddleware(createEntityResolver),
updateEntity: loggedInMiddleware(updateEntityResolver),
updateEntities: loggedInMiddleware(updateEntitiesResolver),
archiveEntity: loggedInMiddleware(archiveEntityResolver),
archiveEntities: loggedInMiddleware(archiveEntitiesResolver),
createPage: loggedInAndSignedUpMiddleware(createPageResolver),
setParentPage: loggedInAndSignedUpMiddleware(setParentPageResolver),
updatePage: loggedInAndSignedUpMiddleware(updatePageResolver),
Expand Down
79 changes: 73 additions & 6 deletions apps/hash-api/src/graphql/resolvers/knowledge/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
currentTimeInstantTemporalAxes,
zeroedGraphResolveDepths,
} from "@local/hash-isomorphic-utils/graph-queries";
import { MutationArchiveEntitiesArgs } from "@local/hash-isomorphic-utils/graphql/api-types.gen";
import {
AccountGroupId,
AccountId,
Entity,
EntityId,
OwnedById,
splitEntityId,
} from "@local/hash-subgraph";
Expand All @@ -31,6 +33,7 @@ import {
modifyEntityAuthorizationRelationships,
removeEntityAdministrator,
removeEntityEditor,
unarchiveEntity,
updateEntity,
} from "../../../../graph/knowledge/primitive/entity";
import { bpMultiFilterToGraphFilter } from "../../../../graph/knowledge/primitive/entity/query";
Expand All @@ -53,6 +56,7 @@ import {
MutationRemoveEntityEditorArgs,
MutationRemoveEntityOwnerArgs,
MutationRemoveEntityViewerArgs,
MutationUpdateEntitiesArgs,
MutationUpdateEntityArgs,
Query,
QueryGetEntityArgs,
Expand Down Expand Up @@ -294,12 +298,14 @@ export const updateEntityResolver: ResolverFn<
> = async (
_,
{
draft,
entityId,
updatedProperties,
leftToRightOrder,
rightToLeftOrder,
entityTypeId,
entityUpdate: {
draft,
entityId,
updatedProperties,
leftToRightOrder,
rightToLeftOrder,
entityTypeId,
},
},
{ dataSources, authentication, user },
) => {
Expand Down Expand Up @@ -349,6 +355,25 @@ export const updateEntityResolver: ResolverFn<
return mapEntityToGQL(updatedEntity);
};

export const updateEntitiesResolver: ResolverFn<
Promise<Entity[]>,
{},
LoggedInGraphQLContext,
MutationUpdateEntitiesArgs
> = async (_, { entityUpdates }, context, info) => {
/**
* @todo: use bulk `updateEntities` endpoint in the Graph API
* when it has been implemented.
*/
const updatedEntities = await Promise.all(
entityUpdates.map(async (entityUpdate) =>
updateEntityResolver({}, { entityUpdate }, context, info),
),
);

return updatedEntities;
};

export const archiveEntityResolver: ResolverFn<
Promise<boolean>,
{},
Expand All @@ -365,6 +390,48 @@ export const archiveEntityResolver: ResolverFn<
return true;
};

export const archiveEntitiesResolver: ResolverFn<
Promise<boolean>,
{},
LoggedInGraphQLContext,
MutationArchiveEntitiesArgs
> = async (_, { entityIds }, { dataSources: context, authentication }) => {
const archivedEntities: Entity[] = [];

const entitiesThatCouldNotBeArchived: EntityId[] = [];

await Promise.all(
entityIds.map(async (entityId) => {
try {
const entity = await getLatestEntityById(context, authentication, {
entityId,
includeDrafts: true,
});

await archiveEntity(context, authentication, { entity });

archivedEntities.push(entity);
} catch (error) {
entitiesThatCouldNotBeArchived.push(entityId);
}
}),
);

if (entitiesThatCouldNotBeArchived.length > 0) {
await Promise.all(
archivedEntities.map((entity) =>
unarchiveEntity(context, authentication, { entity }),
),
);

throw new ApolloError(
`Couldn't archive entities with IDs ${entityIds.join(", ")}`,
);
}

return true;
};

export const addEntityOwnerResolver: ResolverFn<
Promise<boolean>,
{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ export const useBlockProtocolUpdateEntity = (

const { data: updateEntityResponseData } = await updateFn({
variables: {
entityId, // @todo-0.3 consider validating that this matches the id format,
entityTypeId,
updatedProperties: properties,
leftToRightOrder,
rightToLeftOrder,
entityUpdate: {
entityId, // @todo-0.3 consider validating that this matches the id format,
entityTypeId,
updatedProperties: properties,
leftToRightOrder,
rightToLeftOrder,
},
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,46 @@ export const useUpdateAuthenticatedUser = () => {

const { errors } = await updateEntity({
variables: {
entityId: latestUserEntity.metadata.recordId.entityId,
updatedProperties: {
...currentProperties,
...(params.shortname
? {
[extractBaseUrl(
systemPropertyTypes.shortname.propertyTypeId,
)]: params.shortname,
}
: {}),
...(params.preferredName
? {
[extractBaseUrl(
systemPropertyTypes.preferredName.propertyTypeId,
)]: params.preferredName,
}
: {}),
...(typeof params.location !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.location.propertyTypeId,
)]: params.location,
}
: {}),
...(typeof params.websiteUrl !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.websiteUrl.propertyTypeId,
)]: params.websiteUrl,
}
: {}),
...(typeof params.preferredPronouns !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.preferredPronouns.propertyTypeId,
)]: params.preferredPronouns,
}
: {}),
entityUpdate: {
entityId: latestUserEntity.metadata.recordId.entityId,
updatedProperties: {
...currentProperties,
...(params.shortname
? {
[extractBaseUrl(
systemPropertyTypes.shortname.propertyTypeId,
)]: params.shortname,
}
: {}),
...(params.preferredName
? {
[extractBaseUrl(
systemPropertyTypes.preferredName.propertyTypeId,
)]: params.preferredName,
}
: {}),
...(typeof params.location !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.location.propertyTypeId,
)]: params.location,
}
: {}),
...(typeof params.websiteUrl !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.websiteUrl.propertyTypeId,
)]: params.websiteUrl,
}
: {}),
...(typeof params.preferredPronouns !== "undefined"
? {
[extractBaseUrl(
systemPropertyTypes.preferredPronouns.propertyTypeId,
)]: params.preferredPronouns,
}
: {}),
},
},
},
});
Expand Down
30 changes: 14 additions & 16 deletions apps/hash-frontend/src/graphql/queries/knowledge/entity.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,15 @@ export const structuralQueryEntitiesQuery = gql`
`;

export const updateEntityMutation = gql`
mutation updateEntity(
$entityId: EntityId!
$updatedProperties: EntityPropertiesObject!
$leftToRightOrder: Int
$rightToLeftOrder: Int
$entityTypeId: VersionedUrl
$draft: Boolean
) {
mutation updateEntity($entityUpdate: EntityUpdateDefinition!) {
# This is a scalar, which has no selection.
updateEntity(
entityId: $entityId
updatedProperties: $updatedProperties
leftToRightOrder: $leftToRightOrder
rightToLeftOrder: $rightToLeftOrder
entityTypeId: $entityTypeId
draft: $draft
)
updateEntity(entityUpdate: $entityUpdate)
}
`;

export const updateEntitiesMutation = gql`
mutation updateEntities($entityUpdates: [EntityUpdateDefinition!]!) {
updateEntities(entityUpdates: $entityUpdates)
}
`;

Expand All @@ -95,6 +87,12 @@ export const archiveEntityMutation = gql`
}
`;

export const archiveEntitiesMutation = gql`
mutation archiveEntities($entityIds: [EntityId!]!) {
archiveEntities(entityIds: $entityIds)
}
`;

export const addEntityOwnerMutation = gql`
mutation addEntityOwner(
$entityId: EntityId!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ export const EditPinnedEntityTypesModal: FunctionComponent<

await updateEntity({
variables: {
entityId: profile.entity.metadata.recordId.entityId,
entityTypeId: profile.entity.metadata.entityTypeId,
updatedProperties: {
...profile.entity.properties,
[extractBaseUrl(
systemPropertyTypes.pinnedEntityTypeBaseUrl.propertyTypeId,
)]: updatedPinnedEntityTypeBaseUrls,
entityUpdate: {
entityId: profile.entity.metadata.recordId.entityId,
entityTypeId: profile.entity.metadata.entityTypeId,
updatedProperties: {
...profile.entity.properties,
[extractBaseUrl(
systemPropertyTypes.pinnedEntityTypeBaseUrl.propertyTypeId,
)]: updatedPinnedEntityTypeBaseUrls,
},
},
},
});
Expand Down
Loading

0 comments on commit 71a1dbb

Please sign in to comment.