Class ValueTraverser

java.lang.Object
graphql.analysis.values.ValueTraverser

@PublicApi public class ValueTraverser extends Object
This class allows you to traverse a set of input values according to the type system and optional change the values present.

If you just want to traverse without changing anything, just return the value presented to you and nothing will change.

If you want to change a value, perhaps in the presence of a directive say on the containing element, then return a new value back in your visitor.

This class is intended to be used say inside a DataFetcher, allowing you to change the DataFetchingEnvironment.getArguments() say before further processing.

The values passed in are assumed to be valid and coerced. This classes does not check for non nullness say or the right coerced objects given the type system. This is assumed to have occurred earlier in the graphql validation phase. This also means if you are not careful you can undo the validation that has gone before you. For example, it would be possible to change values that are illegal according to the type system, such as null values for non-nullable types say, so you need to be careful.

  • Constructor Details

    • ValueTraverser

      public ValueTraverser()
  • Method Details

    • visitPreOrder

      public static DataFetchingEnvironment visitPreOrder(DataFetchingEnvironment environment, ValueVisitor visitor)
      This will visit the arguments of a DataFetchingEnvironment and if the values are changed by the visitor a new environment will be built
      Parameters:
      environment - the starting data fetching environment
      visitor - the visitor to use
      Returns:
      the same environment if nothing changes or a new one with the DataFetchingEnvironment.getArguments() changed
    • visitPreOrder

      public static Map<String,Object> visitPreOrder(Map<String,Object> coercedArgumentValues, GraphQLFieldDefinition fieldDefinition, ValueVisitor visitor)
      This will visit the arguments of a GraphQLFieldDefinition and if the visitor changes the values, it will return a new set of arguments
      Parameters:
      coercedArgumentValues - the starting coerced arguments
      fieldDefinition - the field definition
      visitor - the visitor to use
      Returns:
      the same set of arguments if nothing changes or new ones if the visitor changes anything
    • visitPreOrder

      public static Object visitPreOrder(Object coercedArgumentValue, GraphQLArgument argument, ValueVisitor visitor)
      This will visit a single argument of a GraphQLArgument and if the visitor changes the value, it will return a new argument value

      Note you cannot return the ABSENCE_SENTINEL from this method as its makes no sense to be somehow make the argument disappear. Use visitPreOrder(Map, GraphQLFieldDefinition, ValueVisitor) say to remove arguments in the fields map of arguments.

      Parameters:
      coercedArgumentValue - the starting coerced argument value
      argument - the argument definition
      visitor - the visitor to use
      Returns:
      the same value if nothing changes or a new value if the visitor changes anything
    • visitPreOrder

      public static Object visitPreOrder(Object coercedArgumentValue, GraphQLAppliedDirectiveArgument argument, ValueVisitor visitor)
      This will visit a single argument of a GraphQLAppliedDirective and if the visitor changes the value, it will return a new argument value

      Note you cannot return the ABSENCE_SENTINEL from this method as its makes no sense to be somehow make the argument disappear.

      Parameters:
      coercedArgumentValue - the starting coerced argument value
      argument - the applied argument
      visitor - the visitor to use
      Returns:
      the same value if nothing changes or a new value if the visitor changes anything