@PublicApi @ThreadSafe public class GraphQLContext extends java.lang.Object
DataFetcher
s
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.Modifier and Type | Class and Description |
---|---|
static class |
GraphQLContext.Builder |
Modifier and Type | Method and Description |
---|---|
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
|
<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(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
Puts all of the values into the context
|
GraphQLContext |
putAll(GraphQLContext.Builder contextBuilder)
Puts all of the values into the context
|
GraphQLContext |
putAll(GraphQLContext context)
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() |
public GraphQLContext delete(java.lang.Object key)
key
- the key to deletepublic <T> T get(java.lang.Object key)
T
- for twokey
- the key to look uppublic <T> T getOrDefault(java.lang.Object key, T defaultValue)
T
- for twokey
- the key to look updefaultValue
- the default value to use if these is no key entrypublic <T> java.util.Optional<T> getOrEmpty(java.lang.Object key)
Optional
value in the context by keyT
- for twokey
- the key to look uppublic boolean hasKey(java.lang.Object key)
key
- the key to lookuppublic GraphQLContext put(java.lang.Object key, java.lang.Object value)
key
- the key to setvalue
- the new value (which not must be null)GraphQLContext
objectpublic GraphQLContext putAll(java.util.Map<?,java.lang.Object> map)
map
- the map of values to useGraphQLContext
objectpublic GraphQLContext putAll(GraphQLContext context)
context
- the other context to useGraphQLContext
objectpublic GraphQLContext putAll(GraphQLContext.Builder contextBuilder)
contextBuilder
- the other context to useGraphQLContext
objectpublic GraphQLContext putAll(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
contextBuilderConsumer
- a call back to that gives out a builder to useGraphQLContext
objectpublic java.util.stream.Stream<java.util.Map.Entry<java.lang.Object,java.lang.Object>> stream()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static GraphQLContext of(java.util.Map<?,java.lang.Object> mapOfContext)
mapOfContext
- the map of context value to usepublic static GraphQLContext of(java.util.function.Consumer<GraphQLContext.Builder> contextBuilderConsumer)
contextBuilderConsumer
- a callback that is given a new builderpublic static GraphQLContext.Builder newContext()