Repository navigation
Expand file tree
/
Copy pathValuesResolver.java
More file actions
143 lines (116 loc) · 5.9 KB
/
Copy pathValuesResolver.java
File metadata and controls
143 lines (116 loc) · 5.9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package graphql.execution;
import graphql.GraphQLException;
import graphql.language.*;
import graphql.schema.*;
import java.io.ObjectInput;
import java.util.*;
public class ValuesResolver {
public Map<String, Object> getVariableValues(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> inputs) {
Map<String, Object> result = new LinkedHashMap<>();
for (VariableDefinition variableDefinition : variableDefinitions) {
result.put(variableDefinition.getName(), getVariableValue(schema, variableDefinition, inputs.get(variableDefinition.getName())));
}
return result;
}
public Map<String, Object> getArgumentValues(List<GraphQLFieldArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
Map<String, Object> result = new LinkedHashMap<>();
Map<String, Argument> argumentMap = argumentMap(arguments);
for (GraphQLFieldArgument fieldArgument : argumentTypes) {
Argument argument = argumentMap.get(fieldArgument.getName());
Object value = coerceValueAst(fieldArgument.getType(), argument.getValue(), variables);
result.put(argument.getName(), value);
}
return result;
}
private Map<String, Argument> argumentMap(List<Argument> arguments) {
Map<String, Argument> result = new LinkedHashMap<>();
for (Argument argument : arguments) {
result.put(argument.getName(), argument);
}
return result;
}
private Object getVariableValue(GraphQLSchema schema, VariableDefinition variableDefinition, Object inputValue) {
GraphQLType type = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
if (!isValid(type, inputValue)) {
throw new GraphQLException("Invalid value for type");
}
if (inputValue == null && variableDefinition.getDefaultValue() != null) {
return coerceValueAst(type, variableDefinition.getDefaultValue(), null);
}
return coerceValue(type, inputValue);
}
private boolean isValid(GraphQLType type, Object inputValue) {
return true;
}
private Object coerceValue(GraphQLType graphQLType, Object value) {
if (graphQLType instanceof GraphQLScalarType) {
return coerceValueForScalar((GraphQLScalarType) graphQLType, value);
} else if (graphQLType instanceof GraphQLEnumType) {
return coerceValueForEnum((GraphQLEnumType) graphQLType, value);
} else if (graphQLType instanceof GraphQLList) {
return coerceValueForList((GraphQLList) graphQLType, value);
} else if (graphQLType instanceof GraphQLInputObjectType) {
return coerceValueForInputObjectField((GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
} else if (graphQLType instanceof GraphQLNonNull) {
return coerceValue(((GraphQLNonNull) graphQLType).getWrappedType(), value);
}
return null;
}
private Object coerceValueForInputObjectField(GraphQLInputObjectType inputObjectType, Map<String, Object> input) {
Map<String, Object> result = new LinkedHashMap<>();
for (GraphQLInputObjectField inputField : inputObjectType.getFields()) {
Object value = coerceValue(inputField.getType(), input.get(inputField.getName()));
result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value);
}
return result;
}
private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object value) {
return graphQLScalarType.getCoercing().coerce(value);
}
private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value) {
return graphQLEnumType.getCoercing().coerce(value);
}
private List coerceValueForList(GraphQLList graphQLList, Object value) {
if (value instanceof Iterable) {
List<Object> result = new ArrayList<>();
for (Object val : (Iterable) value) {
result.add(coerceValue(graphQLList.getWrappedType(), val));
}
return result;
} else {
return Collections.singletonList(coerceValue(graphQLList.getWrappedType(), value));
}
}
private Object coerceValueAst(GraphQLType type, Value inputValue, Map<String, Object> variables) {
if (inputValue instanceof VariableReference) {
return variables.get(((VariableReference) inputValue).getName());
}
if (type instanceof GraphQLScalarType) {
return ((GraphQLScalarType) type).getCoercing().coerceLiteral(inputValue);
}
if (type instanceof GraphQLNonNull) {
return coerceValueAst(((GraphQLNonNull) type).getWrappedType(), inputValue, variables);
}
if (type instanceof GraphQLInputObjectType) {
return coerceValueAstForInputObject((GraphQLInputObjectType) type, (ObjectValue) inputValue, variables);
}
if(type instanceof GraphQLEnumType){
return ((GraphQLEnumType) type).getCoercing().coerceLiteral(inputValue);
}
return null;
}
private Object coerceValueAstForInputObject(GraphQLInputObjectType type, ObjectValue inputValue, Map<String, Object> variables) {
Map<String, Object> result = new LinkedHashMap<>();
for (ObjectField objectField : inputValue.getObjectFields()) {
GraphQLInputObjectField inputObjectField = type.getField(objectField.getName());
// illegal field ... no corresponding key in the schema
if (inputObjectField == null) continue;
Object fieldValue = coerceValueAst(inputObjectField.getType(), objectField.getValue(), variables);
if (fieldValue == null) {
fieldValue = inputObjectField.getDefaultValue();
}
result.put(objectField.getName(), fieldValue);
}
return result;
}
}