|
14 | 14 | */ |
15 | 15 | package graphql.annotations.directives; |
16 | 16 |
|
| 17 | +import graphql.annotations.processor.exceptions.GraphQLAnnotationsException; |
17 | 18 | import graphql.introspection.Introspection; |
18 | 19 | import graphql.schema.*; |
19 | 20 |
|
| 21 | +import java.lang.reflect.InvocationTargetException; |
20 | 22 | import java.util.Arrays; |
21 | 23 | import java.util.HashMap; |
22 | 24 | import java.util.Map; |
23 | 25 |
|
24 | 26 | public class DirectiveWirer { |
| 27 | + @FunctionalInterface |
| 28 | + interface WiringFunction { |
| 29 | + GraphQLDirectiveContainer apply(GraphQLDirective a, GraphQLDirectiveContainer b, AnnotationsDirectiveWiring wiring) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException; |
| 30 | + } |
| 31 | + |
| 32 | + private Map<Class, WiringFunction> functionMap; |
| 33 | + |
| 34 | + public DirectiveWirer() { |
| 35 | + functionMap = createFunctionsMap(); |
| 36 | + } |
| 37 | + |
| 38 | + private void putInMap(Map<Class, WiringFunction> map, Class clazz, String functionName, |
| 39 | + Introspection.DirectiveLocation... locations) { |
| 40 | + map.put(clazz, (d, e, wiring) -> { |
| 41 | + assertLocation(d, e, locations); |
| 42 | + AnnotationsWiringEnvironmentImpl environment = |
| 43 | + new AnnotationsWiringEnvironmentImpl(e, e.getDirective(d.getName())); |
| 44 | + return (GraphQLDirectiveContainer) wiring.getClass().getMethod(functionName, AnnotationsWiringEnvironment.class).invoke(wiring, environment); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + private Map<Class, WiringFunction> createFunctionsMap() { |
| 49 | + Map<Class, WiringFunction> functionMap = new HashMap<>(); |
| 50 | + putInMap(functionMap, GraphQLFieldDefinition.class, "onField", Introspection.DirectiveLocation.FIELD, Introspection.DirectiveLocation.FIELD_DEFINITION); |
| 51 | + putInMap(functionMap, GraphQLObjectType.class, "onObject", Introspection.DirectiveLocation.OBJECT); |
| 52 | + putInMap(functionMap, GraphQLArgument.class, "onArgument", Introspection.DirectiveLocation.ARGUMENT_DEFINITION); |
| 53 | + putInMap(functionMap, GraphQLInterfaceType.class, "onInterface", Introspection.DirectiveLocation.INTERFACE); |
| 54 | + putInMap(functionMap, GraphQLUnionType.class, "onUnion", Introspection.DirectiveLocation.UNION); |
| 55 | + putInMap(functionMap, GraphQLEnumType.class, "onEnum", Introspection.DirectiveLocation.ENUM); |
| 56 | + putInMap(functionMap, GraphQLEnumValueDefinition.class, "onEnumValue", Introspection.DirectiveLocation.ENUM_VALUE); |
| 57 | + putInMap(functionMap, GraphQLScalarType.class, "onScalar", Introspection.DirectiveLocation.SCALAR); |
| 58 | + putInMap(functionMap, GraphQLInputObjectType.class, "onInputObjectType", Introspection.DirectiveLocation.INPUT_OBJECT); |
| 59 | + putInMap(functionMap, GraphQLInputObjectField.class, "onInputObjectField", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION); |
| 60 | + |
| 61 | + return functionMap; |
| 62 | + } |
| 63 | + |
25 | 64 | public GraphQLDirectiveContainer wire(GraphQLDirectiveContainer element, HashMap<GraphQLDirective, AnnotationsDirectiveWiring> directiveWiringMap) { |
26 | 65 | for (Map.Entry<GraphQLDirective, AnnotationsDirectiveWiring> entry : directiveWiringMap.entrySet()) { |
27 | | - System.out.println(entry.getKey()); |
28 | 66 | GraphQLDirective graphQLDirective = entry.getKey(); |
29 | 67 | AnnotationsDirectiveWiring wiring = entry.getValue(); |
30 | 68 |
|
31 | | - if (element instanceof GraphQLFieldDefinition) { |
32 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.FIELD, Introspection.DirectiveLocation.FIELD_DEFINITION); |
33 | | - element = wiring.onField(new AnnotationsWiringEnvironmentImpl<>((GraphQLFieldDefinition) element, element.getDirective(graphQLDirective.getName()))); |
34 | | - } else if (element instanceof GraphQLObjectType) { |
35 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.OBJECT); |
36 | | - element = wiring |
37 | | - .onObject(new AnnotationsWiringEnvironmentImpl<>((GraphQLObjectType) element, element.getDirective(graphQLDirective.getName()))); |
38 | | - } else if (element instanceof GraphQLArgument) { |
39 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.ARGUMENT_DEFINITION); |
40 | | - element = wiring |
41 | | - .onArgument(new AnnotationsWiringEnvironmentImpl<>((GraphQLArgument) element, element.getDirective(graphQLDirective.getName()))); |
42 | | - } else if (element instanceof GraphQLInterfaceType) { |
43 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.INTERFACE); |
44 | | - element = wiring |
45 | | - .onInterface(new AnnotationsWiringEnvironmentImpl<>((GraphQLInterfaceType) element, element.getDirective(graphQLDirective.getName()))); |
46 | | - } else if (element instanceof GraphQLUnionType) { |
47 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.UNION); |
48 | | - element = wiring |
49 | | - .onUnion(new AnnotationsWiringEnvironmentImpl<>((GraphQLUnionType) element, element.getDirective(graphQLDirective.getName()))); |
50 | | - } else if (element instanceof GraphQLEnumType) { // todo support enum |
51 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.ENUM); |
52 | | - element = wiring |
53 | | - .onEnum(new AnnotationsWiringEnvironmentImpl<>((GraphQLEnumType) element, element.getDirective(graphQLDirective.getName()))); |
54 | | - } else if (element instanceof GraphQLEnumValueDefinition) { // todo support enum value |
55 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.ENUM_VALUE); |
56 | | - element = wiring |
57 | | - .onEnumValue(new AnnotationsWiringEnvironmentImpl<>((GraphQLEnumValueDefinition) element, element.getDirective(graphQLDirective.getName()))); |
58 | | - } else if (element instanceof GraphQLScalarType) { // todo: support scalars |
59 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.SCALAR); |
60 | | - element = wiring |
61 | | - .onScalar(new AnnotationsWiringEnvironmentImpl<>((GraphQLScalarType) element, element.getDirective(graphQLDirective.getName()))); |
62 | | - } else if (element instanceof GraphQLInputObjectType) { |
63 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.INPUT_OBJECT); |
64 | | - element = wiring |
65 | | - .onInputObjectType(new AnnotationsWiringEnvironmentImpl<>((GraphQLInputObjectType) element, element.getDirective(graphQLDirective.getName()))); |
66 | | - } else if (element instanceof GraphQLInputObjectField) { |
67 | | - assertLocation(graphQLDirective, element, Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION); |
68 | | - element = wiring |
69 | | - .onInputObjectField(new AnnotationsWiringEnvironmentImpl<>((GraphQLInputObjectField) element, element.getDirective(graphQLDirective.getName()))); |
| 69 | + Class<? extends GraphQLDirectiveContainer> aClass = element.getClass(); |
| 70 | + try { |
| 71 | + element = functionMap.get(aClass).apply(graphQLDirective, element, wiring); |
| 72 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 73 | + throw new GraphQLAnnotationsException(e.getMessage(), e); |
70 | 74 | } |
71 | 75 | } |
72 | | - return element; |
73 | 76 |
|
| 77 | + return element; |
74 | 78 | } |
75 | 79 |
|
76 | 80 | private void assertLocation(GraphQLDirective graphQLDirective, GraphQLDirectiveContainer element, Introspection.DirectiveLocation... validLocations) { |
|
0 commit comments