Skip to content

Commit

Permalink
Merge pull request #6045 from HSLdevcom/gfts-leg-node-refetch
Browse files Browse the repository at this point in the history
Refetch transit leg with a leg query of GTFS GraphQL API
  • Loading branch information
vesameskanen authored Sep 27, 2024
2 parents cd5277e + 857b815 commit 2cfbd7b
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opentripplanner.model.plan.StreetLeg;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.model.plan.WalkStep;
import org.opentripplanner.model.plan.legreference.LegReferenceSerializer;
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs;
import org.opentripplanner.routing.alternativelegs.AlternativeLegsFilter;
Expand Down Expand Up @@ -189,10 +190,12 @@ public DataFetcher<Boolean> realTime() {
return environment -> getSource(environment).getRealTime();
}

// TODO
@Override
public DataFetcher<String> realtimeState() {
return environment -> null;
return environment -> {
var state = getSource(environment).getRealTimeState();
return (state != null) ? state.name() : null;
};
}

@Override
Expand Down Expand Up @@ -324,4 +327,15 @@ public DataFetcher<Iterable<Leg>> nextLegs() {
public DataFetcher<Double> accessibilityScore() {
return environment -> NumberMapper.toDouble(getSource(environment).accessibilityScore());
}

@Override
public DataFetcher<String> id() {
return environment -> {
var ref = getSource(environment).getLegReference();
if (ref == null) {
return null;
}
return LegReferenceSerializer.encode(ref);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.gtfs.mapping.DirectionMapper;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.legreference.LegReference;
import org.opentripplanner.model.plan.legreference.LegReferenceSerializer;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.api.request.RouteRequest;
Expand Down Expand Up @@ -362,6 +365,20 @@ public DataFetcher<Connection<PlaceAtDistance>> nearest() {
};
}

@Override
public DataFetcher<Leg> leg() {
return environment -> {
TransitService transitService = getTransitService(environment);
var args = new GraphQLTypes.GraphQLQueryTypeLegArgs(environment.getArguments());
String id = args.getGraphQLId();
LegReference ref = LegReferenceSerializer.decode(id);
if (ref == null) {
return null;
}
return ref.getLeg(transitService);
};
}

@Override
public DataFetcher<Object> node() {
return environment -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ public interface GraphQLLeg {

public DataFetcher<String> headsign();

public DataFetcher<String> id();

public DataFetcher<Boolean> interlineWithPreviousLeg();

public DataFetcher<Boolean> intermediatePlace();
Expand Down Expand Up @@ -779,6 +781,8 @@ public interface GraphQLQueryType {

public DataFetcher<Trip> fuzzyTrip();

public DataFetcher<Leg> leg();

public DataFetcher<Connection<PlaceAtDistance>> nearest();

public DataFetcher<Object> node();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,25 @@ public void setGraphQLTime(Integer time) {
}
}

public static class GraphQLQueryTypeLegArgs {

private String id;

public GraphQLQueryTypeLegArgs(Map<String, Object> args) {
if (args != null) {
this.id = (String) args.get("id");
}
}

public String getGraphQLId() {
return this.id;
}

public void setGraphQLId(String id) {
this.id = id;
}
}

public static class GraphQLQueryTypeNearestArgs {

private String after;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ private GraphQLSchema create() {
GraphQLFieldDefinition
.newFieldDefinition()
.name("leg")
.description("Refetch a single leg based on its id")
.description("Refetch a single transit leg based on its id")
.withDirective(gqlUtil.timingData)
.type(LegType.REF)
.argument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public static GraphQLObjectType create(
GraphQLFieldDefinition
.newFieldDefinition()
.name("id")
.description("An identifier for the leg, which can be used to re-fetch the information.")
.description(
"An identifier for the leg, which can be used to re-fetch transit leg information."
)
.type(Scalars.GraphQLID)
.dataFetcher(env -> LegReferenceSerializer.encode(leg(env).getLegReference()))
.build()
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/opentripplanner/model/plan/Leg.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.FareZone;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.booking.BookingInfo;
Expand Down Expand Up @@ -244,6 +245,10 @@ default boolean getRealTime() {
return false;
}

default RealTimeState getRealTimeState() {
return null;
}

/**
* Whether this Leg describes a flexible trip. The reason we need this is that FlexTrip does not
* inherit from Trip, so that the information that the Trip is flexible would be lost when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.RealTimeState;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;
Expand Down Expand Up @@ -227,6 +228,11 @@ public boolean getRealTime() {
);
}

@Override
public RealTimeState getRealTimeState() {
return tripTimes.getRealTimeState();
}

@Override
public double getDistanceMeters() {
return distanceMeters;
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ type Leg {
"""
headsign: String
"""
An identifier for the leg, which can be used to re-fetch transit leg information.
Re-fetching fails when the underlying transit data no longer exists.
"""
id: String
"""
Interlines with previous leg.
This is true when the same vehicle is used for the previous leg as for this leg
and passenger can stay inside the vehicle.
Expand Down Expand Up @@ -1166,6 +1171,11 @@ type QueryType {
time: Int!
): Trip
"""
Try refetching the current state of a transit leg using its id.
This fails when the underlying transit data (mostly IDs) has changed or are no longer available.
"""
leg(id: String!): Leg
"""
Get all places (stops, stations, etc. with coordinates) within the specified
radius from a location. The returned type is a Relay connection (see
https://facebook.github.io/relay/graphql/connections.htm). The placeAtDistance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ type Leg {
fromPlace: Place!
"Generalized cost or weight of the leg. Used for debugging."
generalizedCost: Int
"An identifier for the leg, which can be used to re-fetch the information."
"An identifier for the leg, which can be used to re-fetch transit leg information."
id: ID
interchangeFrom: Interchange
interchangeTo: Interchange
Expand Down Expand Up @@ -647,7 +647,7 @@ type QueryType {
groupOfLines(id: String!): GroupOfLines
"Get all groups of lines"
groupsOfLines: [GroupOfLines!]!
"Refetch a single leg based on its id"
"Refetch a single transit leg based on its id"
leg(id: ID!): Leg @timingData
"Get a single line based on its id"
line(id: ID!): Line @timingData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"intermediatePlaces" : null,
"alerts" : [ ],
"rideHailingEstimate" : null,
"accessibilityScore" : null
"accessibilityScore" : null,
"id": null,
"realtimeState": null
},
{
"mode" : "BUS",
Expand Down Expand Up @@ -154,7 +156,9 @@
],
"alerts" : [ ],
"rideHailingEstimate" : null,
"accessibilityScore" : null
"accessibilityScore" : null,
"id": "rO0ABXdBABhTQ0hFRFVMRURfVFJBTlNJVF9MRUdfVjMABUY6MTIyAAoyMDIwLTAyLTAyAAAABQAAAAcAA0Y6QgADRjpDAAA=",
"realtimeState": "UPDATED"
},
{
"mode" : "RAIL",
Expand Down Expand Up @@ -264,7 +268,9 @@
}
],
"rideHailingEstimate" : null,
"accessibilityScore" : null
"accessibilityScore" : null,
"id": "rO0ABXdBABhTQ0hFRFVMRURfVFJBTlNJVF9MRUdfVjMABUY6NDM5AAoyMDIwLTAyLTAyAAAABQAAAAcAA0Y6QwADRjpEAAA=",
"realtimeState": "UPDATED"
},
{
"mode" : "CAR",
Expand Down Expand Up @@ -334,11 +340,13 @@
},
"arrival" : "PT10M"
},
"accessibilityScore" : null
"accessibilityScore" : null,
"id": null,
"realtimeState": null
}
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
arrival
}
accessibilityScore
id
realtimeState
}
}
}
Expand Down

0 comments on commit 2cfbd7b

Please sign in to comment.