All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.schema.PropertyDataFetcher Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.schema;


import graphql.Assert;
import graphql.PublicApi;
import graphql.TrivialDataFetcher;

import java.util.function.Function;

/**
 * This is the default data fetcher used in graphql-java.  It will examine
 * maps 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.
 * maps and POJO java beans for values that match the desired name.
 * 

* 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 `propertyName`
  • *
  • Find any getter method named `propertyName` 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 graphql.schema.DataFetcher */ @PublicApi public class PropertyDataFetcher implements DataFetcher, TrivialDataFetcher { private final String propertyName; private final Function function; /** * This constructor will use the property name and examine the {@link DataFetchingEnvironment#getSource()} * object for a getter method or field with that name. * * @param propertyName the name of the property to retrieve */ public PropertyDataFetcher(String propertyName) { this.propertyName = Assert.assertNotNull(propertyName); this.function = null; } @SuppressWarnings("unchecked") private PropertyDataFetcher(Function function) { this.function = (Function) Assert.assertNotNull(function); this.propertyName = null; } /** * Returns a data fetcher that will use the property name to examine the {@link 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 : *

     * {@code
     *
     *      DataFetcher functionDataFetcher = fetching("pojoPropertyName");
     *
     * }
     * 
* * @param propertyName the name of the property to retrieve * @param the type of result * @return a new PropertyDataFetcher using the provided function as its source of values */ public static PropertyDataFetcher fetching(String propertyName) { return new PropertyDataFetcher<>(propertyName); } /** * Returns a data fetcher that will present the {@link 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 : *

     * {@code
     *
     *      DataFetcher functionDataFetcher = fetching(Thing::getId);
     *
     * }
     * 
* * @param function the function to use to obtain a value from the source object * @param the type of the source object * @param the type of result * @return a new PropertyDataFetcher using the provided function as its source of values */ public static PropertyDataFetcher fetching(Function function) { return new PropertyDataFetcher<>(function); } /** * @return the property that this is fetching for */ public String getPropertyName() { return propertyName; } @SuppressWarnings("unchecked") @Override public T get(DataFetchingEnvironment environment) { Object source = environment.getSource(); if (source == null) { return null; } if (function != null) { return (T) function.apply(source); } return (T) PropertyDataFetcherHelper.getPropertyValue(propertyName, source, environment.getFieldType(), environment); } /** * 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. */ @SuppressWarnings("unused") public static void clearReflectionCache() { PropertyDataFetcherHelper.clearReflectionCache(); } /** * This can be used to control whether PropertyDataFetcher will use {@link java.lang.reflect.Method#setAccessible(boolean)} to gain access to property * values. By default it PropertyDataFetcher WILL use setAccessible. * * @param flag whether to use setAccessible * @return the previous value of the flag */ public static boolean setUseSetAccessible(boolean flag) { return PropertyDataFetcherHelper.setUseSetAccessible(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. * * @param flag whether to cache misses * @return the previous value of the flag */ public static boolean setUseNegativeCache(boolean flag) { return PropertyDataFetcherHelper.setUseNegativeCache(flag); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy