From 02b6804a487188907492df3161a96c3d5bf61848 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Fri, 22 Mar 2024 10:18:38 +0100 Subject: [PATCH] Add sortOrder to schema and add tests --- docs/apis/GraphQL-Tutorial.md | 1 - .../apis/gtfs/datafetchers/RouteImpl.java | 5 + .../gtfs/generated/GraphQLDataFetchers.java | 5 +- .../opentripplanner/apis/gtfs/schema.graphqls | 6 + .../apis/gtfs/GraphQLIntegrationTest.java | 12 ++ .../gtfs/expectations/routes-extended.json | 160 ++++++++++++++++++ .../apis/gtfs/queries/routes-extended.graphql | 14 ++ .../apis/gtfs/queries/routes.graphql | 1 - 8 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/org/opentripplanner/apis/gtfs/expectations/routes-extended.json create mode 100644 src/test/resources/org/opentripplanner/apis/gtfs/queries/routes-extended.graphql diff --git a/docs/apis/GraphQL-Tutorial.md b/docs/apis/GraphQL-Tutorial.md index 92645f9b245..e8bf41032b9 100644 --- a/docs/apis/GraphQL-Tutorial.md +++ b/docs/apis/GraphQL-Tutorial.md @@ -41,7 +41,6 @@ GraphQL query in the left hand panel of the page: name } mode - bikesAllowed } } ``` diff --git a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/RouteImpl.java b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/RouteImpl.java index 56775024e0b..9f9b3c60b31 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/RouteImpl.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/RouteImpl.java @@ -183,6 +183,11 @@ public DataFetcher shortName() { return environment -> getSource(environment).getShortName(); } + @Override + public DataFetcher sortOrder() { + return env -> getSource(env).getGtfsSortOrder(); + } + @Override public DataFetcher> stops() { return this::getStops; diff --git a/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java b/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java index 7fab5720b9a..559acac920d 100644 --- a/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java +++ b/src/main/java/org/opentripplanner/apis/gtfs/generated/GraphQLDataFetchers.java @@ -763,7 +763,8 @@ public interface GraphQLQueryType { public DataFetcher viewer(); } - public interface GraphQLRealtimeEstimate { + /** Real-time estimates for a vehicle at a certain place. */ + public interface GraphQLRealTimeEstimate { public DataFetcher delay(); public DataFetcher time(); @@ -864,6 +865,8 @@ public interface GraphQLRoute { public DataFetcher shortName(); + public DataFetcher sortOrder(); + public DataFetcher> stops(); public DataFetcher textColor(); diff --git a/src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls b/src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls index c1189f21ba1..eb608f34c39 100644 --- a/src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls +++ b/src/main/resources/org/opentripplanner/apis/gtfs/schema.graphqls @@ -3426,6 +3426,12 @@ type Route implements Node { """ types: [RouteAlertType] ): [Alert] + + """ + Orders the routes in a way which is ideal for presentation to passengers. + Routes with smaller values should be displayed first. + """ + sortOrder: Int } """Entities that are relevant for routes that can contain alerts""" diff --git a/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java b/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java index f630e0cee3c..9fafac0b352 100644 --- a/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java +++ b/src/test/java/org/opentripplanner/apis/gtfs/GraphQLIntegrationTest.java @@ -159,6 +159,7 @@ static void setup() { .route(m.name()) .withMode(m) .withLongName(I18NString.of("Long name for %s".formatted(m))) + .withGtfsSortOrder(sortOrder(m)) .build() ) .toList(); @@ -283,6 +284,17 @@ public TransitAlertService getTransitAlertService() { ); } + /** + * We want to provide a variety of numbers and null. + */ + private static Integer sortOrder(TransitMode m) { + if (m.ordinal() == 0) { + return null; + } else { + return m.ordinal(); + } + } + private static void add10MinuteDelay(Itinerary i1) { i1.transformTransitLegs(tl -> { if (tl instanceof ScheduledTransitLeg stl) { diff --git a/src/test/resources/org/opentripplanner/apis/gtfs/expectations/routes-extended.json b/src/test/resources/org/opentripplanner/apis/gtfs/expectations/routes-extended.json new file mode 100644 index 00000000000..d0528a51257 --- /dev/null +++ b/src/test/resources/org/opentripplanner/apis/gtfs/expectations/routes-extended.json @@ -0,0 +1,160 @@ +{ + "data" : { + "routes" : [ + { + "longName" : "Long name for CARPOOL", + "shortName" : "RCARPOOL", + "gtfsId" : "F:CARPOOL", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "CARPOOL", + "sortOrder" : 12 + }, + { + "longName" : "Long name for SUBWAY", + "shortName" : "RSUBWAY", + "gtfsId" : "F:SUBWAY", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "SUBWAY", + "sortOrder" : 2 + }, + { + "longName" : "Long name for BUS", + "shortName" : "RBUS", + "gtfsId" : "F:BUS", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "BUS", + "sortOrder" : 3 + }, + { + "longName" : "Long name for FERRY", + "shortName" : "RFERRY", + "gtfsId" : "F:FERRY", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "FERRY", + "sortOrder" : 5 + }, + { + "longName" : "Long name for COACH", + "shortName" : "RCOACH", + "gtfsId" : "F:COACH", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "COACH", + "sortOrder" : 1 + }, + { + "longName" : "Long name for TRAM", + "shortName" : "RTRAM", + "gtfsId" : "F:TRAM", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "TRAM", + "sortOrder" : 4 + }, + { + "longName" : "Long name for CABLE_CAR", + "shortName" : "RCABLE_CAR", + "gtfsId" : "F:CABLE_CAR", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "CABLE_CAR", + "sortOrder" : 7 + }, + { + "longName" : "Long name for FUNICULAR", + "shortName" : "RFUNICULAR", + "gtfsId" : "F:FUNICULAR", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "FUNICULAR", + "sortOrder" : 9 + }, + { + "longName" : "Long name for RAIL", + "shortName" : "RRAIL", + "gtfsId" : "F:RAIL", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "RAIL", + "sortOrder" : null + }, + { + "longName" : "Long name for MONORAIL", + "shortName" : "RMONORAIL", + "gtfsId" : "F:MONORAIL", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "MONORAIL", + "sortOrder" : 11 + }, + { + "longName" : "Long name for GONDOLA", + "shortName" : "RGONDOLA", + "gtfsId" : "F:GONDOLA", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "GONDOLA", + "sortOrder" : 8 + }, + { + "longName" : "Long name for TROLLEYBUS", + "shortName" : "RTROLLEYBUS", + "gtfsId" : "F:TROLLEYBUS", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "TROLLEYBUS", + "sortOrder" : 10 + }, + { + "longName" : "Long name for AIRPLANE", + "shortName" : "RAIRPLANE", + "gtfsId" : "F:AIRPLANE", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "AIRPLANE", + "sortOrder" : 6 + }, + { + "longName" : "Long name for TAXI", + "shortName" : "RTAXI", + "gtfsId" : "F:TAXI", + "agency" : { + "gtfsId" : "F:A1", + "name" : "Agency Test" + }, + "mode" : "TAXI", + "sortOrder" : 13 + } + ] + } +} \ No newline at end of file diff --git a/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes-extended.graphql b/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes-extended.graphql new file mode 100644 index 00000000000..50d89980442 --- /dev/null +++ b/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes-extended.graphql @@ -0,0 +1,14 @@ +{ + routes { + longName + shortName + gtfsId + agency { + gtfsId + name + } + mode + sortOrder + bikesAllowed + } +} \ No newline at end of file diff --git a/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes.graphql b/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes.graphql index 38585d6a836..66d7f370c3f 100644 --- a/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes.graphql +++ b/src/test/resources/org/opentripplanner/apis/gtfs/queries/routes.graphql @@ -8,6 +8,5 @@ name } mode - bikesAllowed } } \ No newline at end of file