Skip to content

Commit

Permalink
H-3741: Allow querying children of resolved entity types as well (#5834)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann authored Dec 7, 2024
1 parent f37f0cd commit 5f4b811
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const getClosedMultiEntityTypeResolver: ResolverFn<
{
entityTypeIds,
// All references to other types are resolved, and those types provided under 'definitions' in the response
includeResolved: true,
includeResolved: "resolved",
includeDrafts: includeDrafts ?? false,
temporalAxes: includeArchived
? fullTransactionTimeAxis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getReferencedDataTypes = (

for (const value of propertyValues) {
if ("$ref" in value) {
types.push(getDataType(value, definitions));
types.push(getDataType(value, definitions).schema);
}
}

Expand Down
37 changes: 34 additions & 3 deletions libs/@local/graph/api/openapi/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libs/@local/graph/api/src/rest/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use hash_graph_store::{
GetDataTypeSubgraphParams, GetDataTypesParams, GetDataTypesResponse,
UnarchiveDataTypeParams, UpdateDataTypeEmbeddingParams, UpdateDataTypesParams,
},
entity_type::ClosedDataTypeDefinition,
pool::StorePool,
query::ConflictBehavior,
};
Expand Down Expand Up @@ -98,6 +99,7 @@ use crate::rest::{
GetDataTypeSubgraphResponse,
ArchiveDataTypeParams,
UnarchiveDataTypeParams,
ClosedDataTypeDefinition,
ConversionDefinition,
ConversionExpression,
Expand Down
5 changes: 3 additions & 2 deletions libs/@local/graph/api/src/rest/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use hash_graph_store::{
ArchiveEntityTypeParams, CreateEntityTypeParams, EntityTypeQueryToken,
EntityTypeResolveDefinitions, EntityTypeStore as _, GetClosedMultiEntityTypeParams,
GetClosedMultiEntityTypeResponse, GetEntityTypeSubgraphParams, GetEntityTypesParams,
GetEntityTypesResponse, IncludeEntityTypeOption, UnarchiveEntityTypeParams,
UpdateEntityTypeEmbeddingParams, UpdateEntityTypesParams,
GetEntityTypesResponse, IncludeEntityTypeOption, IncludeResolvedEntityTypeOption,
UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams, UpdateEntityTypesParams,
},
pool::StorePool,
query::ConflictBehavior,
Expand Down Expand Up @@ -111,6 +111,7 @@ use crate::rest::{
GetEntityTypeSubgraphResponse,
ArchiveEntityTypeParams,
UnarchiveEntityTypeParams,
IncludeResolvedEntityTypeOption,
)
),
tags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,31 +689,34 @@ where
} else {
None
},
#[expect(
clippy::if_then_some_else_none,
reason = "False positive, use of `await`"
)]
definitions: if params.include_entity_types
== Some(IncludeEntityTypeOption::Resolved)
{
let entity_type_uuids = root_entities
.iter()
.flat_map(|(entity, _)| {
entity
.metadata
.entity_type_ids
.iter()
.map(EntityTypeUuid::from_url)
})
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
definitions: match params.include_entity_types {
Some(
self.get_entity_type_resolve_definitions(actor_id, &entity_type_uuids)
IncludeEntityTypeOption::Resolved
| IncludeEntityTypeOption::ResolvedWithDataTypeChildren,
) => {
let entity_type_uuids = root_entities
.iter()
.flat_map(|(entity, _)| {
entity
.metadata
.entity_type_ids
.iter()
.map(EntityTypeUuid::from_url)
})
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
Some(
self.get_entity_type_resolve_definitions(
actor_id,
&entity_type_uuids,
params.include_entity_types
== Some(IncludeEntityTypeOption::ResolvedWithDataTypeChildren),
)
.await?,
)
} else {
None
)
}
None | Some(IncludeEntityTypeOption::Closed) => None,
},
entities: root_entities
.into_iter()
Expand Down Expand Up @@ -1385,33 +1388,36 @@ where
} else {
None
},
#[expect(
clippy::if_then_some_else_none,
reason = "False positive, use of `await`"
)]
definitions: if params.include_entity_types
== Some(IncludeEntityTypeOption::Resolved)
{
let entity_type_uuids = subgraph
.vertices
.entities
.values()
.flat_map(|entity| {
entity
.metadata
.entity_type_ids
.iter()
.map(EntityTypeUuid::from_url)
})
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
definitions: match params.include_entity_types {
Some(
self.get_entity_type_resolve_definitions(actor_id, &entity_type_uuids)
IncludeEntityTypeOption::Resolved
| IncludeEntityTypeOption::ResolvedWithDataTypeChildren,
) => {
let entity_type_uuids = subgraph
.vertices
.entities
.values()
.flat_map(|entity| {
entity
.metadata
.entity_type_ids
.iter()
.map(EntityTypeUuid::from_url)
})
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
Some(
self.get_entity_type_resolve_definitions(
actor_id,
&entity_type_uuids,
params.include_entity_types
== Some(IncludeEntityTypeOption::ResolvedWithDataTypeChildren),
)
.await?,
)
} else {
None
)
}
None | Some(IncludeEntityTypeOption::Closed) => None,
},
subgraph,
cursor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use hash_graph_authorization::{
};
use hash_graph_store::{
entity_type::{
ArchiveEntityTypeParams, CountEntityTypesParams, CreateEntityTypeParams,
EntityTypeQueryPath, EntityTypeResolveDefinitions, EntityTypeStore,
ArchiveEntityTypeParams, ClosedDataTypeDefinition, CountEntityTypesParams,
CreateEntityTypeParams, EntityTypeQueryPath, EntityTypeResolveDefinitions, EntityTypeStore,
GetClosedMultiEntityTypeParams, GetClosedMultiEntityTypeResponse,
GetEntityTypeSubgraphParams, GetEntityTypeSubgraphResponse, GetEntityTypesParams,
GetEntityTypesResponse, IncludeEntityTypeOption, UnarchiveEntityTypeParams,
Expand Down Expand Up @@ -52,10 +52,10 @@ use tracing::{Instrument as _, instrument};
use type_system::{
Valid, Validator as _,
schema::{
ClosedDataType, ClosedEntityType, ClosedMultiEntityType, DataTypeUuid, EntityType,
EntityTypeResolveData, EntityTypeToPropertyTypeEdge, EntityTypeUuid, EntityTypeValidator,
InheritanceDepth, OntologyTypeResolver, OntologyTypeUuid, PartialEntityType,
PropertyTypeUuid,
ClosedDataType, ClosedEntityType, ClosedMultiEntityType, DataType, DataTypeUuid,
EntityType, EntityTypeResolveData, EntityTypeToPropertyTypeEdge, EntityTypeUuid,
EntityTypeValidator, InheritanceDepth, OntologyTypeResolver, OntologyTypeUuid,
PartialEntityType, PropertyTypeUuid,
},
url::{OntologyTypeVersion, VersionedUrl},
};
Expand Down Expand Up @@ -121,6 +121,7 @@ where
&self,
actor_id: AccountId,
entity_types: &[EntityTypeUuid],
include_data_type_children: bool,
) -> Result<EntityTypeResolveDefinitions, Report<QueryError>> {
let mut definitions = EntityTypeResolveDefinitions::default();
let rows = self
Expand Down Expand Up @@ -214,18 +215,42 @@ where
.insert(VersionedUrl::from(vertex_id), property_type.schema);
}

let query = if include_data_type_children {
"
SELECT DISTINCT ON (ontology_id) schema, closed_schema
FROM data_type_inherits_from
JOIN data_types
ON ontology_id = source_data_type_ontology_id
OR ontology_id = target_data_type_ontology_id
WHERE target_data_type_ontology_id = ANY($1);
"
} else {
"
SELECT schema, closed_schema
FROM data_types
WHERE ontology_id = ANY($1);
"
};

definitions.data_types.extend(
self.as_client()
.query(
"SELECT closed_schema FROM data_types WHERE ontology_id = ANY($1);",
&[&data_type_uuids],
)
.query(query, &[&data_type_uuids])
.await
.change_context(QueryError)?
.into_iter()
.map(|row| {
let schema: Valid<ClosedDataType> = row.get(0);
(schema.id.clone(), schema.into_inner())
let parents = row
.get::<_, Valid<DataType>>(0)
.into_inner()
.all_of
.into_iter()
.map(|reference| reference.url)
.collect();
let schema = row.get::<_, Valid<ClosedDataType>>(1).into_inner();
(schema.id.clone(), ClosedDataTypeDefinition {
schema,
parents,
})
}),
);

Expand Down Expand Up @@ -995,11 +1020,20 @@ where

response.closed_entity_types = Some(self.get_closed_entity_types(&ids).await?);

if include_entity_types == IncludeEntityTypeOption::Resolved {
response.definitions = Some(
self.get_entity_type_resolve_definitions(actor_id, &ids)
.await?,
);
match include_entity_types {
IncludeEntityTypeOption::Closed => {}
IncludeEntityTypeOption::Resolved => {
response.definitions = Some(
self.get_entity_type_resolve_definitions(actor_id, &ids, false)
.await?,
);
}
IncludeEntityTypeOption::ResolvedWithDataTypeChildren => {
response.definitions = Some(
self.get_entity_type_resolve_definitions(actor_id, &ids, true)
.await?,
);
}
}
}

Expand Down Expand Up @@ -1029,11 +1063,10 @@ where
after: None,
limit: None,
include_count: false,
include_entity_types: Some(if params.include_resolved {
IncludeEntityTypeOption::Resolved
} else {
IncludeEntityTypeOption::Closed
}),
include_entity_types: Some(params.include_resolved.map_or(
IncludeEntityTypeOption::Closed,
IncludeEntityTypeOption::from,
)),
include_web_ids: false,
include_edition_created_by_ids: false,
})
Expand Down
12 changes: 6 additions & 6 deletions libs/@local/graph/store/src/entity_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ pub(crate) use self::query::EntityTypeQueryPathVisitor;
pub use self::{
query::{EntityTypeQueryPath, EntityTypeQueryToken},
store::{
ArchiveEntityTypeParams, CountEntityTypesParams, CreateEntityTypeParams,
EntityTypeResolveDefinitions, EntityTypeStore, GetClosedMultiEntityTypeParams,
GetClosedMultiEntityTypeResponse, GetEntityTypeSubgraphParams,
GetEntityTypeSubgraphResponse, GetEntityTypesParams, GetEntityTypesResponse,
IncludeEntityTypeOption, UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams,
UpdateEntityTypesParams,
ArchiveEntityTypeParams, ClosedDataTypeDefinition, CountEntityTypesParams,
CreateEntityTypeParams, EntityTypeResolveDefinitions, EntityTypeStore,
GetClosedMultiEntityTypeParams, GetClosedMultiEntityTypeResponse,
GetEntityTypeSubgraphParams, GetEntityTypeSubgraphResponse, GetEntityTypesParams,
GetEntityTypesResponse, IncludeEntityTypeOption, IncludeResolvedEntityTypeOption,
UnarchiveEntityTypeParams, UpdateEntityTypeEmbeddingParams, UpdateEntityTypesParams,
},
};

Expand Down
Loading

0 comments on commit 5f4b811

Please sign in to comment.