Package graphql

Class GraphQLContext


  • @PublicApi
    @ThreadSafe
    public class GraphQLContext
    extends java.lang.Object
    This context object can be used to contain key values that can be useful as "context" when executing DataFetchers
     
         DataFetcher df = new DataFetcher() {
            public Object get(DataFetchingEnvironment env) {
                GraphQLContext ctx = env.getGraphqlContext()
                User currentUser = ctx.getOrDefault("userKey",new AnonymousUser())
                ...
            }
         }
     
     
    You can set this up via ExecutionInput.getGraphQLContext() All keys and values in the context MUST be non null. The class is mutable via a thread safe implementation but it is recommended to try to use this class in an immutable way if you can.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  GraphQLContext.Builder  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T compute​(java.lang.Object key, java.util.function.BiFunction<java.lang.Object,​? super T,​? extends T> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      <T> T computeIfAbsent​(java.lang.Object key, java.util.function.Function<java.lang.Object,​? extends T> mappingFunction)
      If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
      <T> T computeIfPresent​(java.lang.Object key, java.util.function.BiFunction<java.lang.Object,​? super T,​? extends T> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
      GraphQLContext delete​(java.lang.Object key)
      Deletes a key in the context
      boolean equals​(java.lang.Object o)  
      <T> T get​(java.lang.Object key)
      Returns a value in the context by key
      boolean getBoolean​(java.lang.Object key)
      This returns true if the value at the specified key is equal to Boolean.TRUE
      boolean getBoolean​(java.lang.Object key, java.lang.Boolean defaultValue)
      This returns true if the value at the specified key is equal to Boolean.TRUE or the default value if the key is missing
      static GraphQLContext getDefault()  
      <T> T getOrDefault​(java.lang.Object key, T defaultValue)
      Returns a value in the context by key
      <T> java.util.Optional<T> getOrEmpty​(java.lang.Object key)
      Returns a Optional value in the context by key
      int hashCode()  
      boolean hasKey​(java.lang.Object key)
      Returns true if the context contains a value for that key
      static GraphQLContext.Builder newContext()
      Creates a new GraphqlContext builder
      static GraphQLContext of​(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
      Creates a new GraphqlContext with the map of context added to it
      static GraphQLContext of​(java.util.Map<?,​java.lang.Object> mapOfContext)
      Creates a new GraphqlContext with the map of context added to it
      GraphQLContext put​(java.lang.Object key, java.lang.Object value)
      Puts a value into the context
      GraphQLContext putAll​(GraphQLContext context)
      Puts all of the values into the context
      GraphQLContext putAll​(GraphQLContext.Builder contextBuilder)
      Puts all of the values into the context
      GraphQLContext putAll​(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
      Puts all of the values into the context
      GraphQLContext putAll​(java.util.Map<?,​java.lang.Object> map)
      Puts all of the values into the context
      java.util.stream.Stream<java.util.Map.Entry<java.lang.Object,​java.lang.Object>> stream()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • delete

        public GraphQLContext delete​(java.lang.Object key)
        Deletes a key in the context
        Parameters:
        key - the key to delete
        Returns:
        this GraphQLContext object
      • get

        public <T> T get​(java.lang.Object key)
        Returns a value in the context by key
        Type Parameters:
        T - for two
        Parameters:
        key - the key to look up
        Returns:
        a value or null
      • getOrDefault

        public <T> T getOrDefault​(java.lang.Object key,
                                  T defaultValue)
        Returns a value in the context by key
        Type Parameters:
        T - for two
        Parameters:
        key - the key to look up
        defaultValue - the default value to use if these is no key entry
        Returns:
        a value or default value
      • getOrEmpty

        public <T> java.util.Optional<T> getOrEmpty​(java.lang.Object key)
        Returns a Optional value in the context by key
        Type Parameters:
        T - for two
        Parameters:
        key - the key to look up
        Returns:
        a value or an empty optional value
      • getBoolean

        public boolean getBoolean​(java.lang.Object key)
        This returns true if the value at the specified key is equal to Boolean.TRUE
        Parameters:
        key - the key to look up
        Returns:
        true if the value is equal to Boolean.TRUE
      • getBoolean

        public boolean getBoolean​(java.lang.Object key,
                                  java.lang.Boolean defaultValue)
        This returns true if the value at the specified key is equal to Boolean.TRUE or the default value if the key is missing
        Parameters:
        key - the key to look up
        defaultValue - the value to use if the key is not present
        Returns:
        true if the value is equal to Boolean.TRUE
      • hasKey

        public boolean hasKey​(java.lang.Object key)
        Returns true if the context contains a value for that key
        Parameters:
        key - the key to lookup
        Returns:
        true if there is a value for that key
      • put

        public GraphQLContext put​(java.lang.Object key,
                                  java.lang.Object value)
        Puts a value into the context
        Parameters:
        key - the key to set
        value - the new value (which not must be null)
        Returns:
        this GraphQLContext object
      • putAll

        public GraphQLContext putAll​(java.util.Map<?,​java.lang.Object> map)
        Puts all of the values into the context
        Parameters:
        map - the map of values to use
        Returns:
        this GraphQLContext object
      • putAll

        public GraphQLContext putAll​(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
        Puts all of the values into the context
        Parameters:
        contextBuilderConsumer - a call back to that gives out a builder to use
        Returns:
        this GraphQLContext object
      • compute

        public <T> T compute​(java.lang.Object key,
                             java.util.function.BiFunction<java.lang.Object,​? super T,​? extends T> remappingFunction)
        Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
        Type Parameters:
        T - for two
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
      • computeIfAbsent

        public <T> T computeIfAbsent​(java.lang.Object key,
                                     java.util.function.Function<java.lang.Object,​? extends T> mappingFunction)
        If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
        Type Parameters:
        T - for two
        Parameters:
        key - key with which the specified value is to be associated
        mappingFunction - the function to compute a value
        Returns:
        the current (existing or computed) value associated with the specified key, or null if the computed value is null
      • computeIfPresent

        public <T> T computeIfPresent​(java.lang.Object key,
                                      java.util.function.BiFunction<java.lang.Object,​? super T,​? extends T> remappingFunction)
        If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
        Type Parameters:
        T - for two
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
      • stream

        public java.util.stream.Stream<java.util.Map.Entry<java.lang.Object,​java.lang.Object>> stream()
        Returns:
        a stream of entries in the context
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • of

        public static GraphQLContext of​(java.util.Map<?,​java.lang.Object> mapOfContext)
        Creates a new GraphqlContext with the map of context added to it
        Parameters:
        mapOfContext - the map of context value to use
        Returns:
        the new GraphqlContext
      • of

        public static GraphQLContext of​(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
        Creates a new GraphqlContext with the map of context added to it
        Parameters:
        contextBuilderConsumer - a callback that is given a new builder
        Returns:
        the new GraphqlContext
      • getDefault

        public static GraphQLContext getDefault()
        Returns:
        a new and empty graphql context object
      • newContext

        public static GraphQLContext.Builder newContext()
        Creates a new GraphqlContext builder
        Returns:
        the new builder