Skip to content

Commit

Permalink
chore(graphql): cleanup, refactor, async execution (#1202)
Browse files Browse the repository at this point in the history
* chore(graphql): refactoring and cleanup

* chore(graphql): set up async wrapper

* mark 'simple' fetchers as non-blocking

* fix unit test

* add parsed query cache

* log GraphQL queries

* add self as CORS origin in dev mode

* missing import

* just log queries, not variables

* fix broken test compile

* request logging touchup

* fix test

* remove unused fetcher
  • Loading branch information
andrewazores authored Nov 8, 2022
1 parent d98d8c0 commit ac9d348
Show file tree
Hide file tree
Showing 35 changed files with 586 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
package io.cryostat.net.web.http.api.v2.graph;

import java.util.Set;

import io.cryostat.net.AuthManager;
import io.cryostat.net.AuthorizationErrorException;
import io.cryostat.net.security.PermissionedAction;
Expand All @@ -55,6 +57,14 @@ abstract class AbstractPermissionedDataFetcher<T> implements DataFetcher<T>, Per
this.auth = auth;
}

abstract Set<String> applicableContexts();

abstract String name();

boolean blocking() {
return true;
}

@Override
public final T get(DataFetchingEnvironment environment) throws Exception {
GraphQLContext graphCtx = environment.getGraphQlContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,8 @@
*/
package io.cryostat.net.web.http.api.v2.graph;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import graphql.schema.TypeResolver;

import javax.inject.Inject;

import io.cryostat.net.AuthManager;
import io.cryostat.net.security.ResourceAction;
import io.cryostat.platform.discovery.AbstractNode;
import io.cryostat.platform.discovery.EnvironmentNode;
import io.cryostat.platform.discovery.TargetNode;

import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;

class EnvironmentNodeRecurseFetcher extends AbstractPermissionedDataFetcher<List<EnvironmentNode>> {

@Inject
EnvironmentNodeRecurseFetcher(AuthManager auth) {
super(auth);
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_TARGET);
}

@Override
public List<EnvironmentNode> getAuthenticated(DataFetchingEnvironment environment)
throws Exception {
AbstractNode node = environment.getSource();
if (node instanceof TargetNode) {
return List.of();
} else if (node instanceof EnvironmentNode) {
EnvironmentNode environmentNode = (EnvironmentNode) node;
List<EnvironmentNode> result = new ArrayList<>();
result.add(environmentNode);
for (AbstractNode child : environmentNode.getChildren()) {
DataFetchingEnvironment newEnv =
DataFetchingEnvironmentImpl.newDataFetchingEnvironment(environment)
.source(child)
.build();
result.addAll(get(newEnv));
}
return result;
} else {
throw new IllegalStateException(node.getClass().toString());
}
}
abstract class AbstractTypeResolver implements TypeResolver {
abstract String typeName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,26 @@ class ActiveRecordingsFetcher extends AbstractPermissionedDataFetcher<Active> {
super(auth);
}

@Override
Set<String> applicableContexts() {
return Set.of("Recordings");
}

@Override
String name() {
return "active";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_RECORDING, ResourceAction.READ_TARGET);
}

@Override
boolean blocking() {
return false;
}

@Override
public Active getAuthenticated(DataFetchingEnvironment environment) throws Exception {
Recordings source = environment.getSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ class AllArchivedRecordingsFetcher extends AbstractPermissionedDataFetcher<Archi
this.logger = logger;
}

@Override
Set<String> applicableContexts() {
return Set.of("Query");
}

@Override
String name() {
return "archivedRecordings";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_RECORDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class ArchiveRecordingMutator extends AbstractPermissionedDataFetcher<ArchivedRe
this.credentialsManager = credentialsManager;
}

@Override
Set<String> applicableContexts() {
return Set.of("ActiveRecording");
}

@Override
String name() {
return "doArchive";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,26 @@ class ArchivedRecordingsFetcher extends AbstractPermissionedDataFetcher<Archived
super(auth);
}

@Override
Set<String> applicableContexts() {
return Set.of("Recordings");
}

@Override
String name() {
return "archived";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_RECORDING);
}

@Override
boolean blocking() {
return false;
}

public Archived getAuthenticated(DataFetchingEnvironment environment) throws Exception {
Recordings source = environment.getSource();
FilterInput filter = FilterInput.from(environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class DeleteActiveRecordingMutator
this.credentialsManager = credentialsManager;
}

@Override
Set<String> applicableContexts() {
return Set.of("ActiveRecording");
}

@Override
String name() {
return "doDelete";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class DeleteArchivedRecordingMutator
this.recordingArchiveHelper = recordingArchiveHelper;
}

@Override
Set<String> applicableContexts() {
return Set.of("ArchivedRecording");
}

@Override
String name() {
return "doDelete";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.DELETE_RECORDING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,26 @@ class EnvironmentNodeChildrenFetcher extends AbstractPermissionedDataFetcher<Lis
super(auth);
}

@Override
Set<String> applicableContexts() {
return Set.of("EnvironmentNode");
}

@Override
String name() {
return "children";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_TARGET);
}

@Override
boolean blocking() {
return false;
}

@Override
public List<AbstractNode> getAuthenticated(DataFetchingEnvironment environment)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,26 @@ class EnvironmentNodesFetcher extends AbstractPermissionedDataFetcher<List<Envir
this.rootNodeFetcher = rootNodeFetcher;
}

@Override
Set<String> applicableContexts() {
return Set.of("Query");
}

@Override
String name() {
return "environmentNodes";
}

@Override
public Set<ResourceAction> resourceActions() {
return EnumSet.of(ResourceAction.READ_TARGET);
}

@Override
boolean blocking() {
return false;
}

@Override
public List<EnvironmentNode> getAuthenticated(DataFetchingEnvironment environment)
throws Exception {
Expand Down
Loading

0 comments on commit ac9d348

Please sign in to comment.