/** * 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); }
/** * 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); }
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); } }
@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 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; }
@Override public DataFetcher newFieldDataFetcher(final IGraphQLObjectMapper objectMapper, final Optional<Object> targetObject, final Field field, String fieldName, Class<? extends DataFetcher> dataFetcher) { if (dataFetcher != null) { try { return dataFetcher.newInstance(); } catch (IllegalAccessException | InstantiationException ex) { throw Throwables.propagate(ex); } } else { return new PropertyDataFetcher(fieldName); } }
private DataFetcher getBooleanDataFetcher(DataFetcher actualDataFetcher) { if (checkIfPrefixGetterExists(field.getDeclaringClass(), "is", field.getName()) || checkIfPrefixGetterExists(field.getDeclaringClass(), "get", field.getName())) { actualDataFetcher = wrapExtension(new PropertyDataFetcher(field.getName()), field); } return actualDataFetcher; }
private DataFetcher<?> dataFetcherOfLastResort(FieldWiringEnvironment environment) { String fieldName = environment.getFieldDefinition().getName(); return new PropertyDataFetcher(fieldName); }
private DataFetcher handleNullCase(DataFetcher actualDataFetcher) { // if there is getter for fields type, use propertyDataFetcher, otherwise use method directly if (isaBoolean()) { actualDataFetcher = getBooleanDataFetcher(actualDataFetcher); } else if (checkIfPrefixGetterExists(field.getDeclaringClass(), "get", field.getName())) { actualDataFetcher = wrapExtension(new PropertyDataFetcher(field.getName()), field); } else { actualDataFetcher = getDataFetcherWithFluentGetter(actualDataFetcher); } if (actualDataFetcher == null) { actualDataFetcher = wrapExtension(new PropertyDataFetcher(field.getName()), field); } return actualDataFetcher; }
@Override public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) { return new PropertyDataFetcher(environment.getFieldDefinition().getName()); }
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); } }
@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); } }; }
public static GraphQLFieldDefinition string (String name) { return newFieldDefinition() .name(name) .type(GraphQLString) .dataFetcher(new PropertyDataFetcher(name)) .build(); }
public static GraphQLFieldDefinition intt (String name) { return newFieldDefinition() .name(name) .type(GraphQLInt) .dataFetcher(new PropertyDataFetcher(name)) .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; }
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 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; }