forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forms) Add CRUD endpoints to GraphQL for Form entities (datahub-…
- Loading branch information
1 parent
cf78d4d
commit 89fb331
Showing
13 changed files
with
921 additions
and
5 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
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
83 changes: 83 additions & 0 deletions
83
...ql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/form/CreateFormResolver.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,83 @@ | ||
package com.linkedin.datahub.graphql.resolvers.form; | ||
|
||
import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.bindArgument; | ||
|
||
import com.linkedin.common.urn.Urn; | ||
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.CreateFormInput; | ||
import com.linkedin.datahub.graphql.generated.CreatePromptInput; | ||
import com.linkedin.datahub.graphql.generated.Form; | ||
import com.linkedin.datahub.graphql.generated.FormPromptType; | ||
import com.linkedin.datahub.graphql.resolvers.mutate.util.FormUtils; | ||
import com.linkedin.datahub.graphql.types.form.FormMapper; | ||
import com.linkedin.entity.EntityResponse; | ||
import com.linkedin.entity.client.EntityClient; | ||
import com.linkedin.form.FormInfo; | ||
import com.linkedin.metadata.Constants; | ||
import com.linkedin.metadata.service.FormService; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class CreateFormResolver implements DataFetcher<CompletableFuture<Form>> { | ||
|
||
private final EntityClient _entityClient; | ||
private final FormService _formService; | ||
|
||
public CreateFormResolver( | ||
@Nonnull final EntityClient entityClient, @Nonnull final FormService formService) { | ||
_entityClient = Objects.requireNonNull(entityClient, "entityClient must not be null"); | ||
_formService = Objects.requireNonNull(formService, "formService must not be null"); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Form> get(final DataFetchingEnvironment environment) throws Exception { | ||
final QueryContext context = environment.getContext(); | ||
|
||
final CreateFormInput input = | ||
bindArgument(environment.getArgument("input"), CreateFormInput.class); | ||
final FormInfo formInfo = FormUtils.mapFormInfo(input); | ||
|
||
return CompletableFuture.supplyAsync( | ||
() -> { | ||
try { | ||
if (!AuthorizationUtils.canManageForms(context)) { | ||
throw new AuthorizationException("Unable to create form. Please contact your admin."); | ||
} | ||
validatePrompts(input.getPrompts()); | ||
|
||
Urn formUrn = | ||
_formService.createForm(context.getOperationContext(), formInfo, input.getId()); | ||
EntityResponse response = | ||
_entityClient.getV2( | ||
context.getOperationContext(), Constants.FORM_ENTITY_NAME, formUrn, null); | ||
return FormMapper.map(context, response); | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
String.format("Failed to perform update against input %s", input), e); | ||
} | ||
}); | ||
} | ||
|
||
private void validatePrompts(@Nullable List<CreatePromptInput> prompts) { | ||
if (prompts == null) { | ||
return; | ||
} | ||
prompts.forEach( | ||
prompt -> { | ||
if (prompt.getType().equals(FormPromptType.STRUCTURED_PROPERTY) | ||
|| prompt.getType().equals(FormPromptType.FIELDS_STRUCTURED_PROPERTY)) { | ||
if (prompt.getStructuredPropertyParams() == null) { | ||
throw new IllegalArgumentException( | ||
"Provided prompt with type STRUCTURED_PROPERTY or FIELDS_STRUCTURED_PROPERTY and no structured property params"); | ||
} | ||
} | ||
}); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/form/DeleteFormResolver.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,65 @@ | ||
package com.linkedin.datahub.graphql.resolvers.form; | ||
|
||
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.DeleteFormInput; | ||
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 DeleteFormResolver implements DataFetcher<CompletableFuture<Boolean>> { | ||
|
||
private final EntityClient _entityClient; | ||
|
||
public DeleteFormResolver(@Nonnull final EntityClient entityClient) { | ||
_entityClient = Objects.requireNonNull(entityClient, "entityClient must not be null"); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Boolean> get(final DataFetchingEnvironment environment) | ||
throws Exception { | ||
final QueryContext context = environment.getContext(); | ||
|
||
final DeleteFormInput input = | ||
bindArgument(environment.getArgument("input"), DeleteFormInput.class); | ||
final Urn formUrn = UrnUtils.getUrn(input.getUrn()); | ||
|
||
return CompletableFuture.supplyAsync( | ||
() -> { | ||
try { | ||
if (!AuthorizationUtils.canManageForms(context)) { | ||
throw new AuthorizationException("Unable to delete form. Please contact your admin."); | ||
} | ||
_entityClient.deleteEntity(context.getOperationContext(), formUrn); | ||
// Asynchronously Delete all references to the entity (to return quickly) | ||
CompletableFuture.runAsync( | ||
() -> { | ||
try { | ||
_entityClient.deleteEntityReferences(context.getOperationContext(), formUrn); | ||
} catch (Exception e) { | ||
log.error( | ||
String.format( | ||
"Caught exception while attempting to clear all entity references for Form with urn %s", | ||
formUrn), | ||
e); | ||
} | ||
}); | ||
|
||
return true; | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
String.format("Failed to perform update against input %s", input), e); | ||
} | ||
}); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...ql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/form/UpdateFormResolver.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,98 @@ | ||
package com.linkedin.datahub.graphql.resolvers.form; | ||
|
||
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.Form; | ||
import com.linkedin.datahub.graphql.generated.UpdateFormInput; | ||
import com.linkedin.datahub.graphql.resolvers.mutate.util.FormUtils; | ||
import com.linkedin.datahub.graphql.types.form.FormMapper; | ||
import com.linkedin.entity.EntityResponse; | ||
import com.linkedin.entity.client.EntityClient; | ||
import com.linkedin.form.FormType; | ||
import com.linkedin.metadata.Constants; | ||
import com.linkedin.metadata.aspect.patch.builder.FormInfoPatchBuilder; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.Objects; | ||
import java.util.concurrent.CompletableFuture; | ||
import javax.annotation.Nonnull; | ||
|
||
public class UpdateFormResolver implements DataFetcher<CompletableFuture<Form>> { | ||
|
||
private final EntityClient _entityClient; | ||
|
||
public UpdateFormResolver(@Nonnull final EntityClient entityClient) { | ||
_entityClient = Objects.requireNonNull(entityClient, "entityClient must not be null"); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Form> get(final DataFetchingEnvironment environment) throws Exception { | ||
final QueryContext context = environment.getContext(); | ||
|
||
final UpdateFormInput input = | ||
bindArgument(environment.getArgument("input"), UpdateFormInput.class); | ||
final Urn formUrn = UrnUtils.getUrn(input.getUrn()); | ||
|
||
return CompletableFuture.supplyAsync( | ||
() -> { | ||
try { | ||
if (!AuthorizationUtils.canManageForms(context)) { | ||
throw new AuthorizationException("Unable to update form. Please contact your admin."); | ||
} | ||
if (!_entityClient.exists(context.getOperationContext(), formUrn)) { | ||
throw new IllegalArgumentException( | ||
String.format("Form with urn %s does not exist", formUrn)); | ||
} | ||
|
||
FormInfoPatchBuilder patchBuilder = new FormInfoPatchBuilder().urn(formUrn); | ||
if (input.getName() != null) { | ||
patchBuilder.setName(input.getName()); | ||
} | ||
if (input.getDescription() != null) { | ||
patchBuilder.setDescription(input.getDescription()); | ||
} | ||
if (input.getType() != null) { | ||
patchBuilder.setType(FormType.valueOf(input.getType().toString())); | ||
} | ||
if (input.getPromptsToAdd() != null) { | ||
patchBuilder.addPrompts(FormUtils.mapPromptsToAdd(input.getPromptsToAdd())); | ||
} | ||
if (input.getPromptsToRemove() != null) { | ||
patchBuilder.removePrompts(input.getPromptsToRemove()); | ||
} | ||
if (input.getActors() != null) { | ||
if (input.getActors().getOwners() != null) { | ||
patchBuilder.setOwnershipForm(input.getActors().getOwners()); | ||
} | ||
if (input.getActors().getUsersToAdd() != null) { | ||
input.getActors().getUsersToAdd().forEach(patchBuilder::addAssignedUser); | ||
} | ||
if (input.getActors().getUsersToRemove() != null) { | ||
input.getActors().getUsersToRemove().forEach(patchBuilder::removeAssignedUser); | ||
} | ||
if (input.getActors().getGroupsToAdd() != null) { | ||
input.getActors().getGroupsToAdd().forEach(patchBuilder::addAssignedGroup); | ||
} | ||
if (input.getActors().getGroupsToRemove() != null) { | ||
input.getActors().getGroupsToRemove().forEach(patchBuilder::removeAssignedGroup); | ||
} | ||
} | ||
_entityClient.ingestProposal( | ||
context.getOperationContext(), patchBuilder.build(), false); | ||
|
||
EntityResponse response = | ||
_entityClient.getV2( | ||
context.getOperationContext(), Constants.FORM_ENTITY_NAME, formUrn, null); | ||
return FormMapper.map(context, response); | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
String.format("Failed to perform update against input %s", input), e); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.