@PublicApi public class PropertyDataFetcher<T> extends java.lang.Object implements LightDataFetcher<T>
It uses the following strategies
You can write your own data fetchers to get data from some other backing system if you need highly customised behaviour.
DataFetcher
Constructor and Description |
---|
PropertyDataFetcher(java.lang.String propertyName)
This constructor will use the property name and examine the
DataFetchingEnvironment.getSource()
object for a getter method or field with that name. |
Modifier and Type | Method and Description |
---|---|
static void |
clearReflectionCache()
PropertyDataFetcher caches the methods and fields that map from a class to a property for runtime performance reasons
as well as negative misses.
|
static <T,O> PropertyDataFetcher<T> |
fetching(java.util.function.Function<O,T> function)
Returns a data fetcher that will present the
DataFetchingEnvironment.getSource() object to the supplied
function to obtain a value, which allows you to use Java 8 method references say obtain values in a
more type safe way. |
static <T> PropertyDataFetcher<T> |
fetching(java.lang.String propertyName)
Returns a data fetcher that will use the property name to examine the
DataFetchingEnvironment.getSource() object
for a getter method or field with that name, or if it's a map, it will look up a value using
property name as a key. |
T |
get(DataFetchingEnvironment environment)
This is called by the graphql engine to fetch the value.
|
T |
get(GraphQLFieldDefinition fieldDefinition,
java.lang.Object source,
java.util.function.Supplier<DataFetchingEnvironment> environmentSupplier)
This is called to by the engine to get a value from the source object in a lightweight fashion.
|
java.lang.String |
getPropertyName() |
static boolean |
setUseNegativeCache(boolean flag)
This can be used to control whether PropertyDataFetcher will cache negative lookups for a property for performance reasons.
|
static boolean |
setUseSetAccessible(boolean flag)
This can be used to control whether PropertyDataFetcher will use
AccessibleObject.setAccessible(boolean) to gain access to property
values. |
public PropertyDataFetcher(java.lang.String propertyName)
DataFetchingEnvironment.getSource()
object for a getter method or field with that name.propertyName
- the name of the property to retrievepublic static <T> PropertyDataFetcher<T> fetching(java.lang.String propertyName)
DataFetchingEnvironment.getSource()
object
for a getter method or field with that name, or if it's a map, it will look up a value using
property name as a key.
For example :
DataFetcher functionDataFetcher = fetching("pojoPropertyName");
T
- the type of resultpropertyName
- the name of the property to retrievepublic static <T,O> PropertyDataFetcher<T> fetching(java.util.function.Function<O,T> function)
DataFetchingEnvironment.getSource()
object to the supplied
function to obtain a value, which allows you to use Java 8 method references say obtain values in a
more type safe way.
For example :
DataFetcher functionDataFetcher = fetching(Thing::getId);
O
- the type of the source objectT
- the type of resultfunction
- the function to use to obtain a value from the source objectpublic java.lang.String getPropertyName()
public T get(GraphQLFieldDefinition fieldDefinition, java.lang.Object source, java.util.function.Supplier<DataFetchingEnvironment> environmentSupplier) throws java.lang.Exception
LightDataFetcher
DataFetchingEnvironment
is wrapped
in a supplier that is only created on demand.
If you are a lightweight data fetcher (like PropertyDataFetcher
is) then you can implement this method to have a more lightweight
method invocation. However, if you need field arguments etc. during fetching (most custom fetchers will) then you should use implement
DataFetcher.get(DataFetchingEnvironment)
.
get
in interface LightDataFetcher<T>
fieldDefinition
- the graphql field definitionsource
- the source object to get a value fromenvironmentSupplier
- a supplier of the DataFetchingEnvironment
that creates it lazilyDataFetcherResult
java.lang.Exception
- to relieve the implementations from having to wrap checked exceptions. Any exception thrown
from a DataFetcher
will eventually be handled by the registered DataFetcherExceptionHandler
and the related field will have a value of null
in the result.public T get(DataFetchingEnvironment environment)
DataFetcher
DataFetchingEnvironment
is a composite
context object that tells you all you need to know about how to fetch a data value in graphql type terms.get
in interface DataFetcher<T>
environment
- this is the data fetching environment which contains all the context you need to fetch a valueDataFetcherResult
public static void clearReflectionCache()
However during development you might be using an assistance tool like JRebel to allow you to tweak your code base and this caching may interfere with this. So you can call this method to clear the cache. A JRebel plugin could be developed to do just that.
public static boolean setUseSetAccessible(boolean flag)
AccessibleObject.setAccessible(boolean)
to gain access to property
values. By default it PropertyDataFetcher WILL use setAccessible.flag
- whether to use setAccessiblepublic static boolean setUseNegativeCache(boolean flag)
flag
- whether to cache misses