Skip to content

Commit

Permalink
more generic graphql resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Eilers committed Aug 28, 2023
1 parent eacccb0 commit d98cf77
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import com.linkedin.datahub.graphql.generated.Notebook;
import com.linkedin.datahub.graphql.generated.Owner;
import com.linkedin.datahub.graphql.generated.OwnershipTypeEntity;
import com.linkedin.datahub.graphql.generated.ParentDomainsResult;
import com.linkedin.datahub.graphql.generated.PolicyMatchCriterionValue;
import com.linkedin.datahub.graphql.generated.QueryEntity;
import com.linkedin.datahub.graphql.generated.QuerySubject;
Expand Down Expand Up @@ -1025,6 +1026,13 @@ private void configureGenericEntityResolvers(final RuntimeWiring.Builder builder
.dataFetcher("entities", new EntityTypeBatchResolver(entityTypes,
(env) -> ((BrowseResults) env.getSource()).getEntities()))
)
.type("ParentDomainsResult", typeWiring -> typeWiring
.dataFetcher("domains", new EntityTypeBatchResolver(entityTypes,
(env) -> {
final ParentDomainsResult result = env.getSource();
return result != null ? result.getDomains() : null;
}))
)
.type("EntityRelationshipLegacy", typeWiring -> typeWiring
.dataFetcher("entity", new EntityTypeResolver(entityTypes,
(env) -> ((EntityRelationshipLegacy) env.getSource()).getEntity()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.generated.Domain;
import com.linkedin.datahub.graphql.generated.Entity;
import com.linkedin.datahub.graphql.generated.ParentDomainsResult;
import com.linkedin.datahub.graphql.resolvers.mutate.util.DomainUtils;
Expand All @@ -29,25 +28,24 @@ public ParentDomainsResolver(final EntityClient entityClient) {
public CompletableFuture<ParentDomainsResult> get(DataFetchingEnvironment environment) {
final QueryContext context = environment.getContext();
final Urn urn = UrnUtils.getUrn(((Entity) environment.getSource()).getUrn());
final List<Domain> domains = new ArrayList<>();

final List<Entity> parentDomains = new ArrayList<>();

if (!DOMAIN_ENTITY_NAME.equals(urn.getEntityType())) {
throw new IllegalArgumentException(String.format("Failed to resolve parents for entity type %s", urn));
}

return CompletableFuture.supplyAsync(() -> {
try {
Domain parentDomain = DomainUtils.getParentDomain(urn.toString(), context, _entityClient);
Entity parentDomain = DomainUtils.getParentDomain(urn, context, _entityClient);

while (parentDomain != null) {
domains.add(parentDomain);
parentDomain = DomainUtils.getParentDomain(parentDomain.getUrn(), context, _entityClient);
parentDomains.add(parentDomain);
parentDomain = DomainUtils.getParentDomain(Urn.createFromString(parentDomain.getUrn()), context, _entityClient);
}

final ParentDomainsResult result = new ParentDomainsResult();
result.setCount(domains.size());
result.setDomains(domains);
result.setCount(parentDomains.size());
result.setDomains(parentDomains);
return result;
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to load parent domains for entity %s", urn), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.linkedin.datahub.graphql.authorization.AuthorizationUtils;
import com.datahub.authorization.ConjunctivePrivilegeGroup;
import com.datahub.authorization.DisjunctivePrivilegeGroup;
import com.linkedin.datahub.graphql.generated.Domain;
import com.linkedin.datahub.graphql.generated.Entity;
import com.linkedin.datahub.graphql.generated.ResourceRefInput;
import com.linkedin.datahub.graphql.types.domain.DomainMapper;
import com.linkedin.datahub.graphql.types.common.mappers.UrnToEntityMapper;
import com.linkedin.domain.DomainProperties;
import com.linkedin.domain.Domains;
import com.linkedin.entity.EntityResponse;
Expand Down Expand Up @@ -182,41 +182,40 @@ public static boolean hasNameConflict(
}

@Nullable
public static Domain getParentDomain(
@Nonnull final String urnStr,
public static Entity getParentDomain(
@Nonnull final Urn urn,
@Nonnull final QueryContext context,
@Nonnull final EntityClient entityClient
) {
try {
Urn urn = Urn.createFromString(urnStr);
EntityResponse entityResponse = entityClient.getV2(
final EntityResponse entityResponse = entityClient.getV2(
urn.getEntityType(),
urn,
Collections.singleton(DOMAIN_PROPERTIES_ASPECT_NAME),
context.getAuthentication()
);

if (entityResponse != null && entityResponse.getAspects().containsKey(DOMAIN_PROPERTIES_ASPECT_NAME)) {
DomainProperties properties = new DomainProperties(entityResponse.getAspects().get(DOMAIN_PROPERTIES_ASPECT_NAME).getValue().data());
final DomainProperties properties = new DomainProperties(entityResponse.getAspects().get(DOMAIN_PROPERTIES_ASPECT_NAME).getValue().data());
if (properties.hasParentDomain()) {
Urn parentDomainUrn = properties.getParentDomain();
final Urn parentDomainUrn = properties.getParentDomain();
if (parentDomainUrn != null) {
EntityResponse parentResponse = entityClient.getV2(
final EntityResponse parentResponse = entityClient.getV2(
parentDomainUrn.getEntityType(),
parentDomainUrn,
Collections.singleton(DOMAIN_KEY_ASPECT_NAME),
null,
context.getAuthentication()
);
if (parentResponse != null) {
return DomainMapper.map(parentResponse);
return UrnToEntityMapper.map(parentResponse.getUrn());
}
}
}
}

return null;
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to retrieve parent domain for entity %s", urnStr), e);
throw new RuntimeException(String.format("Failed to retrieve parent domain for entity %s", urn), e);
}
}
}
2 changes: 1 addition & 1 deletion datahub-graphql-core/src/main/resources/entity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9603,7 +9603,7 @@ type ParentDomainsResult {
"""
A list of parent domains in order from direct parent, to parent's parent etc. If there are no parents, return an empty list
"""
domains: [Domain!]!
domains: [Entity!]!
}

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.linkedin.entity.EnvelopedAspect;
import com.linkedin.entity.EnvelopedAspectMap;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.key.DomainKey;
import graphql.schema.DataFetchingEnvironment;
import lombok.extern.slf4j.Slf4j;
import org.mockito.Mockito;
Expand Down Expand Up @@ -55,17 +54,11 @@ public void testGetSuccessForDomain() throws Exception {
parentDomain1Aspects.put(DOMAIN_PROPERTIES_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect(
new DomainProperties().setName("domain parent 1").setParentDomain(parentDomain2.getParentDomain()).data()
)));
parentDomain1Aspects.put(DOMAIN_KEY_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect(
new DomainKey().setId("domain-parent-1-key").data()
)));

Map<String, EnvelopedAspect> parentDomain2Aspects = new HashMap<>();
parentDomain2Aspects.put(DOMAIN_PROPERTIES_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect(
new DomainProperties().setName("domain parent 2").data()
)));
parentDomain2Aspects.put(DOMAIN_KEY_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect(
new DomainKey().setId("domain-parent-2-key").data()
)));

Mockito.when(mockClient.getV2(
Mockito.eq(domainUrn.getEntityType()),
Expand All @@ -77,7 +70,7 @@ public void testGetSuccessForDomain() throws Exception {
Mockito.when(mockClient.getV2(
Mockito.eq(parentDomain1.getParentDomain().getEntityType()),
Mockito.eq(parentDomain1.getParentDomain()),
Mockito.eq(Collections.singleton(DOMAIN_KEY_ASPECT_NAME)),
Mockito.eq(null),
Mockito.any(Authentication.class)
)).thenReturn(new EntityResponse()
.setEntityName(DOMAIN_ENTITY_NAME)
Expand All @@ -94,7 +87,7 @@ public void testGetSuccessForDomain() throws Exception {
Mockito.when(mockClient.getV2(
Mockito.eq(parentDomain2.getParentDomain().getEntityType()),
Mockito.eq(parentDomain2.getParentDomain()),
Mockito.eq(Collections.singleton(DOMAIN_KEY_ASPECT_NAME)),
Mockito.eq(null),
Mockito.any(Authentication.class)
)).thenReturn(new EntityResponse()
.setEntityName(DOMAIN_ENTITY_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ export const ManagePolicies = () => {
variables: {
input: {
start,
count: pageSize,
count: pageSize + 10,
query,
},
},
});
console.log({ policiesData });

// Any time a policy is removed, edited, or created, refetch the list.
const [createPolicy, { error: createPolicyError }] = useCreatePolicyMutation();
Expand Down
13 changes: 5 additions & 8 deletions datahub-web-react/src/graphql/domain.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ query listDomains($input: ListDomainsInput!) {
domains {
urn
type
parentDomains {
urn
type
}
properties {
name
description
Expand All @@ -44,14 +48,7 @@ query listDomains($input: ListDomainsInput!) {
entities(input: { start: 0, count: 1 }) {
total
}
children: relationships(
input: {
types: ["IsPartOf"]
direction: INCOMING
start: 0
count: 0
}
) {
children: relationships(input: { types: ["IsPartOf"], direction: INCOMING, start: 0, count: 0 }) {
total
}
}
Expand Down

0 comments on commit d98cf77

Please sign in to comment.