Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose station entrances in GraphQL API #6082

Open
wants to merge 1 commit into
base: dev-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.site.Entrance;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.site.StopLocation;
Expand Down Expand Up @@ -668,6 +669,36 @@ public DataFetcher<Iterable<Object>> stations() {
};
}

@Override
public DataFetcher<Iterable<Object>> entrances() {
return environment -> {
var args = new GraphQLTypes.GraphQLQueryTypeEntrancesArgs(environment.getArguments());

TransitService transitService = getTransitService(environment);

if (args.getGraphQLIds() != null) {
return args
.getGraphQLIds()
.stream()
.map(FeedScopedId::parse)
.map(transitService::getEntranceById)
.collect(Collectors.toList());
}

Stream<Entrance> entranceStream = transitService.getEntrances().stream();

if (args.getGraphQLName() != null) {
String name = args.getGraphQLName().toLowerCase(environment.getLocale());
entranceStream =
entranceStream.filter(entrance ->
GraphQLUtils.startsWith(entrance.getName(), name, environment.getLocale())
);
}

return entranceStream.collect(Collectors.toList());
};
}

@Override
public DataFetcher<Object> stop() {
return environment ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.Entrance;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.Station;
import org.opentripplanner.transit.model.site.StopLocation;
Expand All @@ -44,7 +45,12 @@ public DataFetcher<Iterable<TransitAlert>> alerts() {
TransitAlertService alertService = getTransitService(environment).getTransitAlertService();
var args = new GraphQLTypes.GraphQLStopAlertsArgs(environment.getArguments());
List<GraphQLTypes.GraphQLStopAlertType> types = args.getGraphQLTypes();
FeedScopedId id = getValue(environment, StopLocation::getId, AbstractTransitEntity::getId);
FeedScopedId id = getValue(
environment,
StopLocation::getId,
AbstractTransitEntity::getId,
Entrance::getId
);
if (types != null) {
Collection<TransitAlert> alerts = new ArrayList<>();
if (types.contains(GraphQLTypes.GraphQLStopAlertType.STOP)) {
Expand Down Expand Up @@ -127,7 +133,8 @@ public DataFetcher<Object> cluster() {

@Override
public DataFetcher<String> code() {
return environment -> getValue(environment, StopLocation::getCode, Station::getCode);
return environment ->
getValue(environment, StopLocation::getCode, Station::getCode, Entrance::getCode);
}

@Override
Expand All @@ -144,6 +151,11 @@ public DataFetcher<String> desc() {
org.opentripplanner.framework.graphql.GraphQLUtils.getTranslation(
station.getDescription(),
environment
),
entrance ->
org.opentripplanner.framework.graphql.GraphQLUtils.getTranslation(
entrance.getDescription(),
environment
)
);
}
Expand All @@ -162,18 +174,27 @@ public DataFetcher<String> url() {
org.opentripplanner.framework.graphql.GraphQLUtils.getTranslation(
station.getUrl(),
environment
)
),
// TODO: Entrances could theoretically support URLs, but not currently in model.
entrance -> null
);
}

@Override
public DataFetcher<Object> locationType() {
return environment -> getValue(environment, stop -> "STOP", station -> "STATION");
return environment ->
getValue(environment, stop -> "STOP", station -> "STATION", entrance -> "ENTRANCE");
}

@Override
public DataFetcher<Object> parentStation() {
return environment -> getValue(environment, StopLocation::getParentStation, station -> null);
return environment ->
getValue(
environment,
StopLocation::getParentStation,
station -> null,
Entrance::getParentStation
);
}

// TODO
Expand All @@ -184,13 +205,19 @@ public DataFetcher<String> direction() {

@Override
public DataFetcher<Object> geometries() {
return environment -> getValue(environment, StopLocation::getGeometry, Station::getGeometry);
return environment ->
getValue(environment, StopLocation::getGeometry, Station::getGeometry, null);
}

@Override
public DataFetcher<String> gtfsId() {
return environment ->
getValue(environment, stop -> stop.getId().toString(), station -> station.getId().toString());
getValue(
environment,
stop -> stop.getId().toString(),
station -> station.getId().toString(),
entrance -> entrance.getId().toString()
);
}

@Override
Expand All @@ -199,18 +226,31 @@ public DataFetcher<Relay.ResolvedGlobalId> id() {
getValue(
environment,
stop -> new Relay.ResolvedGlobalId("Stop", stop.getId().toString()),
station -> new Relay.ResolvedGlobalId("Stop", station.getId().toString())
station -> new Relay.ResolvedGlobalId("Stop", station.getId().toString()),
entrance -> new Relay.ResolvedGlobalId("Stop", entrance.getId().toString())
);
}

@Override
public DataFetcher<Double> lat() {
return environment -> getValue(environment, StopLocation::getLat, Station::getLat);
return environment ->
getValue(
environment,
StopLocation::getLat,
Station::getLat,
entrance -> entrance.getCoordinate().latitude()
);
}

@Override
public DataFetcher<Double> lon() {
return environment -> getValue(environment, StopLocation::getLon, Station::getLon);
return environment ->
getValue(
environment,
StopLocation::getLon,
Station::getLon,
entrance -> entrance.getCoordinate().longitude()
);
}

@Override
Expand All @@ -227,6 +267,11 @@ public DataFetcher<String> name() {
org.opentripplanner.framework.graphql.GraphQLUtils.getTranslation(
station.getName(),
environment
),
entrance ->
org.opentripplanner.framework.graphql.GraphQLUtils.getTranslation(
entrance.getName(),
environment
)
);
}
Expand All @@ -238,7 +283,8 @@ public DataFetcher<Iterable<TripPattern>> patterns() {

@Override
public DataFetcher<String> platformCode() {
return environment -> getValue(environment, StopLocation::getPlatformCode, station -> null);
return environment ->
getValue(environment, StopLocation::getPlatformCode, station -> null, entrance -> null);
}

@Override
Expand Down Expand Up @@ -283,7 +329,8 @@ public DataFetcher<Iterable<TripTimeOnDate>> stopTimesForPattern() {
!args.getGraphQLOmitCanceled()
);
},
station -> null
station -> null,
entrance -> null
);
}

Expand All @@ -293,7 +340,24 @@ public DataFetcher<Iterable<Object>> stops() {
getValue(
environment,
stop -> null,
station -> new ArrayList<Object>(station.getChildStops())
station -> new ArrayList<Object>(station.getChildStops()),
entrance -> null
);
}

@Override
public DataFetcher<Iterable<Object>> entrances() {
return environment ->
getValue(
environment,
stop -> null,
station ->
station
.getEntrances()
.stream()
.sorted((e1, e2) -> e1.getId().compareTo(e2.getId()))
.collect(Collectors.toList()),
entrances -> null
);
}

Expand Down Expand Up @@ -322,7 +386,8 @@ public DataFetcher<Iterable<StopTimesInPattern>> stoptimesForPatterns() {
.stream()
.map(stopTFunction)
.flatMap(Collection::stream)
.collect(Collectors.toList())
.collect(Collectors.toList()),
entrance -> null
);
};
}
Expand Down Expand Up @@ -358,7 +423,8 @@ public DataFetcher<Iterable<StopTimesInPattern>> stoptimesForServiceDate() {
.stream()
.map(stopTFunction)
.flatMap(Collection::stream)
.collect(Collectors.toList())
.collect(Collectors.toList()),
entrance -> null
);
};
}
Expand All @@ -384,7 +450,8 @@ public DataFetcher<Iterable<TripTimeOnDate>> stoptimesWithoutPatterns() {
Stream<StopTimesInPattern> stream = getValue(
environment,
stopTFunction,
station -> station.getChildStops().stream().flatMap(stopTFunction)
station -> station.getChildStops().stream().flatMap(stopTFunction),
entrance -> Stream.of()
);

return stream
Expand All @@ -401,7 +468,8 @@ public DataFetcher<String> timezone() {
getValue(
environment,
stop -> stop.getTimeZone().toString(),
station -> station.getTimezone().toString()
station -> station.getTimezone().toString(),
entrance -> null
);
}

Expand All @@ -426,7 +494,8 @@ public DataFetcher<Iterable<NearbyStop>> transfers() {
)
.collect(Collectors.toList());
},
station -> null
station -> null,
entrance -> null
);
}

Expand All @@ -449,7 +518,8 @@ public DataFetcher<String> vehicleMode() {
.stream()
.findFirst()
.map(Enum::toString)
.orElse(null)
.orElse(null),
entrance -> null
);
};
}
Expand All @@ -466,7 +536,8 @@ public DataFetcher<GraphQLTypes.GraphQLWheelchairBoarding> wheelchairBoarding()
var boarding = getValue(
environment,
StopLocation::getWheelchairAccessibility,
station -> null
station -> null,
Entrance::getWheelchairAccessibility
);
return GraphQLUtils.toGraphQL(boarding);
};
Expand All @@ -475,22 +546,24 @@ public DataFetcher<GraphQLTypes.GraphQLWheelchairBoarding> wheelchairBoarding()
@Override
public DataFetcher<String> zoneId() {
return environment ->
getValue(environment, StopLocation::getFirstZoneAsString, station -> null);
getValue(environment, StopLocation::getFirstZoneAsString, station -> null, null);
}

private Collection<TripPattern> getPatterns(DataFetchingEnvironment environment) {
return getValue(
environment,
stop -> getTransitService(environment).getPatternsForStop(stop, true),
station -> null
station -> null,
entrance -> null
);
}

private Collection<Route> getRoutes(DataFetchingEnvironment environment) {
return getValue(
environment,
stop -> getTransitService(environment).getRoutesForStop(stop),
station -> null
station -> null,
entrance -> null
);
}

Expand Down Expand Up @@ -557,13 +630,16 @@ private Stream<TripPattern> getRealtimeAddedPatternsAsStream(
private static <T> T getValue(
DataFetchingEnvironment environment,
Function<StopLocation, T> stopTFunction,
Function<Station, T> stationTFunction
Function<Station, T> stationTFunction,
Function<Entrance, T> entranceFunction
) {
Object source = environment.getSource();
if (source instanceof StopLocation) {
return stopTFunction.apply((StopLocation) source);
} else if (source instanceof Station) {
return stationTFunction.apply((Station) source);
} else if (source instanceof Entrance) {
return entranceFunction.apply((Entrance) source);
}
return null;
}
Expand Down
Loading
Loading