Skip to content

Commit 2bce110

Browse files
authored
Merge pull request #44044 from mskacelik/issue-43903
Changed quarkus.smallrye-graphql.field-visibility config property to be during ConfigPhase.RUN_TIME
2 parents d085393 + 3286452 commit 2bce110

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;
2+
3+
import static io.restassured.RestAssured.given;
4+
5+
import jakarta.ws.rs.core.MediaType;
6+
7+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
8+
import org.jboss.shrinkwrap.api.asset.StringAsset;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.quarkus.test.QuarkusUnitTest;
13+
14+
public class FieldVisibilityNoIntrospectionTest {
15+
16+
@RegisterExtension
17+
static QuarkusUnitTest test = new QuarkusUnitTest()
18+
.withApplicationRoot((jar) -> jar
19+
.addClasses(FieldVisibilityResource.Book.class, FieldVisibilityResource.Customer.class,
20+
FieldVisibilityResource.Purchase.class, FieldVisibilityResource.class)
21+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
22+
.addAsResource(
23+
new StringAsset("quarkus.smallrye-graphql.schema-include-introspection-types=true\n" +
24+
"quarkus.smallrye-graphql.field-visibility=no-introspection"),
25+
"application.properties"));
26+
27+
@Test
28+
void testSchemaWithInvisibleFields() {
29+
given()
30+
.when()
31+
.accept(MediaType.APPLICATION_JSON)
32+
.get("/graphql/schema.graphql")
33+
.then()
34+
.statusCode(200)
35+
.and().body(containsStringButNoFields("type __Directive"))
36+
.and().body(containsStringButNoFields("type __EnumValue"))
37+
.and().body(containsStringButNoFields("type __Field"))
38+
.and().body(containsStringButNoFields("type __InputValue"))
39+
.and().body(containsStringButNoFields("type __Schema"))
40+
.and().body(containsStringButNoFields("type __Type"));
41+
}
42+
43+
private org.hamcrest.Matcher<String> containsStringButNoFields(String s) {
44+
return new org.hamcrest.BaseMatcher<String>() {
45+
@Override
46+
public boolean matches(Object item) {
47+
return ((String) item).contains(s) && !((String) item).contains(s + " {");
48+
}
49+
50+
@Override
51+
public void describeTo(org.hamcrest.Description description) {
52+
description.appendText("a string containing ").appendValue(s)
53+
.appendText(" but not containing (without fields) ").appendValue(s);
54+
}
55+
};
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;
2+
3+
import org.eclipse.microprofile.graphql.GraphQLApi;
4+
import org.eclipse.microprofile.graphql.Query;
5+
6+
// It is important to note that some of the fields are invisible in the GraphQL schema,
7+
// this is because they have been explicitly hidden via the
8+
// quarkus.smallrye-graphql.field-visibility property.
9+
10+
@GraphQLApi
11+
public class FieldVisibilityResource {
12+
13+
public static class Book {
14+
public String title; // hidden in the schema
15+
public String author;
16+
}
17+
18+
public static class Customer {
19+
public String name; // hidden in the schema
20+
public String address;
21+
}
22+
23+
public static class Purchase {
24+
public Book book;
25+
public Customer customer;
26+
public int count; // hidden in the schema
27+
}
28+
29+
@Query
30+
public Purchase someFirstQuery(Book book, Customer customer, Purchase purchase) {
31+
return purchase;
32+
}
33+
34+
@Query
35+
public Customer someSecondQuery() {
36+
return null;
37+
}
38+
39+
@Query
40+
public Book someThirdQuery() {
41+
return null;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.CoreMatchers.containsString;
5+
6+
import jakarta.ws.rs.core.MediaType;
7+
8+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
9+
import org.jboss.shrinkwrap.api.asset.StringAsset;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import io.quarkus.test.QuarkusUnitTest;
14+
15+
public class FieldVisibilityTest {
16+
17+
@RegisterExtension
18+
static QuarkusUnitTest test = new QuarkusUnitTest()
19+
.withApplicationRoot((jar) -> jar
20+
.addClasses(FieldVisibilityResource.Book.class, FieldVisibilityResource.Customer.class,
21+
FieldVisibilityResource.Purchase.class, FieldVisibilityResource.class)
22+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
23+
.addAsResource(
24+
new StringAsset(
25+
"quarkus.smallrye-graphql.field-visibility=Purchase.count,Customer.name,Book.*\\.title"),
26+
"application.properties"));
27+
28+
@Test
29+
void testSchemaWithInvisibleFields() {
30+
given()
31+
.when()
32+
.accept(MediaType.APPLICATION_JSON)
33+
.get("/graphql/schema.graphql")
34+
.then()
35+
.statusCode(200)
36+
.and().body(containsString("type Book {\n author: String\n}"))
37+
.and().body(containsString("input BookInput {\n author: String\n}"))
38+
.and().body(containsString("type Customer {\n address: String\n}"))
39+
.and().body(containsString("type Purchase {\n book: Book\n customer: Customer\n}"));
40+
}
41+
}

extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLConfig.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,6 @@ public class SmallRyeGraphQLConfig {
146146
@ConfigItem(defaultValue = "off")
147147
public LogPayloadOption logPayload;
148148

149-
/**
150-
* Set the Field visibility.
151-
*/
152-
@ConfigItem(defaultValue = "default")
153-
public String fieldVisibility;
154-
155149
/**
156150
* Exceptions that should be unwrapped (class names).
157151
*/

extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLRuntimeConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ public class SmallRyeGraphQLRuntimeConfig {
1313
@ConfigItem(name = "ui.enable", defaultValue = "true")
1414
boolean enable;
1515

16+
/**
17+
* Specifies the field visibility for the GraphQL schema.
18+
* This configuration item allows you to define comma-separated list of patterns (GraphQLType.GraphQLField).
19+
* These patterns are used to determine which fields should be excluded from the schema.
20+
* Special value {@code no-introspection} will disable introspection fields.
21+
* For more info see <a href="https://smallrye.io/smallrye-graphql/docs/schema/field-visibility">graphql-java
22+
* documentation</a>
23+
*/
24+
@ConfigItem(defaultValue = "default")
25+
public String fieldVisibility;
1626
}

0 commit comments

Comments
 (0)