From feee27dcf8e6cf96841958be67e153cf359a1c05 Mon Sep 17 00:00:00 2001 From: Ainur <59531286+yagudin10@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:10:01 +0200 Subject: [PATCH] CB-3581 update java graphql library (#1762) * CB-3581 update java graphql library * CB-3581 update java graphql library --- .../server/graphql/GraphQLEndpoint.java | 94 ++++++++----------- .../test/platform/ResourceManagerTest.java | 15 +-- .../gql_scripts/rmDeleteResource.json | 9 ++ 3 files changed, 56 insertions(+), 62 deletions(-) create mode 100644 server/test/io.cloudbeaver.test.platform/workspace/gql_scripts/rmDeleteResource.json diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java index bb05b64317..c112259e82 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java @@ -18,14 +18,9 @@ package io.cloudbeaver.server.graphql; import com.google.gson.*; import graphql.*; -import graphql.execution.AsyncExecutionStrategy; -import graphql.execution.DataFetcherExceptionHandlerResult; -import graphql.execution.ExecutionPath; -import graphql.execution.instrumentation.SimpleInstrumentation; -import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters; -import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; +import graphql.execution.*; +import graphql.execution.instrumentation.SimplePerformantInstrumentation; import graphql.language.SourceLocation; -import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLSchema; import graphql.schema.idl.SchemaGenerator; @@ -38,9 +33,7 @@ import io.cloudbeaver.registry.WebServiceRegistry; import io.cloudbeaver.server.CBApplication; import io.cloudbeaver.service.DBWServiceBindingGraphQL; import io.cloudbeaver.service.WebServiceBindingBase; -import io.cloudbeaver.utils.WebAppUtils; import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.model.DBConstants; import org.jkiss.utils.IOUtils; import javax.servlet.ServletException; @@ -53,8 +46,10 @@ import java.io.InputStreamReader; import java.io.Reader; import java.lang.reflect.InvocationTargetException; import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; public class GraphQLEndpoint extends HttpServlet { @@ -82,7 +77,7 @@ public class GraphQLEndpoint extends HttpServlet { graphQL = GraphQL .newGraphQL(schema) - .instrumentation(new WebInstrumentation()) + .instrumentation(new SimplePerformantInstrumentation()) .queryExecutionStrategy(new WebExecutionStrategy()) .mutationExecutionStrategy(new WebExecutionStrategy()) .build(); @@ -260,57 +255,44 @@ public class GraphQLEndpoint extends HttpServlet { response.getWriter().print(resString); } - private static class WebInstrumentation extends SimpleInstrumentation { - @Override - public CompletableFuture instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) { - return super.instrumentExecutionResult(executionResult, parameters); - } + private class WebExecutionStrategy extends AsyncExecutionStrategy { - @Override - public DataFetcher instrumentDataFetcher(DataFetcher dataFetcher, InstrumentationFieldFetchParameters parameters) { - return dataFetcher; -// return environment -> { -// try { -// return dataFetcher.get(environment); -// } catch (Exception e) { -// log.debug(e); -// throw e; -// } -// }; + public WebExecutionStrategy() { + super(new WebDataFetcherExceptionHandler()); } } - private class WebExecutionStrategy extends AsyncExecutionStrategy { - public WebExecutionStrategy() { - super(handlerParameters -> { - Throwable exception = handlerParameters.getException(); - if (exception instanceof GraphQLException && exception.getCause() != null) { - exception = exception.getCause(); - } - if (exception instanceof InvocationTargetException) { - exception = ((InvocationTargetException) exception).getTargetException(); - } - log.debug("GraphQL call failed at '" + handlerParameters.getPath() + "'" /*+ ", " + handlerParameters.getArgumentValues()*/, exception); + private class WebDataFetcherExceptionHandler implements DataFetcherExceptionHandler { + @Override + public CompletableFuture handleException(DataFetcherExceptionHandlerParameters handlerParameters) { + Throwable exception = handlerParameters.getException(); + if (exception instanceof GraphQLException && exception.getCause() != null) { + exception = exception.getCause(); + } + if (exception instanceof InvocationTargetException) { + exception = ((InvocationTargetException) exception).getTargetException(); + } + log.debug("GraphQL call failed at '" + handlerParameters.getPath() + "'" /*+ ", " + handlerParameters.getArgumentValues()*/, exception); - // Log in session - WebSession webSession = WebServiceBindingBase.findWebSession(handlerParameters.getDataFetchingEnvironment()); - if (webSession != null) { - webSession.addSessionError(exception); - } + // Log in session + WebSession webSession = WebServiceBindingBase.findWebSession(handlerParameters.getDataFetchingEnvironment()); + if (webSession != null) { + webSession.addSessionError(exception); + } - SourceLocation sourceLocation = handlerParameters.getSourceLocation(); - ExecutionPath path = handlerParameters.getPath(); + SourceLocation sourceLocation = handlerParameters.getSourceLocation(); + ResultPath path = handlerParameters.getPath(); - DataFetcherExceptionHandlerResult.Builder handlerResult = DataFetcherExceptionHandlerResult.newResult(); - if (!(exception instanceof GraphQLError)) { - exception = new DBWebException(exception.getMessage(), exception); - } - if (exception instanceof DBWebException) { - ((DBWebException) exception).setPath(path.toList()); - ((DBWebException) exception).setLocations(Collections.singletonList(sourceLocation)); - } - return handlerResult.error((GraphQLError) exception).build(); - }); + DataFetcherExceptionHandlerResult.Builder handlerResult = DataFetcherExceptionHandlerResult.newResult(); + if (!(exception instanceof GraphQLError)) { + exception = new DBWebException(exception.getMessage(), exception); + } + if (exception instanceof DBWebException) { + ((DBWebException) exception).setPath(path.toList()); + ((DBWebException) exception).setLocations(Collections.singletonList(sourceLocation)); + } + var result = handlerResult.error((GraphQLError) exception).build(); + return CompletableFuture.completedFuture(result); } } diff --git a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ResourceManagerTest.java b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ResourceManagerTest.java index 46746f78be..671903c6aa 100644 --- a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ResourceManagerTest.java +++ b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ResourceManagerTest.java @@ -34,7 +34,7 @@ import java.util.Map; public class ResourceManagerTest { public static final String GQL_TEMPLATE_RM_WRITE_RESOURCE = "rmWriteResource.json"; - public static final String GQL_TEMPLATE_RM_DELETE_RESOURCE = "navDeleteNode.json"; + public static final String GQL_TEMPLATE_RM_DELETE_RESOURCE = "rmDeleteResource.json"; public static final String GQL_READ_EMPTY_PROJECT_ID_RESOURCES = "rmReadEmptyProjectIdResources.json"; private static HttpClient client; @@ -57,7 +57,7 @@ public class ResourceManagerTest { Assert.assertTrue(createResource(client, false)); Assert.assertFalse(createResource(client, false)); Assert.assertTrue(createResource(client, true)); - Assert.assertEquals(1, deleteResource(client)); + Assert.assertTrue(deleteResource(client)); } @Test @@ -83,14 +83,17 @@ public class ResourceManagerTest { return false; } - private int deleteResource(HttpClient client) throws Exception { + private boolean deleteResource(HttpClient client) throws Exception { String input = WebTestUtils.readScriptTemplate(GQL_TEMPLATE_RM_DELETE_RESOURCE, CEServerTestSuite.getScriptsPath()); Map map = WebTestUtils.doPost(CEServerTestSuite.GQL_API_URL, input, client); Map data = JSONUtils.getObjectOrNull(map, "data"); - if (data != null) { - return JSONUtils.getInteger(data, "navDeleteNodes"); + if (map.containsKey("errors")) { + return false; } - return -1; + if (data != null) { + return data.containsKey("rmDeleteResource"); + } + return false; } } diff --git a/server/test/io.cloudbeaver.test.platform/workspace/gql_scripts/rmDeleteResource.json b/server/test/io.cloudbeaver.test.platform/workspace/gql_scripts/rmDeleteResource.json new file mode 100644 index 0000000000..f820b6222c --- /dev/null +++ b/server/test/io.cloudbeaver.test.platform/workspace/gql_scripts/rmDeleteResource.json @@ -0,0 +1,9 @@ +{ + "query": "\n mutation rmDeleteResource($projectId: String!, $resourcePath: String!, $recursive: Boolean!) {\n rmDeleteResource(\n projectId: $projectId\n resourcePath: $resourcePath\n recursive: $recursive\n )\n}\n ", + "variables": { + "projectId": "u_test", + "resourcePath": "testScript.sql", + "recursive": false + }, + "operationName": "rmDeleteResource" +} \ No newline at end of file