forked from Enigmatis/graphql-java-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedExecutionStrategy.java
More file actions
78 lines (68 loc) · 3.53 KB
/
EnhancedExecutionStrategy.java
File metadata and controls
78 lines (68 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Copyright 2016 Yurii Rashkovskii
*
* 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
*
* 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.annotations.strategies;
import graphql.ExecutionResult;
import graphql.execution.*;
import graphql.language.*;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import java.util.HashMap;
import java.util.Optional;
public class EnhancedExecutionStrategy extends AsyncSerialExecutionStrategy {
private static final String CLIENT_MUTATION_ID = "clientMutationId";
@Override
protected FieldValueInfo completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
graphql.schema.GraphQLType fieldType = parameters.getExecutionStepInfo().getType();
if (parameters.getExecutionStepInfo().getFieldDefinition().getName().contentEquals(CLIENT_MUTATION_ID)) {
Field field = (Field) executionContext.getOperationDefinition().getSelectionSet().getSelections().get(0);
Argument argument = field.getArguments().get(0);
Object clientMutationId;
if (argument.getValue() instanceof VariableReference) {
VariableReference ref = (VariableReference) argument.getValue();
HashMap mutationInputVariables = (HashMap) executionContext.getCoercedVariables().get(ref.getName());
clientMutationId = mutationInputVariables.get(CLIENT_MUTATION_ID);
} else {
ObjectValue value = (ObjectValue) field.getArguments().get(0).getValue();
StringValue clientMutationIdVal = (StringValue) value.getObjectFields().stream()
.filter(f -> f.getName().contentEquals(CLIENT_MUTATION_ID))
.findFirst().get().getValue();
clientMutationId = clientMutationIdVal.getValue();
}
return super.completeValue(executionContext, withSource(parameters, clientMutationId));
}
Object result = parameters.getSource();
if (result instanceof Enum && fieldType instanceof GraphQLEnumType) {
Object value = ((GraphQLEnumType) fieldType).parseValue(((Enum) result).name());
return super.completeValue(executionContext, withSource(parameters, value));
}
if (result instanceof Optional) {
Object value = ((Optional<?>) result).orElse(null);
return completeValue(executionContext, withSource(parameters, value));
}
return super.completeValue(executionContext, parameters);
}
/*
Creates a new parameters with the specified object as its source
*/
private ExecutionStrategyParameters withSource(ExecutionStrategyParameters parameters, Object source) {
return ExecutionStrategyParameters.newParameters()
.fields(parameters.getFields())
.nonNullFieldValidator(parameters.getNonNullFieldValidator())
.executionStepInfo(parameters.getExecutionStepInfo())
.source(source)
.build();
}
}