Skip to content

Commit

Permalink
Merge pull request #19169 from phillip-kruger/graphql-schema-hide
Browse files Browse the repository at this point in the history
GraphQL: New config option to make the schema unavailable
  • Loading branch information
phillip-kruger authored Aug 2, 2021
2 parents e9a5368 + 468a463 commit a01f9ef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public class SmallRyeGraphQLConfig {
@ConfigItem(defaultValue = "false")
boolean printDataFetcherException;

/**
* Make the schema available over HTTP.
*/
@ConfigItem(defaultValue = "true")
boolean schemaAvailable;

/**
* Include the Scalar definitions in the schema.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ void buildSchemaEndpoint(
SmallRyeGraphQLRecorder recorder,
SmallRyeGraphQLConfig graphQLConfig) {

Handler<RoutingContext> schemaHandler = recorder.schemaHandler(graphQLInitializedBuildItem.getInitialized());
Handler<RoutingContext> schemaHandler = recorder.schemaHandler(graphQLInitializedBuildItem.getInitialized(),
graphQLConfig.schemaAvailable);

routeProducer.produce(httpRootPathBuildItem.routeBuilder()
.nestedRoute(graphQLConfig.rootPath, SCHEMA_PATH)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.smallrye.graphql.deployment;

import java.util.HashMap;
import java.util.Map;

import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

/**
* Test when schema is not available
*/
public class GraphQLSchemaUnavailableTest extends AbstractGraphQLTest {

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

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestResource.class, TestPojo.class, TestRandom.class, TestGenericsPojo.class,
BusinessException.class)
.addAsResource(new StringAsset(getPropertyAsString(configuration())), "application.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void testSchema() {
RequestSpecification request = RestAssured.given();
request.accept(MEDIATYPE_TEXT);
request.contentType(MEDIATYPE_TEXT);
Response response = request.get("/graphql/schema.graphql");

Assertions.assertEquals(404, response.statusCode());
}

private static Map<String, String> configuration() {
Map<String, String> m = new HashMap<>();
m.put("quarkus.smallrye-graphql.schema-available", "false");
return m;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public Handler<RoutingContext> subscriptionHandler(BeanContainer beanContainer,
CDI.current().select(CurrentVertxRequest.class).get());
}

public Handler<RoutingContext> schemaHandler(RuntimeValue<Boolean> initialized) {
if (initialized.getValue()) {
public Handler<RoutingContext> schemaHandler(RuntimeValue<Boolean> initialized, boolean schemaAvailable) {
if (initialized.getValue() && schemaAvailable) {
return new SmallRyeGraphQLSchemaHandler();
} else {
return new SmallRyeGraphQLNoEndpointHandler();
Expand Down

0 comments on commit a01f9ef

Please sign in to comment.