Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/hash-api/src/graph/knowledge/primitive/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,14 @@ export const createEntityWithLinks = async <
...args: Parameters<CreateEntityWithLinksFunction<Properties>>
): ReturnType<CreateEntityWithLinksFunction<Properties>> => {
const [context, authentication, params] = args;
const { entityTypeIds, properties, linkedEntities, ...createParams } = params;
const {
entityTypeIds,
properties,
linkedEntities,
entityUuid,
policies,
...createParams
} = params;

const entitiesInTree = linkedTreeFlatten<
EntityDefinition,
Expand Down Expand Up @@ -381,12 +388,15 @@ export const createEntityWithLinks = async <
* draft entities, but would need changing if we change this. H-2430 which would introduce draft/live versions of
* pages which may affect this.
*/
const isRootEntity = definition.parentIndex === -1;

const entity = existingEntityId
? await getLatestEntityById<Properties>(context, authentication, {
entityId: existingEntityId,
})
: await createEntity<Properties>(context, authentication, {
...createParams,
...(isRootEntity ? { entityUuid, policies } : {}),
properties: definition.entityProperties!,
entityTypeIds:
definition.entityTypeIds as Properties["entityTypeIds"],
Expand Down
35 changes: 34 additions & 1 deletion apps/hash-api/src/graph/knowledge/system-types/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
import type {
BaseUrl,
Entity,
EntityUuid,
VersionedUrl,
WebId,
} from "@blockprotocol/type-system";
Expand Down Expand Up @@ -152,6 +153,7 @@ export const createFileFromUploadRequest: ImpureGraphFunction<
description,
displayName: providedDisplayName,
name: unnormalizedFilename,
makePublic,
size,
} = params;

Expand Down Expand Up @@ -235,10 +237,23 @@ export const createFileFromUploadRequest: ImpureGraphFunction<

let fileEntity = existingEntity;
if (!fileEntity) {
const entityUuid = generateUuid() as EntityUuid;

fileEntity = await createEntity<File>(ctx, authentication, {
webId,
entityUuid,
properties: initialProperties,
entityTypeIds: entityTypeIds as File["entityTypeIds"],
policies: makePublic
? [
Comment thread
CiaranMn marked this conversation as resolved.
{
name: `public-view-entity-${entityUuid}`,
effect: "permit",
actions: ["viewEntity"],
principal: null,
},
]
: undefined,
Comment thread
CiaranMn marked this conversation as resolved.
});
}

Expand Down Expand Up @@ -302,7 +317,12 @@ export const createFileFromExternalUrl: ImpureGraphFunction<
false,
true
> = async (ctx, authentication, params) => {
const { description, displayName: providedDisplayName, url } = params;
const {
description,
displayName: providedDisplayName,
makePublic,
url,
} = params;

const urlValidation = validateExternalUrl(url);
if (!urlValidation.valid) {
Expand Down Expand Up @@ -390,6 +410,8 @@ export const createFileFromExternalUrl: ImpureGraphFunction<
},
};

const entityUuid = generateUuid() as EntityUuid;

return existingEntity
? await updateEntity<File>(ctx, authentication, {
entity: existingEntity,
Expand All @@ -407,8 +429,19 @@ export const createFileFromExternalUrl: ImpureGraphFunction<
})
: await createEntity<File>(ctx, authentication, {
webId,
entityUuid,
properties,
entityTypeIds: entityTypeIds as File["entityTypeIds"],
policies: makePublic
? [
{
name: `public-view-entity-${entityUuid}`,
effect: "permit",
Comment thread
CiaranMn marked this conversation as resolved.
actions: ["viewEntity"],
principal: null,
},
]
: undefined,
Comment thread
CiaranMn marked this conversation as resolved.
});
} catch (error) {
throw new Error(
Expand Down
35 changes: 33 additions & 2 deletions apps/hash-api/src/graphql/resolvers/knowledge/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
serializeQueryEntitiesResponse,
serializeQueryEntitySubgraphResponse,
summarizeEntities,
type CreateEntityParameters,
} from "@local/hash-graph-sdk/entity";
import {
createPolicy,
deletePolicyById,
queryPolicies,
} from "@local/hash-graph-sdk/policy";
import { generateUuid } from "@local/hash-isomorphic-utils/generate-uuid";

import {
canUserReadEntity,
Expand Down Expand Up @@ -50,7 +52,12 @@ import type {
ResolverFn,
} from "../../../api-types.gen";
import type { GraphQLContext, LoggedInGraphQLContext } from "../../../context";
import type { Entity, EntityId, WebId } from "@blockprotocol/type-system";
import type {
Entity,
EntityId,
EntityUuid,
WebId,
} from "@blockprotocol/type-system";
import type { EntityValidationReport } from "@local/hash-graph-sdk/validation";

export const createEntityResolver: ResolverFn<
Expand All @@ -60,14 +67,34 @@ export const createEntityResolver: ResolverFn<
MutationCreateEntityArgs
> = async (
_,
{ webId, properties, entityTypeIds, linkedEntities, linkData, draft },
{
webId,
properties,
entityTypeIds,
linkedEntities,
linkData,
draft,
makePublic,
},
graphQLContext,
) => {
const { authentication, user } = graphQLContext;
const context = graphQLContextToImpureGraphContext(graphQLContext);

let entity: Entity;

const entityUuid = generateUuid() as EntityUuid;
const policies: CreateEntityParameters["policies"] = makePublic
? [
{
name: `public-view-entity-${entityUuid}`,
effect: "permit",
actions: ["viewEntity"],
principal: null,
} as const,
]
: undefined;
Comment thread
CiaranMn marked this conversation as resolved.

if (linkData) {
const { leftEntityId, rightEntityId } = linkData;

Expand All @@ -84,21 +111,25 @@ export const createEntityResolver: ResolverFn<

entity = await createLinkEntity(context, authentication, {
webId: webId ?? (user.accountId as WebId),
entityUuid,
properties,
linkData: {
leftEntityId,
rightEntityId,
},
entityTypeIds: mustHaveAtLeastOne(entityTypeIds),
draft: draft ?? undefined,
policies,
Comment thread
CiaranMn marked this conversation as resolved.
});
} else {
entity = await createEntityWithLinks(context, authentication, {
webId: webId ?? (user.accountId as WebId),
entityUuid,
entityTypeIds: mustHaveAtLeastOne(entityTypeIds),
properties,
linkedEntities: linkedEntities ?? undefined,
draft: draft ?? undefined,
policies,
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const createFileFromUrl: ResolverFn<
fileEntityCreationInput,
fileEntityUpdateInput,
displayName,
makePublic,
url,
},
graphQLContext,
Expand All @@ -36,6 +37,7 @@ export const createFileFromUrl: ResolverFn<
displayName,
fileEntityCreationInput,
fileEntityUpdateInput,
makePublic,
url,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const requestFileUpload: ResolverFn<
displayName,
fileEntityCreationInput,
fileEntityUpdateInput,
makePublic,
name,
size,
},
Expand All @@ -57,6 +58,7 @@ export const requestFileUpload: ResolverFn<
displayName,
fileEntityCreationInput,
fileEntityUpdateInput,
makePublic,
name,
size,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const createEntityMutation = gql`
$properties: PropertyObjectWithMetadata!
$linkData: LinkData
$draft: Boolean
$makePublic: Boolean = false
) {
# This is a scalar, which has no selection.
createEntity(
Expand All @@ -15,6 +16,7 @@ export const createEntityMutation = gql`
properties: $properties
linkData: $linkData
draft: $draft
makePublic: $makePublic
)
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const requestFileUpload = gql`
$displayName: String
$fileEntityCreationInput: FileEntityCreationInput
$fileEntityUpdateInput: FileEntityUpdateInput
$makePublic: Boolean = false
) {
requestFileUpload(
size: $size
Expand All @@ -16,6 +17,7 @@ export const requestFileUpload = gql`
displayName: $displayName
fileEntityCreationInput: $fileEntityCreationInput
fileEntityUpdateInput: $fileEntityUpdateInput
makePublic: $makePublic
) {
presignedPut {
url
Expand All @@ -32,13 +34,15 @@ export const createFileFromUrl = gql`
$description: String
$fileEntityCreationInput: FileEntityCreationInput
$fileEntityUpdateInput: FileEntityUpdateInput
$makePublic: Boolean = false
) {
createFileFromUrl(
url: $url
displayName: $displayName
description: $description
fileEntityCreationInput: $fileEntityCreationInput
fileEntityUpdateInput: $fileEntityUpdateInput
makePublic: $makePublic
)
}
`;
43 changes: 3 additions & 40 deletions apps/hash-frontend/src/shared/file-upload-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {
mergePropertyObjectAndMetadata,
} from "@local/hash-graph-sdk/entity";

import { AuthorizationSubjectKind } from "../graphql/api-types.gen";
import {
addEntityViewerMutation,
archiveEntityMutation,
createEntityMutation,
updateEntityMutation,
Expand All @@ -29,8 +27,6 @@ import { uploadFileToStorageProvider } from "./upload-to-storage-provider";

import type { UploadFileRequestData } from "../components/hooks/block-protocol-functions/knowledge/knowledge-shim";
import type {
AddEntityViewerMutation,
AddEntityViewerMutationVariables,
ArchiveEntityMutation,
ArchiveEntityMutationVariables,
CreateEntityMutation,
Expand Down Expand Up @@ -185,11 +181,6 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
{},
);

const [addEntityViewer] = useMutation<
AddEntityViewerMutation,
AddEntityViewerMutationVariables
>(addEntityViewerMutation);

const [archiveEntity] = useMutation<
ArchiveEntityMutation,
ArchiveEntityMutationVariables
Expand Down Expand Up @@ -258,6 +249,7 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
variables: {
description,
displayName: name,
makePublic,
url: fileData.url,
...("fileEntityUpdateInput" in fileData
? { fileEntityUpdateInput: fileData.fileEntityUpdateInput }
Expand All @@ -275,16 +267,6 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
}

fileEntity = new HashEntity<FileEntity>(data.createFileFromUrl);

if (makePublic) {
/** @todo: make entity public as part of `createEntity` query once this is supported */
await addEntityViewer({
variables: {
entityId: fileEntity.metadata.recordId.entityId,
viewer: { kind: AuthorizationSubjectKind.Public },
},
});
}
} catch (err) {
// createFileFromUrlFn might itself throw rather than return errors, thus this catch

Expand Down Expand Up @@ -321,6 +303,7 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
displayName: name,
name: fileData.file.name,
size: fileData.file.size,
makePublic,
...("fileEntityUpdateInput" in fileData
? { fileEntityUpdateInput: fileData.fileEntityUpdateInput }
: {
Expand All @@ -340,16 +323,6 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
data.requestFileUpload.entity,
);

if (makePublic) {
/** @todo: make entity public as part of `createEntity` query once this is supported */
await addEntityViewer({
variables: {
entityId: fileEntity.metadata.recordId.entityId,
viewer: { kind: AuthorizationSubjectKind.Public },
},
});
}

presignedPut = data.requestFileUpload.presignedPut;

// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -519,6 +492,7 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
properties: linkProperties
? mergePropertyObjectAndMetadata(linkProperties, undefined)
: { value: {} },
makePublic,
},
});

Expand All @@ -528,16 +502,6 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {

const linkEntity = new HashLinkEntity(data.createEntity);

if (makePublic) {
/** @todo: make entity public as part of `createEntity` query once this is supported */
await addEntityViewer({
variables: {
entityId: linkEntity.metadata.recordId.entityId,
viewer: { kind: AuthorizationSubjectKind.Public },
},
});
}

const updatedUpload: FileUpload = {
...upload,
status: "complete",
Expand Down Expand Up @@ -567,7 +531,6 @@ export const FileUploadsProvider = ({ children }: PropsWithChildren) => {
}
},
[
addEntityViewer,
archiveEntity,
createEntity,
createFileFromUrlFn,
Expand Down
Loading
Loading