Skip to content

Commit b01a1ca

Browse files
author
Thomas Draier
committed
Fixed EnhancedExecutionStrategy
1 parent b6532ea commit b01a1ca

File tree

3 files changed

+237
-138
lines changed

3 files changed

+237
-138
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations;
16+
17+
import graphql.ExecutionResult;
18+
import graphql.execution.*;
19+
import graphql.language.*;
20+
import graphql.schema.GraphQLEnumType;
21+
import graphql.schema.GraphQLFieldDefinition;
22+
import graphql.schema.GraphQLObjectType;
23+
24+
import java.util.HashMap;
25+
import java.util.Optional;
26+
import java.util.concurrent.CompletableFuture;
27+
28+
public class EnhancedExecutionStrategy extends AsyncSerialExecutionStrategy {
29+
30+
private static final String CLIENT_MUTATION_ID = "clientMutationId";
31+
32+
@Override
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));
36+
if (fieldDef == null) return null;
37+
38+
if (fieldDef.getName().contentEquals(CLIENT_MUTATION_ID)) {
39+
Field field = (Field) executionContext.getOperationDefinition().getSelectionSet().getSelections().get(0);
40+
Argument argument = field.getArguments().get(0);
41+
42+
Object clientMutationId;
43+
if (argument.getValue() instanceof VariableReference) {
44+
VariableReference ref = (VariableReference) argument.getValue();
45+
HashMap mutationInputVariables = (HashMap) executionContext.getVariables().get(ref.getName());
46+
clientMutationId = mutationInputVariables.get(CLIENT_MUTATION_ID);
47+
} else {
48+
ObjectValue value = (ObjectValue) field.getArguments().get(0).getValue();
49+
StringValue clientMutationIdVal = (StringValue) value.getObjectFields().stream()
50+
.filter(f -> f.getName().contentEquals(CLIENT_MUTATION_ID))
51+
.findFirst().get().getValue();
52+
clientMutationId = clientMutationIdVal.getValue();
53+
}
54+
55+
ExecutionTypeInfo fieldTypeInfo = ExecutionTypeInfo.newTypeInfo().type(fieldDef.getType()).parentInfo(parameters.typeInfo()).build();
56+
ExecutionStrategyParameters newParameters = ExecutionStrategyParameters.newParameters()
57+
.arguments(parameters.arguments())
58+
.fields(parameters.fields())
59+
.nonNullFieldValidator(parameters.nonNullFieldValidator())
60+
.typeInfo(fieldTypeInfo)
61+
.source(clientMutationId)
62+
.build();
63+
64+
65+
return completeValue(executionContext, newParameters);
66+
} else {
67+
return super.resolveField(executionContext, parameters);
68+
}
69+
}
70+
71+
@Override
72+
protected CompletableFuture<ExecutionResult> completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
73+
graphql.schema.GraphQLType fieldType = parameters.typeInfo().getType();
74+
Object result = parameters.source();
75+
if (result instanceof Enum && fieldType instanceof GraphQLEnumType) {
76+
Object value = ((GraphQLEnumType) fieldType).getCoercing().parseValue(((Enum) result).name());
77+
return super.completeValue(executionContext, withSource(parameters, value));
78+
}
79+
if (result instanceof Optional) {
80+
Object value = ((Optional<?>) result).orElse(null);
81+
return completeValue(executionContext, withSource(parameters, value));
82+
}
83+
return super.completeValue(executionContext, parameters);
84+
}
85+
86+
/*
87+
Creates a new parameters with the specified object as its source
88+
*/
89+
private ExecutionStrategyParameters withSource(ExecutionStrategyParameters parameters, Object source) {
90+
return ExecutionStrategyParameters.newParameters()
91+
.arguments(parameters.arguments())
92+
.fields(parameters.fields())
93+
.nonNullFieldValidator(parameters.nonNullFieldValidator())
94+
.typeInfo(parameters.typeInfo())
95+
.source(source)
96+
.build();
97+
}
98+
99+
}

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

Lines changed: 1 addition & 1 deletion
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 {

src/test/java/graphql/annotations/RelayTest.java

Lines changed: 137 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -90,141 +90,141 @@ public void notAnObjectType() {
9090
GraphQLObjectType object = GraphQLAnnotations.object(WrongReturnType.class);
9191
}
9292

93-
// @Test
94-
// public void noArgMutation() {
95-
// GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
96-
//
97-
// GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomething");
98-
//
99-
// assertNotNull(doSomething);
100-
//
101-
// assertEquals(doSomething.getArguments().size(), 1);
102-
// GraphQLInputType input = doSomething.getArgument("input").getType();
103-
// assertTrue(input instanceof GraphQLNonNull);
104-
// GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
105-
// assertTrue(inputType instanceof GraphQLInputObjectType);
106-
//
107-
// assertTrue(doSomething.getType() instanceof GraphQLObjectType);
108-
// GraphQLObjectType returnType = (GraphQLObjectType) doSomething.getType();
109-
//
110-
// assertNotNull(returnType.getFieldDefinition("i"));
111-
// assertNotNull(returnType.getFieldDefinition("clientMutationId"));
112-
//
113-
// GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
114-
//
115-
// GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
116-
//
117-
// ExecutionResult result = graphQL.execute("mutation { doSomething(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
118-
//
119-
// assertEquals(result.getErrors().size(), 0);
120-
//
121-
// Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomething");
122-
//
123-
// assertEquals(returns.get("i"), 0);
124-
// assertEquals(returns.get("clientMutationId"), "1");
125-
// }
126-
127-
// @Test
128-
// public void interfaceReturningMutation() {
129-
// GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
130-
//
131-
// GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomethingI");
132-
//
133-
// assertNotNull(doSomething);
134-
//
135-
// GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
136-
//
137-
// GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
138-
//
139-
// ExecutionResult result = graphQL.execute("mutation { doSomethingI(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
140-
//
141-
// assertEquals(result.getErrors().size(), 0);
142-
//
143-
// Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingI");
144-
//
145-
// assertEquals(returns.get("i"), 0);
146-
// assertEquals(returns.get("clientMutationId"), "1");
147-
// }
148-
149-
150-
// @Test
151-
// public void argMutation() {
152-
// GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
153-
//
154-
// GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse");
155-
//
156-
// assertNotNull(doSomethingElse);
157-
//
158-
// assertEquals(doSomethingElse.getArguments().size(), 1);
159-
// GraphQLInputType input = doSomethingElse.getArgument("input").getType();
160-
// assertTrue(input instanceof GraphQLNonNull);
161-
// GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
162-
// assertTrue(inputType instanceof GraphQLInputObjectType);
163-
// GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType;
164-
// assertNotNull(inputType_.getField("a"));
165-
// assertNotNull(inputType_.getField("b"));
166-
// assertEquals(inputType_.getField("a").getDescription(), "A");
167-
//
168-
// assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType);
169-
// GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType();
170-
//
171-
// assertNotNull(returnType.getFieldDefinition("i"));
172-
// assertNotNull(returnType.getFieldDefinition("clientMutationId"));
173-
//
174-
// GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
175-
//
176-
// GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
177-
//
178-
// ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
179-
//
180-
// assertEquals(result.getErrors().size(), 0);
181-
//
182-
// Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse");
183-
//
184-
// assertEquals(returns.get("i"), -1);
185-
// assertEquals(returns.get("clientMutationId"), "1");
186-
// }
187-
188-
// @Test
189-
// public void argVariableMutation() {
190-
// GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
191-
//
192-
// GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse");
193-
//
194-
// assertNotNull(doSomethingElse);
195-
//
196-
// assertEquals(doSomethingElse.getArguments().size(), 1);
197-
// GraphQLInputType input = doSomethingElse.getArgument("input").getType();
198-
// assertTrue(input instanceof GraphQLNonNull);
199-
// GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
200-
// assertTrue(inputType instanceof GraphQLInputObjectType);
201-
// GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType;
202-
// assertNotNull(inputType_.getField("a"));
203-
// assertNotNull(inputType_.getField("b"));
204-
//
205-
// assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType);
206-
// GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType();
207-
//
208-
// assertNotNull(returnType.getFieldDefinition("i"));
209-
// assertNotNull(returnType.getFieldDefinition("clientMutationId"));
210-
//
211-
// GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
212-
//
213-
// GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
214-
//
215-
// Map<String, Object> variables = new HashMap<>();
216-
// Map<String, Object> inputVariables = new HashMap<>();
217-
// inputVariables.put("a", 0);
218-
// inputVariables.put("b", 1);
219-
// inputVariables.put("clientMutationId", "1");
220-
// variables.put("input", inputVariables);
221-
// ExecutionResult result = graphQL.execute("mutation VariableMutation($input:DoSomethingElseInput!) { doSomethingElse(input: $input) { i clientMutationId } }", new TestObject(), variables);
222-
//
223-
// assertEquals(result.getErrors().size(), 0);
224-
//
225-
// Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse");
226-
//
227-
// assertEquals(returns.get("i"), -1);
228-
// assertEquals(returns.get("clientMutationId"), "1");
229-
// }
93+
@Test
94+
public void noArgMutation() {
95+
GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
96+
97+
GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomething");
98+
99+
assertNotNull(doSomething);
100+
101+
assertEquals(doSomething.getArguments().size(), 1);
102+
GraphQLInputType input = doSomething.getArgument("input").getType();
103+
assertTrue(input instanceof GraphQLNonNull);
104+
GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
105+
assertTrue(inputType instanceof GraphQLInputObjectType);
106+
107+
assertTrue(doSomething.getType() instanceof GraphQLObjectType);
108+
GraphQLObjectType returnType = (GraphQLObjectType) doSomething.getType();
109+
110+
assertNotNull(returnType.getFieldDefinition("i"));
111+
assertNotNull(returnType.getFieldDefinition("clientMutationId"));
112+
113+
GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
114+
115+
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
116+
117+
ExecutionResult result = graphQL.execute("mutation { doSomething(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
118+
119+
assertEquals(result.getErrors().size(), 0);
120+
121+
Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomething");
122+
123+
assertEquals(returns.get("i"), 0);
124+
assertEquals(returns.get("clientMutationId"), "1");
125+
}
126+
127+
@Test
128+
public void interfaceReturningMutation() {
129+
GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
130+
131+
GraphQLFieldDefinition doSomething = object.getFieldDefinition("doSomethingI");
132+
133+
assertNotNull(doSomething);
134+
135+
GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
136+
137+
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
138+
139+
ExecutionResult result = graphQL.execute("mutation { doSomethingI(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
140+
141+
assertEquals(result.getErrors().size(), 0);
142+
143+
Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingI");
144+
145+
assertEquals(returns.get("i"), 0);
146+
assertEquals(returns.get("clientMutationId"), "1");
147+
}
148+
149+
150+
@Test
151+
public void argMutation() {
152+
GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
153+
154+
GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse");
155+
156+
assertNotNull(doSomethingElse);
157+
158+
assertEquals(doSomethingElse.getArguments().size(), 1);
159+
GraphQLInputType input = doSomethingElse.getArgument("input").getType();
160+
assertTrue(input instanceof GraphQLNonNull);
161+
GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
162+
assertTrue(inputType instanceof GraphQLInputObjectType);
163+
GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType;
164+
assertNotNull(inputType_.getField("a"));
165+
assertNotNull(inputType_.getField("b"));
166+
assertEquals(inputType_.getField("a").getDescription(), "A");
167+
168+
assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType);
169+
GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType();
170+
171+
assertNotNull(returnType.getFieldDefinition("i"));
172+
assertNotNull(returnType.getFieldDefinition("clientMutationId"));
173+
174+
GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
175+
176+
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
177+
178+
ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject());
179+
180+
assertEquals(result.getErrors().size(), 0);
181+
182+
Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse");
183+
184+
assertEquals(returns.get("i"), -1);
185+
assertEquals(returns.get("clientMutationId"), "1");
186+
}
187+
188+
@Test
189+
public void argVariableMutation() {
190+
GraphQLObjectType object = GraphQLAnnotations.object(TestObject.class);
191+
192+
GraphQLFieldDefinition doSomethingElse = object.getFieldDefinition("doSomethingElse");
193+
194+
assertNotNull(doSomethingElse);
195+
196+
assertEquals(doSomethingElse.getArguments().size(), 1);
197+
GraphQLInputType input = doSomethingElse.getArgument("input").getType();
198+
assertTrue(input instanceof GraphQLNonNull);
199+
GraphQLType inputType = ((graphql.schema.GraphQLNonNull) input).getWrappedType();
200+
assertTrue(inputType instanceof GraphQLInputObjectType);
201+
GraphQLInputObjectType inputType_ = (GraphQLInputObjectType) inputType;
202+
assertNotNull(inputType_.getField("a"));
203+
assertNotNull(inputType_.getField("b"));
204+
205+
assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType);
206+
GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType();
207+
208+
assertNotNull(returnType.getFieldDefinition("i"));
209+
assertNotNull(returnType.getFieldDefinition("clientMutationId"));
210+
211+
GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build();
212+
213+
GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build();
214+
215+
Map<String, Object> variables = new HashMap<>();
216+
Map<String, Object> inputVariables = new HashMap<>();
217+
inputVariables.put("a", 0);
218+
inputVariables.put("b", 1);
219+
inputVariables.put("clientMutationId", "1");
220+
variables.put("input", inputVariables);
221+
ExecutionResult result = graphQL.execute("mutation VariableMutation($input:DoSomethingElseInput!) { doSomethingElse(input: $input) { i clientMutationId } }", new TestObject(), variables);
222+
223+
assertEquals(result.getErrors().size(), 0);
224+
225+
Map<String, Object> returns = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("doSomethingElse");
226+
227+
assertEquals(returns.get("i"), -1);
228+
assertEquals(returns.get("clientMutationId"), "1");
229+
}
230230
}

0 commit comments

Comments
 (0)