/** * 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 : * <pre> * {@code * * DataFetcher functionDataFetcher = fetching("pojoPropertyName"); * * } * </pre> * * @param propertyName the name of the property to retrieve * @param <T> the type of result * * @return a new PropertyDataFetcher using the provided function as its source of values */ public static <T> PropertyDataFetcher<T> fetching(String propertyName) { return new PropertyDataFetcher<>(propertyName); }
@Override public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) { return env -> { GraphQLOutputType fieldType = env.getFieldType(); if (fieldType instanceof GraphQLObjectType) { return fakeObjectValue((GraphQLObjectType) fieldType); } else { PropertyDataFetcher<Object> df = new PropertyDataFetcher<>(env.getFieldDefinition().getName()); return df.get(env); } }; }
private Object getPropertyViaGetter(Object object, GraphQLOutputType outputType, DataFetchingEnvironment environment) { try { return getPropertyViaGetterMethod(object, outputType, this::findPubliclyAccessibleMethod, environment); } catch (NoSuchMethodException ignored) { try { return getPropertyViaGetterMethod(object, outputType, this::findViaSetAccessible, environment); } catch (NoSuchMethodException ignored2) { return getPropertyViaFieldAccess(object); } } }
private Object getPropertyViaGetterMethod(Object object, GraphQLOutputType outputType, MethodFinder methodFinder, DataFetchingEnvironment environment) throws NoSuchMethodException { if (isBooleanProperty(outputType)) { try { return getPropertyViaGetterUsingPrefix(object, "is", methodFinder, environment); } catch (NoSuchMethodException e) { return getPropertyViaGetterUsingPrefix(object, "get", methodFinder, environment); } } else { return getPropertyViaGetterUsingPrefix(object, "get", methodFinder, environment); } }
@Override public Object get(DataFetchingEnvironment environment) { final Object result = super.get(environment); if (flip) { return !(Boolean)result; } else { return result; } } }
private void directWiring() { GraphQLFieldDefinition descriptionField = GraphQLFieldDefinition.newFieldDefinition() .name("description") .type(Scalars.GraphQLString) .dataFetcher(PropertyDataFetcher.fetching("desc")) .build(); }
@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); } if (source instanceof Map) { return (T) ((Map<?, ?>) source).get(propertyName); } return (T) getPropertyViaGetter(source, environment.getFieldType(), environment); }
private Object getPropertyViaGetterMethod(Object object, GraphQLOutputType outputType, MethodFinder methodFinder, DataFetchingEnvironment environment) throws NoSuchMethodException { if (isBooleanProperty(outputType)) { try { return getPropertyViaGetterUsingPrefix(object, "is", methodFinder, environment); } catch (NoSuchMethodException e) { return getPropertyViaGetterUsingPrefix(object, "get", methodFinder, environment); } } else { return getPropertyViaGetterUsingPrefix(object, "get", methodFinder, environment); } }
@Override public Object get (DataFetchingEnvironment aEnvironment) { Object source = aEnvironment.getSource(); if(source == null) { return null; } if(source instanceof Element) { Element element = (Element) source; if(propertyName.equals("id")) { return element.id(); } else { return element.property(propertyName); } } return super.get(aEnvironment); }
DataFetcher dataFetcher = PropertyDataFetcher.fetching(attribute.getName());
@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); } if (source instanceof Map) { return (T) ((Map<?, ?>) source).get(propertyName); } return (T) getPropertyViaGetter(source, environment.getFieldType(), environment); }
/** * 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 : * <pre> * {@code * * DataFetcher functionDataFetcher = fetching(Thing::getId); * * } * </pre> * * @param function the function to use to obtain a value from the source object * @param <O> the type of the source object * @param <T> the type of result * * @return a new PropertyDataFetcher using the provided function as its source of values */ public static <T, O> PropertyDataFetcher<T> fetching(Function<O, T> function) { return new PropertyDataFetcher<>(function); }
@Override public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) { return env -> { GraphQLOutputType fieldType = env.getFieldType(); if (fieldType instanceof GraphQLObjectType) { return fakeObjectValue((GraphQLObjectType) fieldType); } else { PropertyDataFetcher<Object> df = new PropertyDataFetcher<>(env.getFieldDefinition().getName()); return df.get(env); } }; }
private Object getPropertyViaGetter(Object object, GraphQLOutputType outputType, DataFetchingEnvironment environment) { try { return getPropertyViaGetterMethod(object, outputType, this::findPubliclyAccessibleMethod, environment); } catch (NoSuchMethodException ignored) { try { return getPropertyViaGetterMethod(object, outputType, this::findViaSetAccessible, environment); } catch (NoSuchMethodException ignored2) { return getPropertyViaFieldAccess(object); } } }
private DataFetcher<?> dataFetcherOfLastResort(FieldWiringEnvironment environment) { String fieldName = environment.getFieldDefinition().getName(); return new PropertyDataFetcher(fieldName); }
public GraphQLFieldDefinition build() { if (dataFetcherFactory == null) { dataFetcherFactory = DataFetcherFactories.useDataFetcher(new PropertyDataFetcher<>(name)); } return new GraphQLFieldDefinition(name, description, type, dataFetcherFactory, valuesToList(arguments), deprecationReason, valuesToList(directives), definition); } }
private static DataFetcher getDataFetcherImpl(GraphQLFieldsContainer parentType, GraphQLFieldDefinition fieldDefinition, Map<FieldCoordinates, DataFetcherFactory> dataFetcherMap, Map<String, DataFetcherFactory> systemDataFetcherMap) { assertNotNull(parentType); assertNotNull(fieldDefinition); DataFetcherFactory dataFetcherFactory = systemDataFetcherMap.get(fieldDefinition.getName()); if (dataFetcherFactory == null) { dataFetcherFactory = dataFetcherMap.get(FieldCoordinates.coordinates(parentType, fieldDefinition)); if (dataFetcherFactory == null) { dataFetcherFactory = DataFetcherFactories.useDataFetcher(new PropertyDataFetcher<>(fieldDefinition.getName())); } } return dataFetcherFactory.get(newDataFetchingFactoryEnvironment() .fieldDefinition(fieldDefinition) .build()); }
@Override public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition node, TraverserContext<GraphQLType> context) { GraphQLFieldsContainer parentContainerType = (GraphQLFieldsContainer) context.getParentContext().thisNode(); DataFetcher dataFetcher = node.getDataFetcher(); if (dataFetcher == null) { dataFetcher = new PropertyDataFetcher<>(node.getName()); } FieldCoordinates coordinates = coordinates(parentContainerType, node); codeRegistry.dataFetcherIfAbsent(coordinates, dataFetcher); return CONTINUE; }
@Override public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) { GraphQLFieldDefinition field = environment.getElement(); String fetchName = atFetchFromSupport(field.getName(), field.getDirectives()); DataFetcher dataFetcher = new PropertyDataFetcher(fetchName); environment.getCodeRegistry().dataFetcher(coordinates(environment.getFieldsContainer(), field), dataFetcher); return field; }
/** * 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 : * <pre> * {@code * * DataFetcher functionDataFetcher = fetching("pojoPropertyName"); * * } * </pre> * * @param propertyName the name of the property to retrieve * @param <T> the type of result * * @return a new PropertyDataFetcher using the provided function as its source of values */ public static <T> PropertyDataFetcher<T> fetching(String propertyName) { return new PropertyDataFetcher<>(propertyName); }