Class PropertyDataFetcher<T>
- java.lang.Object
-
- graphql.schema.PropertyDataFetcher<T>
-
- All Implemented Interfaces:
DataFetcher<T>
,LightDataFetcher<T>
,TrivialDataFetcher<T>
@PublicApi public class PropertyDataFetcher<T> extends java.lang.Object implements LightDataFetcher<T>
This is the default data fetcher used in graphql-java, and it will examine maps, records and POJO java beans for values that match the desired name, typically the field name, or it will use a provided function to obtain values.It uses the following strategies
- If the source is null, return null
- If the source is a Map, return map.get(propertyName)
- If a function is provided, it is used
- Find a public JavaBean getter method named `getPropertyName()` or `isPropertyName()`
- Find any getter method named `getPropertyName()` or `isPropertyName()` and call method.setAccessible(true)
- Find a public field named `propertyName`
- Find any field named `propertyName` and call field.setAccessible(true)
- Find a public Record like method named `propertyName()`
- If this cant find anything, then null is returned
You can write your own data fetchers to get data from some other backing system if you need highly customised behaviour.
- See Also:
DataFetcher
-
-
Constructor Summary
Constructors Constructor Description PropertyDataFetcher(java.lang.String propertyName)
This constructor will use the property name and examine theDataFetchingEnvironment.getSource()
object for a getter method or field with that name.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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> PropertyDataFetcher<T>
fetching(java.lang.String propertyName)
Returns a data fetcher that will use the property name to examine theDataFetchingEnvironment.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.static <T,O>
PropertyDataFetcher<T>fetching(java.util.function.Function<O,T> function)
Returns a data fetcher that will present theDataFetchingEnvironment.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.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 useMethod.setAccessible(boolean)
to gain access to property values.
-
-
-
Constructor Detail
-
PropertyDataFetcher
public PropertyDataFetcher(java.lang.String propertyName)
This constructor will use the property name and examine theDataFetchingEnvironment.getSource()
object for a getter method or field with that name.- Parameters:
propertyName
- the name of the property to retrieve
-
-
Method Detail
-
fetching
public static <T> PropertyDataFetcher<T> fetching(java.lang.String propertyName)
Returns a data fetcher that will use the property name to examine theDataFetchingEnvironment.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");
- Type Parameters:
T
- the type of result- Parameters:
propertyName
- the name of the property to retrieve- Returns:
- a new PropertyDataFetcher using the provided function as its source of values
-
fetching
public static <T,O> PropertyDataFetcher<T> fetching(java.util.function.Function<O,T> function)
Returns a data fetcher that will present theDataFetchingEnvironment.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);
- Type Parameters:
O
- the type of the source objectT
- the type of result- Parameters:
function
- the function to use to obtain a value from the source object- Returns:
- a new PropertyDataFetcher using the provided function as its source of values
-
getPropertyName
public java.lang.String getPropertyName()
- Returns:
- the property that this is fetching for
-
get
public T get(GraphQLFieldDefinition fieldDefinition, java.lang.Object source, java.util.function.Supplier<DataFetchingEnvironment> environmentSupplier) throws java.lang.Exception
Description copied from interface:LightDataFetcher
This is called to by the engine to get a value from the source object in a lightweight fashion. Only the field and source object are passed in a materialised way. The more heavy weightDataFetchingEnvironment
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 implementDataFetcher.get(DataFetchingEnvironment)
.- Specified by:
get
in interfaceLightDataFetcher<T>
- Parameters:
fieldDefinition
- the graphql field definitionsource
- the source object to get a value fromenvironmentSupplier
- a supplier of theDataFetchingEnvironment
that creates it lazily- Returns:
- a value of type T. May be wrapped in a
DataFetcherResult
- Throws:
java.lang.Exception
- to relieve the implementations from having to wrap checked exceptions. Any exception thrown from aDataFetcher
will eventually be handled by the registeredDataFetcherExceptionHandler
and the related field will have a value ofnull
in the result.
-
get
public T get(DataFetchingEnvironment environment)
Description copied from interface:DataFetcher
This is called by the graphql engine to fetch the value. TheDataFetchingEnvironment
is a composite context object that tells you all you need to know about how to fetch a data value in graphql type terms.- Specified by:
get
in interfaceDataFetcher<T>
- Parameters:
environment
- this is the data fetching environment which contains all the context you need to fetch a value- Returns:
- a value of type T. May be wrapped in a
DataFetcherResult
-
clearReflectionCache
public 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.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.
-
setUseSetAccessible
public static boolean setUseSetAccessible(boolean flag)
This can be used to control whether PropertyDataFetcher will useMethod.setAccessible(boolean)
to gain access to property values. By default it PropertyDataFetcher WILL use setAccessible.- Parameters:
flag
- whether to use setAccessible- Returns:
- the previous value of the flag
-
setUseNegativeCache
public static boolean setUseNegativeCache(boolean flag)
This can be used to control whether PropertyDataFetcher will cache negative lookups for a property for performance reasons. By default it PropertyDataFetcher WILL cache misses.- Parameters:
flag
- whether to cache misses- Returns:
- the previous value of the flag
-
-