Skip to content

Commit 503f71e

Browse files
authored
Merge pull request #105 from Jahia/graphql-java-v42
Update to graphql-java 4.2
2 parents e1ceb93 + b01a1ca commit 503f71e

File tree

9 files changed

+55
-77
lines changed

9 files changed

+55
-77
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ gradle.projectsEvaluated {
6868

6969
dependencies {
7070
compile 'javax.validation:validation-api:1.1.0.Final'
71-
compile 'com.graphql-java:graphql-java:3.0.0'
71+
compile 'com.graphql-java:graphql-java:4.2'
7272

7373
// OSGi
7474
compileOnly 'org.osgi:org.osgi.core:6.0.0'

src/main/java/graphql/annotations/EnhancedExecutionStrategy.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,24 @@
1515
package graphql.annotations;
1616

1717
import graphql.ExecutionResult;
18-
import graphql.execution.ExecutionContext;
19-
import graphql.execution.ExecutionParameters;
20-
import graphql.execution.SimpleExecutionStrategy;
21-
import graphql.execution.TypeInfo;
22-
import graphql.language.Argument;
23-
import graphql.language.Field;
24-
import graphql.language.ObjectValue;
25-
import graphql.language.StringValue;
26-
import graphql.language.VariableReference;
18+
import graphql.execution.*;
19+
import graphql.language.*;
2720
import graphql.schema.GraphQLEnumType;
2821
import graphql.schema.GraphQLFieldDefinition;
2922
import graphql.schema.GraphQLObjectType;
30-
import graphql.schema.GraphQLType;
3123

3224
import java.util.HashMap;
33-
import java.util.List;
3425
import java.util.Optional;
26+
import java.util.concurrent.CompletableFuture;
3527

36-
import static graphql.execution.ExecutionParameters.newParameters;
37-
import static graphql.execution.TypeInfoWorkaround.newTypeInfo;
38-
39-
public class EnhancedExecutionStrategy extends SimpleExecutionStrategy {
28+
public class EnhancedExecutionStrategy extends AsyncSerialExecutionStrategy {
4029

4130
private static final String CLIENT_MUTATION_ID = "clientMutationId";
4231

4332
@Override
44-
protected ExecutionResult resolveField(ExecutionContext executionContext, ExecutionParameters parameters, List<Field> fields) {
45-
GraphQLObjectType parentType = (GraphQLObjectType) parameters.typeInfo().type();
46-
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));
33+
protected CompletableFuture<ExecutionResult> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters) {
34+
GraphQLObjectType parentType = (GraphQLObjectType) parameters.typeInfo().getType();
35+
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, parameters.field().get(0));
4736
if (fieldDef == null) return null;
4837

4938
if (fieldDef.getName().contentEquals(CLIENT_MUTATION_ID)) {
@@ -63,45 +52,48 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
6352
clientMutationId = clientMutationIdVal.getValue();
6453
}
6554

66-
TypeInfo fieldTypeInfo = newTypeInfo(fieldDef.getType(), parameters.typeInfo());
67-
ExecutionParameters newParameters = newParameters()
55+
ExecutionTypeInfo fieldTypeInfo = ExecutionTypeInfo.newTypeInfo().type(fieldDef.getType()).parentInfo(parameters.typeInfo()).build();
56+
ExecutionStrategyParameters newParameters = ExecutionStrategyParameters.newParameters()
6857
.arguments(parameters.arguments())
6958
.fields(parameters.fields())
59+
.nonNullFieldValidator(parameters.nonNullFieldValidator())
7060
.typeInfo(fieldTypeInfo)
7161
.source(clientMutationId)
7262
.build();
7363

7464

75-
return completeValue(executionContext, newParameters, fields);
65+
return completeValue(executionContext, newParameters);
7666
} else {
77-
return super.resolveField(executionContext, parameters, fields);
67+
return super.resolveField(executionContext, parameters);
7868
}
7969
}
8070

8171
@Override
82-
protected ExecutionResult completeValue(ExecutionContext executionContext, ExecutionParameters parameters, List<Field> fields) {
83-
GraphQLType fieldType = parameters.typeInfo().type();
72+
protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
73+
graphql.schema.GraphQLType fieldType = parameters.typeInfo().getType();
8474
Object result = parameters.source();
8575
if (result instanceof Enum && fieldType instanceof GraphQLEnumType) {
8676
Object value = ((GraphQLEnumType) fieldType).getCoercing().parseValue(((Enum) result).name());
87-
return super.completeValue(executionContext, withSource(parameters, value), fields);
77+
return super.completeValue(executionContext, withSource(parameters, value));
8878
}
8979
if (result instanceof Optional) {
9080
Object value = ((Optional<?>) result).orElse(null);
91-
return completeValue(executionContext, withSource(parameters, value), fields);
81+
return completeValue(executionContext, withSource(parameters, value));
9282
}
93-
return super.completeValue(executionContext, parameters, fields);
83+
return super.completeValue(executionContext, parameters);
9484
}
9585

9686
/*
9787
Creates a new parameters with the specified object as its source
9888
*/
99-
private ExecutionParameters withSource(ExecutionParameters parameters, Object source) {
100-
return newParameters()
89+
private ExecutionStrategyParameters withSource(ExecutionStrategyParameters parameters, Object source) {
90+
return ExecutionStrategyParameters.newParameters()
10191
.arguments(parameters.arguments())
10292
.fields(parameters.fields())
93+
.nonNullFieldValidator(parameters.nonNullFieldValidator())
10394
.typeInfo(parameters.typeInfo())
10495
.source(source)
10596
.build();
10697
}
98+
10799
}

src/main/java/graphql/annotations/ExtensionDataFetcherWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public ExtensionDataFetcherWrapper(Class declaringClass, DataFetcher<T> dataFetc
3737
public T get(DataFetchingEnvironment environment) {
3838
Object source = environment.getSource();
3939
if (source != null && (!declaringClass.isInstance(source)) && !(source instanceof Map)) {
40-
environment = new DataFetchingEnvironmentImpl(newInstance(declaringClass, source), environment.getArguments(),
41-
environment.getContext(), environment.getFields(), environment.getFieldType(), environment.getParentType(),
42-
environment.getGraphQLSchema(), environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet());
40+
environment = new DataFetchingEnvironmentImpl(newInstance(declaringClass, source), environment.getArguments(), environment.getContext(),
41+
environment.getRoot(), environment.getFieldDefinition(), environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(),
42+
environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet(), environment.getFieldTypeInfo());
4343
}
4444

4545
return dataFetcher.get(environment);

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
659659
relayFieldDefinition = relay.mutationWithClientMutationId(title, method.getName(),
660660
args.stream().
661661
map(t -> newInputObjectField().name(t.getName()).type(t.getType()).description(t.getDescription()).build()).
662-
collect(Collectors.toList()), fieldDefinitions, null);
662+
collect(Collectors.toList()), fieldDefinitions, new StaticDataFetcher(null));
663663
builder.argument(relayFieldDefinition.getArguments());
664664
builder.type(relayFieldDefinition.getType());
665665
} else {
@@ -843,8 +843,8 @@ public Object get(DataFetchingEnvironment environment) {
843843
HashMap<String, Object> arguments = new HashMap<>(environment.getArguments());
844844
arguments.keySet().removeAll(Arrays.asList("first", "last", "before", "after"));
845845
DataFetchingEnvironment env = new DataFetchingEnvironmentImpl(environment.getSource(), arguments, environment.getContext(),
846-
environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(),
847-
environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet());
846+
environment.getRoot(), environment.getFieldDefinition(), environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(),
847+
environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet(), environment.getFieldTypeInfo());
848848
Object data = actualDataFetcher.get(env);
849849
if (data != null) {
850850
Connection conn = constructNewInstance(constructor, data);

src/main/java/graphql/annotations/MethodDataFetcher.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import graphql.schema.GraphQLType;
1919

2020
import java.lang.reflect.*;
21+
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.InvocationTargetException;
23+
import java.lang.reflect.Method;
24+
import java.lang.reflect.Modifier;
25+
import java.lang.reflect.Parameter;
2126
import java.util.*;
2227

2328
import static graphql.annotations.ReflectionKit.constructNewInstance;
@@ -63,17 +68,28 @@ public Object get(DataFetchingEnvironment environment) {
6368

6469
private Object[] invocationArgs(DataFetchingEnvironment environment) {
6570
List<Object> result = new ArrayList<>();
66-
Iterator envArgs = environment.getArguments().values().iterator();
71+
Map<String,Object> envArgs = environment.getArguments();
6772
for (Parameter p : method.getParameters()) {
73+
String parameterName;
74+
GraphQLName name = p.getAnnotation(GraphQLName.class);
75+
if (name != null) {
76+
parameterName = toGraphqlName(name.value());
77+
} else {
78+
parameterName = toGraphqlName(p.getName());
79+
}
80+
6881
Class<?> paramType = p.getType();
6982
if (DataFetchingEnvironment.class.isAssignableFrom(paramType)) {
7083
result.add(environment);
7184
continue;
7285
}
7386

7487
graphql.schema.GraphQLType graphQLType = typeFunction.buildType(true, paramType, p.getAnnotatedType());
75-
Object arg = envArgs.next();
76-
result.add(buildArg(p.getParameterizedType(), graphQLType, arg));
88+
if (envArgs.containsKey(parameterName)) {
89+
result.add(buildArg(p.getParameterizedType(), graphQLType, envArgs.get(parameterName)));
90+
} else {
91+
result.add(null);
92+
}
7793
}
7894
return result.toArray();
7995
}

src/main/java/graphql/execution/TypeInfoWorkaround.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/test/java/graphql/annotations/GraphQLConnectionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public List<Obj> getNonNullObjs() {
112112
}
113113

114114
@GraphQLField
115-
@GraphQLConnection(name = "null")
116-
public List<Obj> getNull() {
115+
@GraphQLConnection(name = "nullObj")
116+
public List<Obj> getNullObj() {
117117
return null;
118118
}
119119

@@ -197,14 +197,14 @@ public void methodNull() {
197197
GraphQLSchema schema = newSchema().query(object).build();
198198

199199
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
200-
ExecutionResult result = graphQL.execute("{ null(first: 1) { edges { cursor node { id, val } } } }",
200+
ExecutionResult result = graphQL.execute("{ nullObj(first: 1) { edges { cursor node { id, val } } } }",
201201
new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"))));
202202

203203
assertTrue(result.getErrors().isEmpty());
204204

205205
Map<String, Map<String, List<Map<String, Map<String, Object>>>>> data = result.getData();
206206

207-
assertEquals(data.get("null"), null);
207+
assertEquals(data.get("nullObj"), null);
208208
}
209209

210210
@Test

src/test/java/graphql/annotations/GraphQLObjectTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ public void queryOptional() {
657657
GraphQLObjectType object = GraphQLAnnotations.object(OptionalTest.class);
658658
GraphQLSchema schema = newSchema().query(object).build();
659659

660-
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
660+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
661661
ExecutionResult result = graphQL.execute("{empty, nonempty}", new OptionalTest());
662662
assertTrue(result.getErrors().isEmpty());
663663
Map<String, Object> v = (Map<String, Object>) result.getData();
@@ -676,7 +676,7 @@ public void optionalInput() {
676676
}).build()).build();
677677
GraphQLSchema schema = newSchema().query(object).mutation(mutation).build();
678678

679-
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
679+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
680680
ExecutionResult result = graphQL.execute("mutation {test(input: {empty: \"test\"}) { empty nonempty } }", new OptionalTest());
681681
assertTrue(result.getErrors().isEmpty());
682682
Map<String, Object> v = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("test");
@@ -707,7 +707,7 @@ public String toString() {
707707
public void queryEnum() {
708708
GraphQLObjectType object = GraphQLAnnotations.object(EnumTest.class);
709709
GraphQLSchema schema = newSchema().query(object).build();
710-
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
710+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
711711

712712
ExecutionResult result = graphQL.execute("{e}", new EnumTest(EnumTest.E.B));
713713
assertTrue(result.getErrors().isEmpty());

src/test/java/graphql/annotations/MethodDataFetcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public String method() throws TestException {
4040
public void exceptionRethrowing() {
4141
try {
4242
MethodDataFetcher methodDataFetcher = new MethodDataFetcher(getClass().getMethod("method"));
43-
methodDataFetcher.get(new DataFetchingEnvironmentImpl(this, new HashMap<>(), null, new ArrayList<>(), null, null, null, null, null, null));
43+
methodDataFetcher.get(new DataFetchingEnvironmentImpl(this, new HashMap<String,Object>(), null, null, null, new ArrayList<>(), null, null, null, null, null, null, null));
4444
} catch (NoSuchMethodException e) {
4545
e.printStackTrace();
4646
}

0 commit comments

Comments
 (0)