@PublicApi public class PropertyDataFetcher<T> extends java.lang.Object implements DataFetcher<T>, TrivialDataFetcher<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 its 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.
|
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 its 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(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