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 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()` using LambdaMetafactory.metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)
  • Find a public Record like method named `propertyName()`
  • 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)
  • 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:
  • Constructor Details

    • PropertyDataFetcher

      public PropertyDataFetcher(String propertyName)
      This constructor will use the property name and examine the DataFetchingEnvironment.getSource() object for a getter method or field with that name.
      Parameters:
      propertyName - the name of the property to retrieve
  • Method Details

    • fetching

      public static <T> PropertyDataFetcher<T> fetching(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.

      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(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.

      For example :

       
      
            DataFetcher functionDataFetcher = fetching(Thing::getId);
      
       
       
      Type Parameters:
      T - the type of result
      O - the type of the source object
      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 String getPropertyName()
      Returns:
      the property that this is fetching for
    • get

      public T get(GraphQLFieldDefinition fieldDefinition, Object source, Supplier<DataFetchingEnvironment> environmentSupplier) throws 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 weight 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).

      Specified by:
      get in interface LightDataFetcher<T>
      Parameters:
      fieldDefinition - the graphql field definition
      source - the source object to get a value from
      environmentSupplier - a supplier of the DataFetchingEnvironment that creates it lazily
      Returns:
      a value of type T. May be wrapped in a DataFetcherResult
      Throws:
      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.
    • get

      public T get(DataFetchingEnvironment environment)
      Description copied from interface: DataFetcher
      This is called by the graphql engine to fetch the value. The 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.
      Specified by:
      get in interface DataFetcher<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 use Method.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