Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): add graphql endpoint to check whether an entity exists #5102

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import com.linkedin.datahub.graphql.resolvers.domain.ListDomainsResolver;
import com.linkedin.datahub.graphql.resolvers.domain.SetDomainResolver;
import com.linkedin.datahub.graphql.resolvers.domain.UnsetDomainResolver;
import com.linkedin.datahub.graphql.resolvers.entity.EntityExistsResolver;
import com.linkedin.datahub.graphql.resolvers.glossary.CreateGlossaryNodeResolver;
import com.linkedin.datahub.graphql.resolvers.glossary.CreateGlossaryTermResolver;
import com.linkedin.datahub.graphql.resolvers.glossary.DeleteGlossaryEntityResolver;
Expand Down Expand Up @@ -621,6 +622,7 @@ private void configureQueryResolvers(final RuntimeWiring.Builder builder) {
.dataFetcher("listTests", new ListTestsResolver(entityClient))
.dataFetcher("getRootGlossaryTerms", new GetRootGlossaryTermsResolver(this.entityClient))
.dataFetcher("getRootGlossaryNodes", new GetRootGlossaryNodesResolver(this.entityClient))
.dataFetcher("getDoesEntityExist", new EntityExistsResolver(this.entityService))
aditya-radhakrishnan marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.linkedin.datahub.graphql.resolvers.entity;

import com.linkedin.common.urn.Urn;
import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.*;


/**
* Resolver responsible for returning whether an entity exists.
*/
public class EntityExistsResolver implements DataFetcher<CompletableFuture<Boolean>> {
private final EntityService _entityService;

public EntityExistsResolver(final EntityService entityService) {
_entityService = entityService;
}

@Override
public CompletableFuture<Boolean> get(final DataFetchingEnvironment environment) throws Exception {
final String entityUrnString = bindArgument(environment.getArgument("urn"), String.class);
Objects.requireNonNull(entityUrnString, "Entity urn must not be null!");

Urn entityUrn = Urn.createFromString(entityUrnString);
return CompletableFuture.supplyAsync(() -> {
try {
return _entityService.exists(entityUrn);
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to check whether entity %s exists", entityUrnString));
}
});
}
}
5 changes: 5 additions & 0 deletions datahub-graphql-core/src/main/resources/entity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ type Query {
Get all GlossaryNodes without a parentNode
"""
getRootGlossaryNodes(input: GetRootGlossaryEntitiesInput!): GetRootGlossaryNodesResult

"""
Get whether or not not an entity exists
"""
getDoesEntityExist(urn: String!): Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still has wrong name!

}

"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.linkedin.datahub.graphql.resolvers.entity;

import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetchingEnvironment;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.*;
import static org.testng.Assert.*;


public class EntityExistsResolverTest {
private static final String ENTITY_URN_STRING = "urn:li:corpuser:test";

private EntityService _entityService;
private DataFetchingEnvironment _dataFetchingEnvironment;
private EntityExistsResolver _resolver;

@BeforeMethod
public void setupTest() {
_entityService = mock(EntityService.class);
_dataFetchingEnvironment = mock(DataFetchingEnvironment.class);

_resolver = new EntityExistsResolver(_entityService);
}

@Test
public void testFailsNullEntity() {
when(_dataFetchingEnvironment.getArgument("urn")).thenReturn(null);

assertThrows(() -> _resolver.get(_dataFetchingEnvironment).join());
}

@Test
public void testPasses() throws Exception {
when(_dataFetchingEnvironment.getArgument("urn")).thenReturn(ENTITY_URN_STRING);
when(_entityService.exists(any())).thenReturn(true);

assertTrue(_resolver.get(_dataFetchingEnvironment).join());
}
}