Skip to content

Commit

Permalink
Merge pull request #6330 from entur/otp2_add_schema_endpoint
Browse files Browse the repository at this point in the history
Add endpoint to download Transmodel GraphQL Schema
  • Loading branch information
t2gran authored Dec 11, 2024
2 parents da0d520 + f765199 commit cd254bf
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.SchemaPrinter;
import io.micrometer.core.instrument.Tag;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
Expand All @@ -31,6 +33,15 @@
@Produces(MediaType.APPLICATION_JSON)
public class TransmodelAPI {

// Note, the blank line at the end is intended
private static final String SCHEMA_DOC_HEADER =
"""
# THIS IS NOT INTENDED FOR PRODUCTION USE. We recommend using the GraphQL introspection instead.
# This is intended for the OTP Debug UI and can also be used by humans to get the schema with the
# OTP configured default-values injected.
""";

private static final Logger LOG = LoggerFactory.getLogger(TransmodelAPI.class);

private static GraphQLSchema schema;
Expand Down Expand Up @@ -138,6 +149,13 @@ public Response getGraphQL(String query, @Context HttpHeaders headers) {
);
}

@GET
@Path("schema.graphql")
public Response getGraphQLSchema() {
var text = SCHEMA_DOC_HEADER + new SchemaPrinter().print(schema);
return Response.ok().encoding("UTF-8").entity(text).build();
}

private static Iterable<Tag> getTagsFromHeaders(HttpHeaders headers) {
return tracingHeaderTags
.stream()
Expand Down

0 comments on commit cd254bf

Please sign in to comment.