Skip to content
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

CB-5196 Update GraphQL usage #2671

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,77 @@ public class GraphQLConstants {

public static final String CONTENT_TYPE_JSON_UTF8 = "application/json;charset=UTF-8";

public static final String SCHEMA_READ_QUERY = " __schema {\n" +
" queryType { name }\n" +
" mutationType { name }\n" +
" subscriptionType { name }\n" +
" types {\n" +
" ...FullType\n" +
" }\n" +
" directives {\n" +
" name\n" +
" description\n" +
" locations\n" +
" args {\n" +
" ...InputValue\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" fragment FullType on __Type {\n" +
" kind\n" +
" name\n" +
" description\n" +
" fields(includeDeprecated: true) {\n" +
" name\n" +
" description\n" +
" args {\n" +
" ...InputValue\n" +
" }\n" +
" type {\n" +
" ...TypeRef\n" +
" }\n" +
" isDeprecated\n" +
" deprecationReason\n" +
" }\n" +
" inputFields {\n" +
" ...InputValue\n" +
" }\n" +
" interfaces {\n" +
" ...TypeRef\n" +
" }\n" +
" enumValues(includeDeprecated: true) {\n" +
" name\n" +
" description\n" +
" isDeprecated\n" +
" deprecationReason\n" +
" }\n" +
" possibleTypes {\n" +
" ...TypeRef\n" +
" }\n" +
" }\n" +
" fragment InputValue on __InputValue {\n" +
" name\n" +
" description\n" +
" type { ...TypeRef }\n" +
" defaultValue\n" +
" }\n" +
" fragment TypeRef on __Type {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" ofType {\n" +
" kind\n" +
" name\n" +
" }\n" +
" }\n" +
" }";
public static final String SCHEMA_READ_QUERY = """
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}\
""";

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import io.cloudbeaver.registry.WebServiceRegistry;
import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.HttpConstants;
import io.cloudbeaver.service.DBWBindingContext;
import io.cloudbeaver.service.DBWServiceBindingGraphQL;
import io.cloudbeaver.service.WebServiceBindingBase;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.data.json.JSONUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

Expand All @@ -64,11 +66,9 @@

private static final String CORE_SCHEMA_FILE_NAME = "schema/schema.graphqls";

private static final String SESSION_TEMP_COOKIE = "cb-session";

private final GraphQL graphQL;

private static Gson gson = new GsonBuilder()
private static final Gson gson = new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
.create();
Expand Down Expand Up @@ -117,7 +117,7 @@
}

@Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doOptions(HttpServletRequest request, HttpServletResponse response) {
setDevelHeaders(request, response);
}

Expand Down Expand Up @@ -164,12 +164,11 @@
}
String postBody = IOUtils.readToString(request.getReader());
JsonElement json = gson.fromJson(postBody, JsonElement.class);
if (json instanceof JsonArray) {
if (json instanceof JsonArray array) {
setDevelHeaders(request, response);
response.setContentType(GraphQLConstants.CONTENT_TYPE_JSON_UTF8);
response.getWriter().print("[\n");

JsonArray array = (JsonArray)json;
int reqCount = 0;
for (int i = 0; i < array.size(); i++) {
if (reqCount > 0) {
Expand All @@ -183,8 +182,7 @@
}

response.getWriter().print("\n]");
} else if (json instanceof JsonObject) {
JsonObject reqObject = (JsonObject) json;
} else if (json instanceof JsonObject reqObject) {
executeSingleQuery(request, response, reqObject);
} else {
response.sendError(400, "Bad JSON request");
Expand All @@ -198,15 +196,15 @@
return;
}
JsonElement varJSON = reqObject.get("variables");
Map<String, Object> variables = varJSON == null ? null : gson.fromJson(varJSON, Map.class);
Map<String, Object> variables = varJSON == null ? null : gson.fromJson(varJSON, JSONUtils.MAP_TYPE_TOKEN);

JsonElement operNameJSON = reqObject.get("operationName");

executeQuery(request, response, query.getAsString(), variables, operNameJSON == null || operNameJSON instanceof JsonNull ? null : operNameJSON.getAsString());
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = request.getPathInfo();
if (path == null) {
path = request.getServletPath();
Expand All @@ -230,13 +228,13 @@
}

private void executeQuery(HttpServletRequest request, HttpServletResponse response, String query, Map<String, Object> variables, String operationName) throws IOException {
GraphQLContext context = new GraphQLContext.Builder()
.of("request", request)
.of("response", response)
.of("bindingContext", bindingContext)
.build();
Map<String, Object> mapOfContext =
Map.of(
"request", request,
"response", response,
"bindingContext", bindingContext);
ExecutionInput.Builder contextBuilder = ExecutionInput.newExecutionInput()
.context(context)
.graphQLContext(mapOfContext)
.query(query);
if (variables != null) {
contextBuilder.variables(variables);
Expand Down Expand Up @@ -265,14 +263,14 @@
response.getWriter().print(resString);
}

private class WebExecutionStrategy extends AsyncExecutionStrategy {
private static class WebExecutionStrategy extends AsyncExecutionStrategy {

public WebExecutionStrategy() {
super(new WebDataFetcherExceptionHandler());
}
}

private class WebDataFetcherExceptionHandler implements DataFetcherExceptionHandler {
private static class WebDataFetcherExceptionHandler implements DataFetcherExceptionHandler {
@Override
public CompletableFuture<DataFetcherExceptionHandlerResult> handleException(DataFetcherExceptionHandlerParameters handlerParameters) {
Throwable exception = handlerParameters.getException();
Expand Down Expand Up @@ -308,7 +306,7 @@


public static HttpServletRequest getServletRequest(DataFetchingEnvironment env) {
GraphQLContext context = env.getContext();
GraphQLContext context = env.getGraphQlContext();
HttpServletRequest request = context.get("request");
if (request == null) {
throw new IllegalStateException("Null request");
Expand All @@ -317,16 +315,16 @@
}

public static HttpServletResponse getServletResponse(DataFetchingEnvironment env) {
GraphQLContext context = env.getContext();
GraphQLContext context = env.getGraphQlContext();
HttpServletResponse response = context.get("response");
if (response == null) {
throw new IllegalStateException("Null response");
}
return response;
}

public static GraphQLBindingContext getBindingContext(DataFetchingEnvironment env) {
GraphQLContext context = env.getContext();
public static DBWBindingContext getBindingContext(DataFetchingEnvironment env) {

Check warning on line 326 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java#L326

Missing a Javadoc comment.
GraphQLContext context = env.getGraphQlContext();
return context.get("bindingContext");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@
public class CBJettyServer {

private static final Log log = Log.getLog(CBJettyServer.class);
private static final String SESSION_CACHE_DIR = ".http-sessions";

static {
// Set Jetty log level to WARN
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "WARN");
}

private final CBApplication application;
private final CBApplication<?> application;

public CBJettyServer(@NotNull CBApplication application) {
public CBJettyServer(@NotNull CBApplication<?> application) {
this.application = application;
}

Expand Down Expand Up @@ -182,7 +180,7 @@ private Path getSslConfigurationPath() {
}

private void initSessionManager(
@NotNull CBApplication application,
@NotNull CBApplication<?> application,
@NotNull ServletContextHandler servletContextHandler
) {
// Init sessions persistence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import jakarta.servlet.http.HttpServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jkiss.dbeaver.DBException;

public class CBJettyServletContext implements DBWServletContext {
private final ServletContextHandler contextHandler;
Expand All @@ -31,7 +30,7 @@ public CBJettyServletContext(ServletContextHandler contextHandler) {
}

@Override
public void addServlet(String servletId, HttpServlet servlet, String mapping) throws DBException {
public void addServlet(String servletId, HttpServlet servlet, String mapping) {
contextHandler.addServlet(new ServletHolder(servletId, servlet), mapping);
}
}
Loading
Loading