|
| 1 | +package graphql.annotations.directives; |
| 2 | + |
| 3 | +import graphql.introspection.Introspection; |
| 4 | +import graphql.schema.*; |
| 5 | +import graphql.util.TraversalControl; |
| 6 | +import graphql.util.TraverserContext; |
| 7 | + |
| 8 | +import java.lang.reflect.InvocationTargetException; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static graphql.util.TreeTransformerUtil.changeNode; |
| 15 | + |
| 16 | +public class DirectiveSchemaVisitor implements GraphQLTypeVisitor { |
| 17 | + private HashMap<String, AnnotationsDirectiveWiring> directiveWiringMap; |
| 18 | + private GraphQLCodeRegistry.Builder codeRegistryBuilder; |
| 19 | + |
| 20 | + @FunctionalInterface |
| 21 | + interface WiringFunction { |
| 22 | + GraphQLDirectiveContainer apply(GraphQLDirective a, GraphQLDirectiveContainer b, |
| 23 | + AnnotationsDirectiveWiring wiring, GraphQLSchemaElement parentElement) |
| 24 | + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException; |
| 25 | + } |
| 26 | + |
| 27 | + private Map<Class, WiringFunction> functionMap; |
| 28 | + |
| 29 | + |
| 30 | + public DirectiveSchemaVisitor(HashMap<String, AnnotationsDirectiveWiring> directiveWiringMap, GraphQLCodeRegistry.Builder codeRegistryBuilder) { |
| 31 | + this.directiveWiringMap = directiveWiringMap; |
| 32 | + this.functionMap = createFunctionsMap(); |
| 33 | + this.codeRegistryBuilder = codeRegistryBuilder; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public TraversalControl visitGraphQLArgument(GraphQLArgument node, TraverserContext<GraphQLSchemaElement> context) { |
| 38 | + return this.visitGraphQLType(GraphQLArgument.class, node, context); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + @Override |
| 43 | + public TraversalControl visitGraphQLInterfaceType(GraphQLInterfaceType node, TraverserContext<GraphQLSchemaElement> context) { |
| 44 | + return this.visitGraphQLType(GraphQLInterfaceType.class, node, context); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public TraversalControl visitGraphQLEnumType(GraphQLEnumType node, TraverserContext<GraphQLSchemaElement> context) { |
| 49 | + return this.visitGraphQLType(GraphQLEnumType.class, node, context); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public TraversalControl visitGraphQLEnumValueDefinition(GraphQLEnumValueDefinition node, TraverserContext<GraphQLSchemaElement> context) { |
| 54 | + return this.visitGraphQLType(GraphQLEnumValueDefinition.class, node, context); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition node, TraverserContext<GraphQLSchemaElement> context) { |
| 59 | + return this.visitGraphQLType(GraphQLFieldDefinition.class, node, context); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public TraversalControl visitGraphQLDirective(GraphQLDirective node, TraverserContext<GraphQLSchemaElement> context) { |
| 64 | + return TraversalControl.CONTINUE; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField node, TraverserContext<GraphQLSchemaElement> context) { |
| 69 | + return this.visitGraphQLType(GraphQLInputObjectField.class, node, context); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType node, TraverserContext<GraphQLSchemaElement> context) { |
| 74 | + return this.visitGraphQLType(GraphQLInputObjectType.class, node, context); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public TraversalControl visitGraphQLList(GraphQLList node, TraverserContext<GraphQLSchemaElement> context) { |
| 79 | + return TraversalControl.CONTINUE; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public TraversalControl visitGraphQLNonNull(GraphQLNonNull node, TraverserContext<GraphQLSchemaElement> context) { |
| 84 | + return TraversalControl.CONTINUE; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public TraversalControl visitGraphQLObjectType(GraphQLObjectType node, TraverserContext<GraphQLSchemaElement> context) { |
| 89 | + return this.visitGraphQLType(GraphQLObjectType.class, node, context); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public TraversalControl visitGraphQLScalarType(GraphQLScalarType node, TraverserContext<GraphQLSchemaElement> context) { |
| 94 | + return this.visitGraphQLType(GraphQLScalarType.class, node, context); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public TraversalControl visitGraphQLTypeReference(GraphQLTypeReference node, TraverserContext<GraphQLSchemaElement> context) { |
| 99 | + return TraversalControl.CONTINUE; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public TraversalControl visitGraphQLUnionType(GraphQLUnionType node, TraverserContext<GraphQLSchemaElement> context) { |
| 104 | + return this.visitGraphQLType(GraphQLUnionType.class, node, context); |
| 105 | + } |
| 106 | + |
| 107 | + private TraversalControl visitGraphQLType(Class<? extends GraphQLDirectiveContainer> typeOfContainer, |
| 108 | + GraphQLDirectiveContainer node, TraverserContext<GraphQLSchemaElement> context) { |
| 109 | + List<GraphQLDirective> directives = node.getDirectives(); |
| 110 | + if (directives.size() == 0) { |
| 111 | + return TraversalControl.CONTINUE; |
| 112 | + } |
| 113 | + GraphQLDirectiveContainer newNode = node; |
| 114 | + for (GraphQLDirective directive : directives) { |
| 115 | + AnnotationsDirectiveWiring wiring = this.directiveWiringMap.get(directive.getName()); |
| 116 | + if (wiring != null) { |
| 117 | + try { |
| 118 | + GraphQLSchemaElement parentElement = context.getParentNode(); |
| 119 | + newNode = functionMap.get(typeOfContainer).apply(directive, newNode, |
| 120 | + wiring, parentElement); |
| 121 | + } catch (Exception e) { |
| 122 | + e.printStackTrace(); |
| 123 | + return TraversalControl.CONTINUE; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return changeNode(context, newNode); |
| 128 | + } |
| 129 | + |
| 130 | + private void putInMap(Map<Class, WiringFunction> map, Class clazz, String functionName, |
| 131 | + Introspection.DirectiveLocation... locations) { |
| 132 | + map.put(clazz, (d, e, wiring, parentElement) -> { |
| 133 | + assertLocation(d, e, locations); |
| 134 | + AnnotationsWiringEnvironmentImpl environment = |
| 135 | + new AnnotationsWiringEnvironmentImpl(e, e.getDirective(d.getName()), parentElement, codeRegistryBuilder); |
| 136 | + return (GraphQLDirectiveContainer) wiring.getClass().getMethod(functionName, AnnotationsWiringEnvironment.class) |
| 137 | + .invoke(wiring, environment); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + private Map<Class, WiringFunction> createFunctionsMap() { |
| 142 | + Map<Class, WiringFunction> functionMap = new HashMap<>(); |
| 143 | + putInMap(functionMap, GraphQLFieldDefinition.class, "onField", Introspection.DirectiveLocation.FIELD, Introspection.DirectiveLocation.FIELD_DEFINITION); |
| 144 | + putInMap(functionMap, GraphQLObjectType.class, "onObject", Introspection.DirectiveLocation.OBJECT); |
| 145 | + putInMap(functionMap, GraphQLArgument.class, "onArgument", Introspection.DirectiveLocation.ARGUMENT_DEFINITION); |
| 146 | + putInMap(functionMap, GraphQLInterfaceType.class, "onInterface", Introspection.DirectiveLocation.INTERFACE); |
| 147 | + putInMap(functionMap, GraphQLUnionType.class, "onUnion", Introspection.DirectiveLocation.UNION); |
| 148 | + putInMap(functionMap, GraphQLEnumType.class, "onEnum", Introspection.DirectiveLocation.ENUM); |
| 149 | + putInMap(functionMap, GraphQLEnumValueDefinition.class, "onEnumValue", Introspection.DirectiveLocation.ENUM_VALUE); |
| 150 | + putInMap(functionMap, GraphQLScalarType.class, "onScalar", Introspection.DirectiveLocation.SCALAR); |
| 151 | + putInMap(functionMap, GraphQLInputObjectType.class, "onInputObjectType", Introspection.DirectiveLocation.INPUT_OBJECT); |
| 152 | + putInMap(functionMap, GraphQLInputObjectField.class, "onInputObjectField", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION); |
| 153 | + |
| 154 | + return functionMap; |
| 155 | + } |
| 156 | + |
| 157 | + private void assertLocation(GraphQLDirective graphQLDirective, GraphQLDirectiveContainer element, Introspection.DirectiveLocation... validLocations) { |
| 158 | + boolean isSupported = false; |
| 159 | + for (Introspection.DirectiveLocation validLocation : validLocations) { |
| 160 | + if (graphQLDirective.validLocations().contains(validLocation)) { |
| 161 | + isSupported = true; |
| 162 | + } |
| 163 | + } |
| 164 | + if (!isSupported) { |
| 165 | + throw getInvalidDirectiveLocationException(element, graphQLDirective, validLocations); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private InvalidDirectiveLocationException getInvalidDirectiveLocationException(GraphQLDirectiveContainer element, GraphQLDirective graphQLDirective, Introspection.DirectiveLocation... validLocations) { |
| 170 | + return new InvalidDirectiveLocationException("The element: '" + element.getName() + "' is annotated with the directive: '" |
| 171 | + + graphQLDirective.getName() + "' which is not valid on the element location: '" + Arrays.toString(Arrays.stream(validLocations).map(Enum::name).toArray()) + "'", null); |
| 172 | + } |
| 173 | +} |
0 commit comments