-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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(forms) Add CRUD endpoints to GraphQL for Form entities #10825
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
}); | ||
Comment on lines
+47
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding more granular error handling. The catch block is catching all exceptions and throwing a RuntimeException. Consider handling specific exceptions to provide more detailed error messages. 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 (AuthorizationException e) {
throw e;
} 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"); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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()); | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding input validation. Validate the final DeleteFormInput input =
bindArgument(environment.getArgument("input"), DeleteFormInput.class);
Objects.requireNonNull(input, "input must not be null"); |
||
|
||
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); | ||
} | ||
}); | ||
Comment on lines
+37
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding more granular error handling. The catch block is catching all exceptions and throwing a RuntimeException. Consider handling specific exceptions to provide more detailed error messages. 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 (AuthorizationException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(
String.format("Failed to perform update against input %s", input), e);
} |
||
} | ||
} |
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); | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding input validation.
Validate the
input
parameter to ensure it is not null before proceeding.