Skip to content

Commit

Permalink
feature: Inject custom documentation in Transmodel API
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Jan 2, 2025
1 parent d2fd01b commit 90ac6d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.opentripplanner.apis.support.graphql.injectdoc.ApiDocumentationProfile;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
Expand Down Expand Up @@ -80,6 +81,7 @@ public static void setUp(
TransmodelAPIParameters config,
TimetableRepository timetableRepository,
RouteRequest defaultRouteRequest,
ApiDocumentationProfile documentationProfile,
TransitRoutingConfig transitRoutingConfig
) {
if (config.hideFeedId()) {
Expand All @@ -91,6 +93,7 @@ public static void setUp(
TransmodelGraphQLSchema.create(
defaultRouteRequest,
timetableRepository.getTimeZone(),
documentationProfile,
transitRoutingConfig
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.SchemaTransformer;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
Expand All @@ -42,8 +43,12 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.opentripplanner.apis.support.graphql.injectdoc.ApiDocumentationProfile;
import org.opentripplanner.apis.support.graphql.injectdoc.CustomDocumentation;
import org.opentripplanner.apis.support.graphql.injectdoc.InjectCustomDocumentation;
import org.opentripplanner.apis.transmodel.mapping.PlaceMapper;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.apis.transmodel.model.DefaultRouteRequestType;
Expand Down Expand Up @@ -155,10 +160,12 @@ private TransmodelGraphQLSchema(
public static GraphQLSchema create(
RouteRequest defaultRequest,
ZoneId timeZoneId,
TransitTuningParameters transitTuningParameters
ApiDocumentationProfile docProfile,
TransitTuningParameters transitTuning
) {
return new TransmodelGraphQLSchema(defaultRequest, timeZoneId, transitTuningParameters)
.create();
var schema = new TransmodelGraphQLSchema(defaultRequest, timeZoneId, transitTuning).create();
schema = decorateSchemaWithCustomDocumentation(schema, docProfile);
return schema;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1620,17 +1627,31 @@ private GraphQLSchema create() {
.field(DatedServiceJourneyQuery.createQuery(datedServiceJourneyType))
.build();

return GraphQLSchema
var schema = GraphQLSchema
.newSchema()
.query(queryType)
.additionalType(placeInterface)
.additionalType(timetabledPassingTime)
.additionalType(Relay.pageInfoType)
.additionalDirective(TransmodelDirectives.TIMING_DATA)
.build();

return schema;
}

private static GraphQLSchema decorateSchemaWithCustomDocumentation(
GraphQLSchema schema,
ApiDocumentationProfile docProfile
) {
var customDocumentation = CustomDocumentation.of(docProfile);
if (customDocumentation.isEmpty()) {
return schema;
}
var visitor = new InjectCustomDocumentation(customDocumentation);
return SchemaTransformer.transformSchema(schema, visitor);
}

private List<FeedScopedId> toIdList(List<String> ids) {
private List<FeedScopedId> toIdList(@Nullable List<String> ids) {
if (ids == null) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private void setupTransitRoutingServer() {
routerConfig().transmodelApi(),
timetableRepository(),
routerConfig().routingRequestDefaults(),
routerConfig().server().apiDocumentationProfile(),
routerConfig().transitTuningConfig()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.File;
import org.junit.jupiter.api.Test;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.apis.support.graphql.injectdoc.ApiDocumentationProfile;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TransitTuningParameters;
import org.opentripplanner.routing.api.request.RouteRequest;

Expand All @@ -23,6 +24,7 @@ void testSchemaBuild() {
var schema = TransmodelGraphQLSchema.create(
new RouteRequest(),
ZoneIds.OSLO,
ApiDocumentationProfile.DEFAULT,
TransitTuningParameters.FOR_TEST
);
assertNotNull(schema);
Expand Down

0 comments on commit 90ac6d2

Please sign in to comment.