From de40d251e27d73e3d7506b773480dd0d4e11ac3a Mon Sep 17 00:00:00 2001 From: Chris Collins Date: Wed, 18 Sep 2024 18:32:49 -0400 Subject: [PATCH] feat(structuredProps) Add delete structured props endpoint and handle null props (#11418) --- .../datahub/graphql/GmsGraphQLEngine.java | 7 +++ .../DeleteStructuredPropertyResolver.java | 52 +++++++++++++++++++ .../StructuredPropertyMapper.java | 23 ++++++++ .../src/main/resources/properties.graphql | 20 +++++++ .../shared/tabs/Dataset/Schema/SchemaTab.tsx | 5 +- .../SchemaFieldDrawer/FieldProperties.tsx | 6 ++- .../Properties/useStructuredProperties.tsx | 35 +++++++------ .../src/graphql/fragments.graphql | 1 + 8 files changed, 130 insertions(+), 19 deletions(-) create mode 100644 datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/structuredproperties/DeleteStructuredPropertyResolver.java diff --git a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/GmsGraphQLEngine.java b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/GmsGraphQLEngine.java index bb4c26d89f5cea..d38c1030b61be8 100644 --- a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/GmsGraphQLEngine.java +++ b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/GmsGraphQLEngine.java @@ -293,6 +293,7 @@ import com.linkedin.datahub.graphql.resolvers.step.BatchGetStepStatesResolver; import com.linkedin.datahub.graphql.resolvers.step.BatchUpdateStepStatesResolver; import com.linkedin.datahub.graphql.resolvers.structuredproperties.CreateStructuredPropertyResolver; +import com.linkedin.datahub.graphql.resolvers.structuredproperties.DeleteStructuredPropertyResolver; import com.linkedin.datahub.graphql.resolvers.structuredproperties.RemoveStructuredPropertiesResolver; import com.linkedin.datahub.graphql.resolvers.structuredproperties.UpdateStructuredPropertyResolver; import com.linkedin.datahub.graphql.resolvers.structuredproperties.UpsertStructuredPropertiesResolver; @@ -1344,6 +1345,9 @@ private void configureMutationResolvers(final RuntimeWiring.Builder builder) { .dataFetcher( "updateStructuredProperty", new UpdateStructuredPropertyResolver(this.entityClient)) + .dataFetcher( + "deleteStructuredProperty", + new DeleteStructuredPropertyResolver(this.entityClient)) .dataFetcher("raiseIncident", new RaiseIncidentResolver(this.entityClient)) .dataFetcher( "updateIncidentStatus", @@ -2117,6 +2121,9 @@ private void configureStructuredPropertyResolvers(final RuntimeWiring.Builder bu .getAllowedTypes().stream() .map(entityTypeType.getKeyProvider()) .collect(Collectors.toList())))); + builder.type( + "StructuredPropertyEntity", + typeWiring -> typeWiring.dataFetcher("exists", new EntityExistsResolver(entityService))); } /** diff --git a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/structuredproperties/DeleteStructuredPropertyResolver.java b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/structuredproperties/DeleteStructuredPropertyResolver.java new file mode 100644 index 00000000000000..e7d59494654fdd --- /dev/null +++ b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/structuredproperties/DeleteStructuredPropertyResolver.java @@ -0,0 +1,52 @@ +package com.linkedin.datahub.graphql.resolvers.structuredproperties; + +import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.bindArgument; + +import com.linkedin.common.urn.Urn; +import com.linkedin.common.urn.UrnUtils; +import com.linkedin.datahub.graphql.QueryContext; +import com.linkedin.datahub.graphql.authorization.AuthorizationUtils; +import com.linkedin.datahub.graphql.exception.AuthorizationException; +import com.linkedin.datahub.graphql.generated.DeleteStructuredPropertyInput; +import com.linkedin.entity.client.EntityClient; +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import javax.annotation.Nonnull; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class DeleteStructuredPropertyResolver implements DataFetcher> { + + private final EntityClient _entityClient; + + public DeleteStructuredPropertyResolver(@Nonnull final EntityClient entityClient) { + _entityClient = Objects.requireNonNull(entityClient, "entityClient must not be null"); + } + + @Override + public CompletableFuture get(final DataFetchingEnvironment environment) + throws Exception { + final QueryContext context = environment.getContext(); + + final DeleteStructuredPropertyInput input = + bindArgument(environment.getArgument("input"), DeleteStructuredPropertyInput.class); + final Urn propertyUrn = UrnUtils.getUrn(input.getUrn()); + + return CompletableFuture.supplyAsync( + () -> { + try { + if (!AuthorizationUtils.canManageStructuredProperties(context)) { + throw new AuthorizationException( + "Unable to delete structured property. Please contact your admin."); + } + _entityClient.deleteEntity(context.getOperationContext(), propertyUrn); + return true; + } catch (Exception e) { + throw new RuntimeException( + String.format("Failed to perform update against input %s", input), e); + } + }); + } +} diff --git a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/types/structuredproperty/StructuredPropertyMapper.java b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/types/structuredproperty/StructuredPropertyMapper.java index cacb6958dc2020..c539c65118ac6d 100644 --- a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/types/structuredproperty/StructuredPropertyMapper.java +++ b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/types/structuredproperty/StructuredPropertyMapper.java @@ -2,7 +2,9 @@ import static com.linkedin.metadata.Constants.*; +import com.google.common.collect.ImmutableList; import com.linkedin.common.urn.Urn; +import com.linkedin.common.urn.UrnUtils; import com.linkedin.data.DataMap; import com.linkedin.data.template.StringArrayMap; import com.linkedin.datahub.graphql.QueryContext; @@ -46,6 +48,9 @@ public StructuredPropertyEntity apply( final StructuredPropertyEntity result = new StructuredPropertyEntity(); result.setUrn(entityResponse.getUrn().toString()); result.setType(EntityType.STRUCTURED_PROPERTY); + // set the default required values for a structured property in case references are still being + // cleaned up + setDefaultProperty(result); EnvelopedAspectMap aspectMap = entityResponse.getAspects(); MappingHelper mappingHelper = new MappingHelper<>(aspectMap, result); mappingHelper.mapToResult( @@ -134,4 +139,22 @@ private EntityTypeEntity createEntityTypeEntity(final String entityTypeUrnStr) { entityType.setType(EntityType.ENTITY_TYPE); return entityType; } + + /* + * In the case that a property is deleted and the references haven't been cleaned up yet (this process is async) + * set a default property to prevent APIs breaking. The UI queries for whether the entity exists and it will + * be filtered out. + */ + private void setDefaultProperty(final StructuredPropertyEntity result) { + StructuredPropertyDefinition definition = new StructuredPropertyDefinition(); + definition.setQualifiedName(""); + definition.setCardinality(PropertyCardinality.SINGLE); + definition.setImmutable(true); + definition.setValueType( + createDataTypeEntity(UrnUtils.getUrn("urn:li:dataType:datahub.string"))); + definition.setEntityTypes( + ImmutableList.of( + createEntityTypeEntity(UrnUtils.getUrn("urn:li:entityType:datahub.dataset")))); + result.setDefinition(definition); + } } diff --git a/datahub-graphql-core/src/main/resources/properties.graphql b/datahub-graphql-core/src/main/resources/properties.graphql index 6c1f910e02b0ee..292381d064f362 100644 --- a/datahub-graphql-core/src/main/resources/properties.graphql +++ b/datahub-graphql-core/src/main/resources/properties.graphql @@ -18,6 +18,11 @@ extend type Mutation { Update an existing structured property """ updateStructuredProperty(input: UpdateStructuredPropertyInput!): StructuredPropertyEntity! + + """ + Delete an existing structured property + """ + deleteStructuredProperty(input: DeleteStructuredPropertyInput!): Boolean! } """ @@ -34,6 +39,11 @@ type StructuredPropertyEntity implements Entity { """ type: EntityType! + """ + Whether or not this entity exists on DataHub + """ + exists: Boolean + """ Definition of this structured property including its name """ @@ -457,3 +467,13 @@ input UpdateTypeQualifierInput { """ newAllowedTypes: [String!] } + +""" +Input for deleting a form +""" +input DeleteStructuredPropertyInput { + """ + The urn of the structured properties that is being deleted + """ + urn: String! +} diff --git a/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/SchemaTab.tsx b/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/SchemaTab.tsx index f9d7c8db025690..52c141282c3456 100644 --- a/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/SchemaTab.tsx +++ b/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/SchemaTab.tsx @@ -79,7 +79,10 @@ export const SchemaTab = ({ properties }: { properties?: any }) => { const hasProperties = useMemo( () => entityWithSchema?.schemaMetadata?.fields?.some( - (schemaField) => !!schemaField.schemaFieldEntity?.structuredProperties?.properties?.length, + (schemaField) => + !!schemaField.schemaFieldEntity?.structuredProperties?.properties?.filter( + (prop) => prop.structuredProperty.exists, + )?.length, ), [entityWithSchema], ); diff --git a/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/components/SchemaFieldDrawer/FieldProperties.tsx b/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/components/SchemaFieldDrawer/FieldProperties.tsx index 689a191f469f53..9a0da20f22dfde 100644 --- a/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/components/SchemaFieldDrawer/FieldProperties.tsx +++ b/datahub-web-react/src/app/entity/shared/tabs/Dataset/Schema/components/SchemaFieldDrawer/FieldProperties.tsx @@ -34,14 +34,16 @@ interface Props { export default function FieldProperties({ expandedField }: Props) { const { schemaFieldEntity } = expandedField; const { refetch } = useGetEntityWithSchema(true); + const properties = + schemaFieldEntity?.structuredProperties?.properties?.filter((prop) => prop.structuredProperty.exists) || []; - if (!schemaFieldEntity?.structuredProperties?.properties?.length) return null; + if (!schemaFieldEntity || !properties.length) return null; return ( <> Properties - {schemaFieldEntity.structuredProperties.properties.map((structuredProp) => { + {properties.map((structuredProp) => { const isRichText = structuredProp.structuredProperty.definition.valueType?.info.type === StdDataType.RichText; const valuesData = mapStructuredPropertyValues(structuredProp); diff --git a/datahub-web-react/src/app/entity/shared/tabs/Properties/useStructuredProperties.tsx b/datahub-web-react/src/app/entity/shared/tabs/Properties/useStructuredProperties.tsx index 5600d7c3e8498e..86365b8232905c 100644 --- a/datahub-web-react/src/app/entity/shared/tabs/Properties/useStructuredProperties.tsx +++ b/datahub-web-react/src/app/entity/shared/tabs/Properties/useStructuredProperties.tsx @@ -27,23 +27,26 @@ export function mapStructuredPropertyValues(structuredPropertiesEntry: Structure function getStructuredPropertyRows(entityData?: GenericEntityProperties | null) { const structuredPropertyRows: PropertyRow[] = []; - entityData?.structuredProperties?.properties?.forEach((structuredPropertiesEntry) => { - const { displayName, qualifiedName } = structuredPropertiesEntry.structuredProperty.definition; - structuredPropertyRows.push({ - displayName: displayName || qualifiedName, - qualifiedName, - values: mapStructuredPropertyValues(structuredPropertiesEntry), - dataType: structuredPropertiesEntry.structuredProperty.definition.valueType, - structuredProperty: structuredPropertiesEntry.structuredProperty, - type: - structuredPropertiesEntry.values[0] && structuredPropertiesEntry.values[0].__typename - ? { - type: typeNameToType[structuredPropertiesEntry.values[0].__typename].type, - nativeDataType: typeNameToType[structuredPropertiesEntry.values[0].__typename].nativeDataType, - } - : undefined, + entityData?.structuredProperties?.properties + ?.filter((prop) => prop.structuredProperty.exists) + .forEach((structuredPropertiesEntry) => { + const { displayName, qualifiedName } = structuredPropertiesEntry.structuredProperty.definition; + structuredPropertyRows.push({ + displayName: displayName || qualifiedName, + qualifiedName, + values: mapStructuredPropertyValues(structuredPropertiesEntry), + dataType: structuredPropertiesEntry.structuredProperty.definition.valueType, + structuredProperty: structuredPropertiesEntry.structuredProperty, + type: + structuredPropertiesEntry.values[0] && structuredPropertiesEntry.values[0].__typename + ? { + type: typeNameToType[structuredPropertiesEntry.values[0].__typename].type, + nativeDataType: + typeNameToType[structuredPropertiesEntry.values[0].__typename].nativeDataType, + } + : undefined, + }); }); - }); return structuredPropertyRows; } diff --git a/datahub-web-react/src/graphql/fragments.graphql b/datahub-web-react/src/graphql/fragments.graphql index e5bbb5f0dc29d5..18742397517238 100644 --- a/datahub-web-react/src/graphql/fragments.graphql +++ b/datahub-web-react/src/graphql/fragments.graphql @@ -1314,6 +1314,7 @@ fragment structuredPropertyFields on StructuredPropertyEntity { fragment structuredPropertiesFields on StructuredPropertiesEntry { structuredProperty { + exists ...structuredPropertyFields } values {