Skip to content

Commit

Permalink
graphql-java 4.1 compatibility. one failing test, no idea why.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Oudmaijer committed Sep 5, 2017
1 parent eb08e16 commit 767e318
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 204 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ dependencies {
compile 'commons-fileupload:commons-fileupload:1.3.1'

// GraphQL
compile 'com.graphql-java:graphql-java:3.0.0'
compile 'com.graphql-java:graphql-java:4.1'

compileOnly 'com.graphql-java:graphql-java-annotations:0.13.1'
testCompile 'com.graphql-java:graphql-java-annotations:0.13.1'
compileOnly 'com.graphql-java:graphql-java-annotations:3.0.2'
testCompile 'com.graphql-java:graphql-java-annotations:3.0.2'

// JSON
compile 'com.fasterxml.jackson.core:jackson-core:2.8.4'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = 4.2.2
version = 4.3.0
group = com.graphql-java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.servlet;

import graphql.execution.AsyncExecutionStrategy;
import graphql.execution.ExecutionStrategy;
import graphql.execution.SimpleExecutionStrategy;

/**
* @author Andrew Potter
Expand All @@ -41,7 +41,7 @@ public DefaultExecutionStrategyProvider(ExecutionStrategy queryExecutionStrategy
}

private ExecutionStrategy defaultIfNull(ExecutionStrategy executionStrategy) {
return defaultIfNull(executionStrategy, new SimpleExecutionStrategy());
return defaultIfNull(executionStrategy, new AsyncExecutionStrategy());
}

private ExecutionStrategy defaultIfNull(ExecutionStrategy executionStrategy, ExecutionStrategy defaultStrategy) {
Expand Down
63 changes: 33 additions & 30 deletions src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -16,21 +16,19 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQLError;
import graphql.execution.TypeInfo;
import graphql.execution.ExecutionTypeInfo;
import graphql.execution.instrumentation.Instrumentation;
import graphql.introspection.IntrospectionQuery;
import graphql.schema.GraphQLFieldDefinition;
Expand Down Expand Up @@ -75,22 +73,27 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
public static final int STATUS_OK = 200;
public static final int STATUS_BAD_REQUEST = 400;

private static final ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).registerModule(new SimpleModule().addSerializer(TypeInfo.class, new JsonSerializer<TypeInfo>() {
private static final ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).registerModule(new SimpleModule().addSerializer(ExecutionTypeInfo.class, new JsonSerializer<ExecutionTypeInfo>() {
@Override
public void serialize(TypeInfo value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
public void serialize(ExecutionTypeInfo value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject();
serializers.defaultSerializeField("type", value.type(), gen);
serializers.defaultSerializeField("parentTypeInfo", value.parentTypeInfo(), gen);
serializers.defaultSerializeField("typeIsNonNull", value.typeIsNonNull(), gen);
serializers.defaultSerializeField("type", value.getType(), gen);
serializers.defaultSerializeField("parentTypeInfo", value.getParentTypeInfo(), gen);
serializers.defaultSerializeField("typeIsNonNull", value.isNonNullType(), gen);
gen.writeEndObject();
}
}));

protected abstract GraphQLSchemaProvider getSchemaProvider();

protected abstract GraphQLContext createContext(Optional<HttpServletRequest> request, Optional<HttpServletResponse> response);

protected abstract ExecutionStrategyProvider getExecutionStrategyProvider();

protected abstract Instrumentation getInstrumentation();

protected abstract Map<String, Object> transformVariables(GraphQLSchema schema, String query, Map<String, Object> variables);

protected abstract GraphQLErrorHandler getGraphQLErrorHandler();

private final List<GraphQLServletListener> listeners;
Expand Down Expand Up @@ -193,7 +196,7 @@ public GraphQLServlet(List<GraphQLServletListener> listeners, FileItemFactory fi
return;
}

Map<String,Object> variables = graphQLRequest.getVariables();
Map<String, Object> variables = graphQLRequest.getVariables();
if (variables == null) {
variables = new HashMap<>();
}
Expand Down Expand Up @@ -258,7 +261,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

private Optional<FileItem> getFileItem(Map<String, List<FileItem>> fileItems, String name) {
List<FileItem> items = fileItems.get(name);
if(items == null || items.isEmpty()) {
if (items == null || items.isEmpty()) {
return Optional.empty();
}

Expand All @@ -268,11 +271,11 @@ private Optional<FileItem> getFileItem(Map<String, List<FileItem>> fileItems, St
private GraphQL newGraphQL(GraphQLSchema schema) {
ExecutionStrategyProvider executionStrategyProvider = getExecutionStrategyProvider();
return GraphQL.newGraphQL(schema)
.queryExecutionStrategy(executionStrategyProvider.getQueryExecutionStrategy())
.mutationExecutionStrategy(executionStrategyProvider.getMutationExecutionStrategy())
.subscriptionExecutionStrategy(executionStrategyProvider.getSubscriptionExecutionStrategy())
.instrumentation(getInstrumentation())
.build();
.queryExecutionStrategy(executionStrategyProvider.getQueryExecutionStrategy())
.mutationExecutionStrategy(executionStrategyProvider.getMutationExecutionStrategy())
.subscriptionExecutionStrategy(executionStrategyProvider.getSubscriptionExecutionStrategy())
.instrumentation(getInstrumentation())
.build();
}

private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
Expand Down Expand Up @@ -300,7 +303,7 @@ private void query(String query, String operationName, Map<String, Object> varia
resp.setStatus(STATUS_OK);
resp.getWriter().write(response);

if(getGraphQLErrorHandler().errorsPresent(errors)) {
if (getGraphQLErrorHandler().errorsPresent(errors)) {
runCallbacks(operationCallbacks, c -> c.onError(context, operationName, query, variables, data, errors));
} else {
runCallbacks(operationCallbacks, c -> c.onSuccess(context, operationName, query, variables, data));
Expand Down Expand Up @@ -328,16 +331,16 @@ private <R> List<R> runListeners(Function<? super GraphQLServletListener, R> act
}

return listeners.stream()
.map(listener -> {
try {
return action.apply(listener);
} catch (Throwable t) {
log.error("Error running listener: {}", listener, t);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
.map(listener -> {
try {
return action.apply(listener);
} catch (Throwable t) {
log.error("Error running listener: {}", listener, t);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private <T> void runCallbacks(List<T> callbacks, Consumer<T> action) {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/graphql/servlet/OsgiGraphQLServlet.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -36,7 +36,7 @@
import static graphql.schema.GraphQLSchema.newSchema;

@Component(
service={javax.servlet.http.HttpServlet.class,javax.servlet.Servlet.class},
service = {javax.servlet.http.HttpServlet.class, javax.servlet.Servlet.class},
property = {"alias=/graphql", "jmx.objectname=graphql.servlet:type=graphql"}
)
public class OsgiGraphQLServlet extends GraphQLServlet {
Expand All @@ -46,7 +46,7 @@ public class OsgiGraphQLServlet extends GraphQLServlet {
private final List<GraphQLTypesProvider> typesProviders = new ArrayList<>();

private GraphQLContextBuilder contextBuilder = new DefaultGraphQLContextBuilder();
private ExecutionStrategyProvider executionStrategyProvider = new EnhancedExecutionStrategyProvider();
private ExecutionStrategyProvider executionStrategyProvider = new DefaultExecutionStrategyProvider();
private InstrumentationProvider instrumentationProvider = new NoOpInstrumentationProvider();
private GraphQLErrorHandler errorHandler = new DefaultGraphQLErrorHandler();

Expand Down Expand Up @@ -100,6 +100,7 @@ public void bindProvider(GraphQLProvider provider) {
}
updateSchema();
}

public void unbindProvider(GraphQLProvider provider) {
if (provider instanceof GraphQLQueryProvider) {
queryProviders.remove(provider);
Expand All @@ -118,6 +119,7 @@ public void bindQueryProvider(GraphQLQueryProvider queryProvider) {
queryProviders.add(queryProvider);
updateSchema();
}

public void unbindQueryProvider(GraphQLQueryProvider queryProvider) {
queryProviders.remove(queryProvider);
updateSchema();
Expand All @@ -128,6 +130,7 @@ public void bindMutationProvider(GraphQLMutationProvider mutationProvider) {
mutationProviders.add(mutationProvider);
updateSchema();
}

public void unbindMutationProvider(GraphQLMutationProvider mutationProvider) {
mutationProviders.remove(mutationProvider);
updateSchema();
Expand All @@ -138,6 +141,7 @@ public void typesProviders(GraphQLTypesProvider typesProvider) {
typesProviders.add(typesProvider);
updateSchema();
}

public void unbindTypesProvider(GraphQLTypesProvider typesProvider) {
typesProviders.remove(typesProvider);
updateSchema();
Expand All @@ -147,6 +151,7 @@ public void unbindTypesProvider(GraphQLTypesProvider typesProvider) {
public void bindServletListener(GraphQLServletListener listener) {
this.addListener(listener);
}

public void unbindServletListener(GraphQLServletListener listener) {
this.removeListener(listener);
}
Expand All @@ -155,6 +160,7 @@ public void unbindServletListener(GraphQLServletListener listener) {
public void setContextProvider(GraphQLContextBuilder contextBuilder) {
this.contextBuilder = contextBuilder;
}

public void unsetContextProvider(GraphQLContextBuilder contextBuilder) {
this.contextBuilder = new DefaultGraphQLContextBuilder();
}
Expand All @@ -163,14 +169,16 @@ public void unsetContextProvider(GraphQLContextBuilder contextBuilder) {
public void setExecutionStrategyProvider(ExecutionStrategyProvider provider) {
executionStrategyProvider = provider;
}

public void unsetExecutionStrategyProvider(ExecutionStrategyProvider provider) {
executionStrategyProvider = new EnhancedExecutionStrategyProvider();
executionStrategyProvider = new DefaultExecutionStrategyProvider();
}

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
public void setInstrumentationProvider(InstrumentationProvider provider) {
instrumentationProvider = provider;
}

public void unsetInstrumentationProvider(InstrumentationProvider provider) {
instrumentationProvider = new NoOpInstrumentationProvider();
}
Expand All @@ -179,6 +187,7 @@ public void unsetInstrumentationProvider(InstrumentationProvider provider) {
public void setErrorHandler(GraphQLErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}

public void unsetErrorHandler(GraphQLErrorHandler errorHandler) {
this.errorHandler = new DefaultGraphQLErrorHandler();
}
Expand Down
Loading

0 comments on commit 767e318

Please sign in to comment.