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

com.conveyal.gtfs.graphql.fetchers.MapFetcher Maven / Gradle / Ivy

Go to download

A library to load and index GTFS feeds of arbitrary size using disk-backed storage

There is a newer version: 6.2.0
Show newest version
package com.conveyal.gtfs.graphql.fetchers;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLOutputType;

import java.util.Map;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;

/**
 * This just grabs an entry out of a Map
 * It allows pulling a single field out of the result of a DataFetcher that always returns all fields
 * (like our JDBC SQL fetcher).
 */
public class MapFetcher implements DataFetcher {

    final String key;

    public MapFetcher(String key) {
        this.key = key;
    }

    @Override
    public Object get(DataFetchingEnvironment dataFetchingEnvironment) {
        Object source = dataFetchingEnvironment.getSource();
        return ((Map)source).get(key);
    }

    public static GraphQLFieldDefinition field(String name) {
        return field(name, GraphQLString);
    }

    public static GraphQLFieldDefinition field(String name, GraphQLOutputType type) {
        return newFieldDefinition()
                .name(name)
                .type(type)
                .dataFetcher(new MapFetcher(name))
                .build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy