-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DirectivesResolver.java
57 lines (50 loc) · 2.57 KB
/
DirectivesResolver.java
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
package graphql.execution.directives;
import com.google.common.collect.ImmutableMap;
import graphql.Internal;
import graphql.execution.CoercedVariables;
import graphql.execution.ValuesResolver;
import graphql.language.Directive;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLSchema;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This turns AST directives into runtime directives with resolved types and so on
*/
@Internal
public class DirectivesResolver {
private final ValuesResolver valuesResolver = new ValuesResolver();
public DirectivesResolver() {
}
public Map<String, GraphQLDirective> resolveDirectives(List<Directive> directives, GraphQLSchema schema, Map<String, Object> variables) {
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry();
Map<String, GraphQLDirective> directiveMap = new LinkedHashMap<>();
directives.forEach(directive -> {
GraphQLDirective protoType = schema.getDirective(directive.getName());
if (protoType != null) {
GraphQLDirective newDirective = protoType.transform(builder -> buildArguments(builder, codeRegistry, protoType, directive, variables));
directiveMap.put(newDirective.getName(), newDirective);
}
});
return ImmutableMap.copyOf(directiveMap);
}
private void buildArguments(GraphQLDirective.Builder directiveBuilder, GraphQLCodeRegistry codeRegistry, GraphQLDirective protoType, Directive fieldDirective, Map<String, Object> variables) {
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), CoercedVariables.of(variables));
directiveBuilder.clearArguments();
protoType.getArguments().forEach(protoArg -> {
if (argumentValues.containsKey(protoArg.getName())) {
Object argValue = argumentValues.get(protoArg.getName());
GraphQLArgument newArgument = protoArg.transform(argBuilder -> argBuilder.value(argValue));
directiveBuilder.argument(newArgument);
} else {
// this means they can ask for the argument default value because the argument on the directive
// object is present - but null
GraphQLArgument newArgument = protoArg.transform(argBuilder -> argBuilder.value(null));
directiveBuilder.argument(newArgument);
}
});
}
}