Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usergroup): implement corpgroup in graphql, refactor avatars and…
… ownership in react (#2519)
- Loading branch information
1 parent
2811d23
commit d7d8870
Showing
39 changed files
with
999 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ MANIFEST | |
|
||
# Mac OS | ||
**/.DS_Store | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...hql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/load/OwnerTypeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.linkedin.datahub.graphql.resolvers.load; | ||
|
||
import com.linkedin.datahub.graphql.generated.Entity; | ||
import com.linkedin.datahub.graphql.generated.OwnerType; | ||
import com.linkedin.datahub.graphql.types.LoadableType; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
import org.dataloader.DataLoader; | ||
import java.util.stream.Collectors; | ||
import com.google.common.collect.Iterables; | ||
|
||
/** | ||
* Generic GraphQL resolver responsible for | ||
* | ||
* 1. Retrieving a single input urn. | ||
* 2. Resolving a single {@link LoadableType}. | ||
* | ||
* Note that this resolver expects that {@link DataLoader}s were registered | ||
* for the provided {@link LoadableType} under the name provided by {@link LoadableType#name()} | ||
* | ||
* @param <T> the generated GraphQL POJO corresponding to the resolved type. | ||
*/ | ||
public class OwnerTypeResolver<T> implements DataFetcher<CompletableFuture<T>> { | ||
|
||
private final List<LoadableType<?>> _loadableTypes; | ||
private final Function<DataFetchingEnvironment, OwnerType> _urnProvider; | ||
|
||
public OwnerTypeResolver(final List<LoadableType<?>> loadableTypes, final Function<DataFetchingEnvironment, OwnerType> urnProvider) { | ||
_loadableTypes = loadableTypes; | ||
_urnProvider = urnProvider; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<T> get(DataFetchingEnvironment environment) { | ||
final OwnerType ownerType = _urnProvider.apply(environment); | ||
final LoadableType<?> filteredEntity = Iterables.getOnlyElement(_loadableTypes.stream() | ||
.filter(entity -> ownerType.getClass().isAssignableFrom(entity.objectClass())) | ||
.collect(Collectors.toList())); | ||
final DataLoader<String, T> loader = environment.getDataLoaderRegistry().getDataLoader(filteredEntity.name()); | ||
return loader.load(((Entity) ownerType).getUrn()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...raphql-core/src/main/java/com/linkedin/datahub/graphql/types/corpgroup/CorpGroupType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.linkedin.datahub.graphql.types.corpgroup; | ||
|
||
import com.linkedin.common.urn.CorpGroupUrn; | ||
import com.linkedin.datahub.graphql.QueryContext; | ||
import com.linkedin.datahub.graphql.generated.EntityType; | ||
import com.linkedin.datahub.graphql.types.SearchableEntityType; | ||
import com.linkedin.datahub.graphql.generated.AutoCompleteResults; | ||
import com.linkedin.datahub.graphql.generated.CorpGroup; | ||
import com.linkedin.datahub.graphql.generated.FacetFilterInput; | ||
import com.linkedin.datahub.graphql.generated.SearchResults; | ||
import com.linkedin.datahub.graphql.types.mappers.AutoCompleteResultsMapper; | ||
import com.linkedin.datahub.graphql.types.corpgroup.mappers.CorpGroupMapper; | ||
import com.linkedin.datahub.graphql.types.mappers.SearchResultsMapper; | ||
import com.linkedin.identity.client.CorpGroups; | ||
import com.linkedin.metadata.query.AutoCompleteResult; | ||
import com.linkedin.restli.common.CollectionResponse; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class CorpGroupType implements SearchableEntityType<CorpGroup> { | ||
|
||
private static final String DEFAULT_AUTO_COMPLETE_FIELD = "name"; | ||
|
||
private final CorpGroups _corpGroupsClient; | ||
|
||
public CorpGroupType(final CorpGroups corpGroupsClient) { | ||
_corpGroupsClient = corpGroupsClient; | ||
} | ||
|
||
@Override | ||
public Class<CorpGroup> objectClass() { | ||
return CorpGroup.class; | ||
} | ||
|
||
@Override | ||
public EntityType type() { | ||
return EntityType.CORP_GROUP; | ||
} | ||
|
||
@Override | ||
public List<CorpGroup> batchLoad(final List<String> urns, final QueryContext context) { | ||
try { | ||
final List<CorpGroupUrn> corpGroupUrns = urns | ||
.stream() | ||
.map(this::getCorpGroupUrn) | ||
.collect(Collectors.toList()); | ||
|
||
final Map<CorpGroupUrn, com.linkedin.identity.CorpGroup> corpGroupMap = _corpGroupsClient | ||
.batchGet(new HashSet<>(corpGroupUrns)); | ||
|
||
final List<com.linkedin.identity.CorpGroup> results = new ArrayList<>(); | ||
for (CorpGroupUrn urn : corpGroupUrns) { | ||
results.add(corpGroupMap.getOrDefault(urn, null)); | ||
} | ||
return results.stream() | ||
.map(gmsCorpGroup -> gmsCorpGroup == null ? null : CorpGroupMapper.map(gmsCorpGroup)) | ||
.collect(Collectors.toList()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to batch load CorpGroup", e); | ||
} | ||
} | ||
|
||
@Override | ||
public SearchResults search(@Nonnull String query, | ||
@Nullable List<FacetFilterInput> filters, | ||
int start, | ||
int count, | ||
@Nonnull final QueryContext context) throws Exception { | ||
final CollectionResponse<com.linkedin.identity.CorpGroup> searchResult = _corpGroupsClient.search(query, Collections.emptyMap(), start, count); | ||
return SearchResultsMapper.map(searchResult, CorpGroupMapper::map); | ||
} | ||
|
||
@Override | ||
public AutoCompleteResults autoComplete(@Nonnull String query, | ||
@Nullable String field, | ||
@Nullable List<FacetFilterInput> filters, | ||
int limit, | ||
@Nonnull final QueryContext context) throws Exception { | ||
field = field != null ? field : DEFAULT_AUTO_COMPLETE_FIELD; | ||
final AutoCompleteResult result = _corpGroupsClient.autocomplete(query, field, Collections.emptyMap(), limit); | ||
return AutoCompleteResultsMapper.map(result); | ||
} | ||
|
||
private CorpGroupUrn getCorpGroupUrn(final String urnStr) { | ||
try { | ||
return CorpGroupUrn.createFromString(urnStr); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(String.format("Failed to retrieve CorpGroup with urn %s, invalid urn", urnStr)); | ||
} | ||
} | ||
} |
Oops, something went wrong.