Skip to content

Commit

Permalink
Add leg type to GraphQL API
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 11, 2024
1 parent c00ea1e commit be93a97
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.opentripplanner.apis.gtfs.GraphQLRequestContext;
import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers;
import org.opentripplanner.apis.gtfs.generated.GraphQLTypes;
import org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLLegType;
import org.opentripplanner.apis.gtfs.mapping.LegTypeMapper;
import org.opentripplanner.apis.gtfs.mapping.NumberMapper;
import org.opentripplanner.ext.restapi.mapping.LocalDateMapper;
import org.opentripplanner.ext.ridehailing.model.RideEstimate;
Expand Down Expand Up @@ -262,6 +264,12 @@ public DataFetcher<Trip> trip() {
return environment -> getSource(environment).getTrip();
}

@Override
public DataFetcher<GraphQLLegType> type() {
return environment ->
LegTypeMapper.map(getSource(environment));
}

@Override
public DataFetcher<Boolean> walkingBike() {
return environment -> getSource(environment).getWalkingBike();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ public interface GraphQLLeg {

public DataFetcher<Trip> trip();

public DataFetcher<org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLLegType> type();

public DataFetcher<Boolean> walkingBike();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,12 @@ public void setGraphQLOriginModesWithParentStation(
}
}

public enum GraphQLLegType {
FLEX,
SCHEDULED_TRANSIT,
STREET,
}

public static class GraphQLLocalDateRangeInput {

private java.time.LocalDate end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ config:
FareMedium: org.opentripplanner.model.fare.FareMedium#FareMedium
RiderCategory: org.opentripplanner.model.fare.RiderCategory#RiderCategory
StopPosition: org.opentripplanner.apis.gtfs.model.StopPosition#StopPosition
LegType: org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLLegType

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.opentripplanner.apis.gtfs.mapping;

import javax.annotation.Nonnull;
import org.opentripplanner.apis.gtfs.generated.GraphQLTypes;
import org.opentripplanner.ext.flex.FlexibleTransitLeg;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.ScheduledTransitLeg;
import org.opentripplanner.model.plan.StreetLeg;

public class LegTypeMapper {


@Nonnull
public static GraphQLTypes.GraphQLLegType map(Leg source) {
return switch (source) {
case StreetLeg ignored -> GraphQLTypes.GraphQLLegType.STREET;
case FlexibleTransitLeg ignored -> GraphQLTypes.GraphQLLegType.FLEX;
case ScheduledTransitLeg ignored -> GraphQLTypes.GraphQLLegType.SCHEDULED_TRANSIT;
default -> throw new IllegalStateException("Unhandled leg type: " + source);
};
}
}
14 changes: 14 additions & 0 deletions src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ type Leg {
transitLeg: Boolean
"For transit legs, the trip that is used for traversing the leg. For non-transit legs, `null`."
trip: Trip
"What the type of the leg is."
type: LegType!
"Whether this leg is walking with a bike."
walkingBike: Boolean
}
Expand Down Expand Up @@ -2886,6 +2888,18 @@ enum ItineraryFilterDebugProfile {
OFF
}

enum LegType {
"""
Leg is on a flexible trip that uses flexible time windows, areas or groups of stops. This may or may not be classed
as transit, depending the type of service.
"""
FLEX
"Leg is on a transit vehicle like bus, train or ferry which follows a published schedule, potentially with added real time information."
SCHEDULED_TRANSIT
"Leg traverses the street network, for example by walking to a stop, using a rental scooter or driving."
STREET
}

"Identifies whether this stop represents a stop or station."
enum LocationType {
ENTRANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"walkTime" : 20,
"legs" : [
{
"type" : "STREET",
"mode" : "WALK",
"start" : {
"scheduledTime" : "2020-02-02T11:00:00Z",
Expand Down Expand Up @@ -67,6 +68,7 @@
"accessibilityScore" : null
},
{
"type" : "SCHEDULED_TRANSIT",
"mode" : "BUS",
"start" : {
"scheduledTime" : "2020-02-02T10:51:00Z",
Expand Down Expand Up @@ -157,6 +159,7 @@
"accessibilityScore" : null
},
{
"type" : "SCHEDULED_TRANSIT",
"mode" : "RAIL",
"start" : {
"scheduledTime" : "2020-02-02T11:20:00Z",
Expand Down Expand Up @@ -267,6 +270,7 @@
"accessibilityScore" : null
},
{
"type" : "STREET",
"mode" : "CAR",
"start" : {
"scheduledTime" : "2020-02-02T11:50:00Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
walkDistance
walkTime
legs {
type
mode
start {
scheduledTime
Expand Down

0 comments on commit be93a97

Please sign in to comment.