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