|
| 1 | +package graphql.analysis; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.execution.ConditionalNodes; |
| 5 | +import graphql.execution.ValuesResolver; |
| 6 | +import graphql.introspection.Introspection; |
| 7 | +import graphql.language.Field; |
| 8 | +import graphql.language.FragmentDefinition; |
| 9 | +import graphql.language.FragmentSpread; |
| 10 | +import graphql.language.InlineFragment; |
| 11 | +import graphql.language.Node; |
| 12 | +import graphql.language.NodeTraverser; |
| 13 | +import graphql.language.NodeVisitorStub; |
| 14 | +import graphql.language.TypeName; |
| 15 | +import graphql.schema.GraphQLCodeRegistry; |
| 16 | +import graphql.schema.GraphQLCompositeType; |
| 17 | +import graphql.schema.GraphQLFieldDefinition; |
| 18 | +import graphql.schema.GraphQLFieldsContainer; |
| 19 | +import graphql.schema.GraphQLSchema; |
| 20 | +import graphql.schema.GraphQLUnmodifiedType; |
| 21 | +import graphql.util.TraversalControl; |
| 22 | +import graphql.util.TraverserContext; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static graphql.Assert.assertNotNull; |
| 27 | +import static graphql.language.NodeTraverser.LeaveOrEnter.LEAVE; |
| 28 | +import static graphql.schema.GraphQLTypeUtil.unwrapAll; |
| 29 | + |
| 30 | +/** |
| 31 | + * Internally used node visitor which delegates to a {@link QueryVisitor} with type |
| 32 | + * information about the visited field. |
| 33 | + */ |
| 34 | +@Internal |
| 35 | +public class NodeVisitorWithTypeTracking extends NodeVisitorStub { |
| 36 | + |
| 37 | + |
| 38 | + private final QueryVisitor preOrderCallback; |
| 39 | + private final QueryVisitor postOrderCallback; |
| 40 | + private final Map<String, Object> variables; |
| 41 | + private final GraphQLSchema schema; |
| 42 | + private final Map<String, FragmentDefinition> fragmentsByName; |
| 43 | + |
| 44 | + private final ConditionalNodes conditionalNodes = new ConditionalNodes(); |
| 45 | + private final ValuesResolver valuesResolver = new ValuesResolver(); |
| 46 | + |
| 47 | + |
| 48 | + public NodeVisitorWithTypeTracking(QueryVisitor preOrderCallback, QueryVisitor postOrderCallback, Map<String, Object> variables, GraphQLSchema schema, Map<String, FragmentDefinition> fragmentsByName) { |
| 49 | + this.preOrderCallback = preOrderCallback; |
| 50 | + this.postOrderCallback = postOrderCallback; |
| 51 | + this.variables = variables; |
| 52 | + this.schema = schema; |
| 53 | + this.fragmentsByName = fragmentsByName; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public TraversalControl visitInlineFragment(InlineFragment inlineFragment, TraverserContext<Node> context) { |
| 58 | + if (!conditionalNodes.shouldInclude(variables, inlineFragment.getDirectives())) { |
| 59 | + return TraversalControl.ABORT; |
| 60 | + } |
| 61 | + |
| 62 | + QueryVisitorInlineFragmentEnvironment inlineFragmentEnvironment = new QueryVisitorInlineFragmentEnvironmentImpl(inlineFragment, context); |
| 63 | + |
| 64 | + if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { |
| 65 | + postOrderCallback.visitInlineFragment(inlineFragmentEnvironment); |
| 66 | + return TraversalControl.CONTINUE; |
| 67 | + } |
| 68 | + |
| 69 | + preOrderCallback.visitInlineFragment(inlineFragmentEnvironment); |
| 70 | + |
| 71 | + // inline fragments are allowed not have type conditions, if so the parent type counts |
| 72 | + QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); |
| 73 | + |
| 74 | + GraphQLCompositeType fragmentCondition; |
| 75 | + if (inlineFragment.getTypeCondition() != null) { |
| 76 | + TypeName typeCondition = inlineFragment.getTypeCondition(); |
| 77 | + fragmentCondition = (GraphQLCompositeType) schema.getType(typeCondition.getName()); |
| 78 | + } else { |
| 79 | + fragmentCondition = parentEnv.getRawType(); |
| 80 | + } |
| 81 | + // for unions we only have other fragments inside |
| 82 | + context.setVar(QueryTraversalContext.class, new QueryTraversalContext(fragmentCondition, fragmentCondition, parentEnv.getEnvironment(), inlineFragment)); |
| 83 | + return TraversalControl.CONTINUE; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public TraversalControl visitFragmentDefinition(FragmentDefinition node, TraverserContext<Node> context) { |
| 88 | + if (!conditionalNodes.shouldInclude(variables, node.getDirectives())) { |
| 89 | + return TraversalControl.ABORT; |
| 90 | + } |
| 91 | + |
| 92 | + if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { |
| 93 | + return TraversalControl.CONTINUE; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); |
| 98 | + GraphQLCompositeType typeCondition = (GraphQLCompositeType) schema.getType(node.getTypeCondition().getName()); |
| 99 | + context |
| 100 | + .setVar(QueryTraversalContext.class, new QueryTraversalContext(typeCondition, typeCondition, parentEnv.getEnvironment(), node)); |
| 101 | + return TraversalControl.CONTINUE; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public TraversalControl visitFragmentSpread(FragmentSpread fragmentSpread, TraverserContext<Node> context) { |
| 106 | + if (!conditionalNodes.shouldInclude(variables, fragmentSpread.getDirectives())) { |
| 107 | + return TraversalControl.ABORT; |
| 108 | + } |
| 109 | + |
| 110 | + FragmentDefinition fragmentDefinition = fragmentsByName.get(fragmentSpread.getName()); |
| 111 | + if (!conditionalNodes.shouldInclude(variables, fragmentDefinition.getDirectives())) { |
| 112 | + return TraversalControl.ABORT; |
| 113 | + } |
| 114 | + |
| 115 | + QueryVisitorFragmentSpreadEnvironment fragmentSpreadEnvironment = new QueryVisitorFragmentSpreadEnvironmentImpl(fragmentSpread, fragmentDefinition, context); |
| 116 | + if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { |
| 117 | + postOrderCallback.visitFragmentSpread(fragmentSpreadEnvironment); |
| 118 | + return TraversalControl.CONTINUE; |
| 119 | + } |
| 120 | + |
| 121 | + preOrderCallback.visitFragmentSpread(fragmentSpreadEnvironment); |
| 122 | + |
| 123 | + QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); |
| 124 | + |
| 125 | + GraphQLCompositeType typeCondition = (GraphQLCompositeType) schema.getType(fragmentDefinition.getTypeCondition().getName()); |
| 126 | + assertNotNull(typeCondition, "Invalid type condition '%s' in fragment '%s'", fragmentDefinition.getTypeCondition().getName(), |
| 127 | + fragmentDefinition.getName()); |
| 128 | + context |
| 129 | + .setVar(QueryTraversalContext.class, new QueryTraversalContext(typeCondition, typeCondition, parentEnv.getEnvironment(), fragmentDefinition)); |
| 130 | + return TraversalControl.CONTINUE; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public TraversalControl visitField(Field field, TraverserContext<Node> context) { |
| 135 | + QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); |
| 136 | + |
| 137 | + GraphQLFieldDefinition fieldDefinition = Introspection.getFieldDef(schema, parentEnv.getRawType(), field.getName()); |
| 138 | + boolean isTypeNameIntrospectionField = fieldDefinition == Introspection.TypeNameMetaFieldDef; |
| 139 | + GraphQLFieldsContainer fieldsContainer = !isTypeNameIntrospectionField ? (GraphQLFieldsContainer) unwrapAll(parentEnv.getOutputType()) : null; |
| 140 | + GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry(); |
| 141 | + Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), field.getArguments(), variables); |
| 142 | + QueryVisitorFieldEnvironment environment = new QueryVisitorFieldEnvironmentImpl(isTypeNameIntrospectionField, |
| 143 | + field, |
| 144 | + fieldDefinition, |
| 145 | + parentEnv.getOutputType(), |
| 146 | + fieldsContainer, |
| 147 | + parentEnv.getEnvironment(), |
| 148 | + argumentValues, |
| 149 | + parentEnv.getSelectionSetContainer(), |
| 150 | + context); |
| 151 | + |
| 152 | + NodeTraverser.LeaveOrEnter leaveOrEnter = context.getVar(NodeTraverser.LeaveOrEnter.class); |
| 153 | + if (leaveOrEnter == LEAVE) { |
| 154 | + postOrderCallback.visitField(environment); |
| 155 | + return TraversalControl.CONTINUE; |
| 156 | + } |
| 157 | + |
| 158 | + if (!conditionalNodes.shouldInclude(variables, field.getDirectives())) { |
| 159 | + return TraversalControl.ABORT; |
| 160 | + } |
| 161 | + |
| 162 | + preOrderCallback.visitField(environment); |
| 163 | + |
| 164 | + GraphQLUnmodifiedType unmodifiedType = unwrapAll(fieldDefinition.getType()); |
| 165 | + QueryTraversalContext fieldEnv = (unmodifiedType instanceof GraphQLCompositeType) |
| 166 | + ? new QueryTraversalContext(fieldDefinition.getType(), (GraphQLCompositeType) unmodifiedType, environment, field) |
| 167 | + : new QueryTraversalContext(null, null, environment, field);// Terminal (scalar) node, EMPTY FRAME |
| 168 | + |
| 169 | + |
| 170 | + context.setVar(QueryTraversalContext.class, fieldEnv); |
| 171 | + return TraversalControl.CONTINUE; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments