Skip to content

Commit

Permalink
Merge pull request quarkusio#44044 from mskacelik/issue-43903
Browse files Browse the repository at this point in the history
Changed quarkus.smallrye-graphql.field-visibility config property to be during ConfigPhase.RUN_TIME
  • Loading branch information
jmartisk authored Oct 30, 2024
2 parents d085393 + 3286452 commit 2bce110
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;

import static io.restassured.RestAssured.given;

import jakarta.ws.rs.core.MediaType;

import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class FieldVisibilityNoIntrospectionTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FieldVisibilityResource.Book.class, FieldVisibilityResource.Customer.class,
FieldVisibilityResource.Purchase.class, FieldVisibilityResource.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsResource(
new StringAsset("quarkus.smallrye-graphql.schema-include-introspection-types=true\n" +
"quarkus.smallrye-graphql.field-visibility=no-introspection"),
"application.properties"));

@Test
void testSchemaWithInvisibleFields() {
given()
.when()
.accept(MediaType.APPLICATION_JSON)
.get("/graphql/schema.graphql")
.then()
.statusCode(200)
.and().body(containsStringButNoFields("type __Directive"))
.and().body(containsStringButNoFields("type __EnumValue"))
.and().body(containsStringButNoFields("type __Field"))
.and().body(containsStringButNoFields("type __InputValue"))
.and().body(containsStringButNoFields("type __Schema"))
.and().body(containsStringButNoFields("type __Type"));
}

private org.hamcrest.Matcher<String> containsStringButNoFields(String s) {
return new org.hamcrest.BaseMatcher<String>() {
@Override
public boolean matches(Object item) {
return ((String) item).contains(s) && !((String) item).contains(s + " {");
}

@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("a string containing ").appendValue(s)
.appendText(" but not containing (without fields) ").appendValue(s);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

// It is important to note that some of the fields are invisible in the GraphQL schema,
// this is because they have been explicitly hidden via the
// quarkus.smallrye-graphql.field-visibility property.

@GraphQLApi
public class FieldVisibilityResource {

public static class Book {
public String title; // hidden in the schema
public String author;
}

public static class Customer {
public String name; // hidden in the schema
public String address;
}

public static class Purchase {
public Book book;
public Customer customer;
public int count; // hidden in the schema
}

@Query
public Purchase someFirstQuery(Book book, Customer customer, Purchase purchase) {
return purchase;
}

@Query
public Customer someSecondQuery() {
return null;
}

@Query
public Book someThirdQuery() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.smallrye.graphql.deployment.fieldvisibility;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;

import jakarta.ws.rs.core.MediaType;

import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class FieldVisibilityTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FieldVisibilityResource.Book.class, FieldVisibilityResource.Customer.class,
FieldVisibilityResource.Purchase.class, FieldVisibilityResource.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsResource(
new StringAsset(
"quarkus.smallrye-graphql.field-visibility=Purchase.count,Customer.name,Book.*\\.title"),
"application.properties"));

@Test
void testSchemaWithInvisibleFields() {
given()
.when()
.accept(MediaType.APPLICATION_JSON)
.get("/graphql/schema.graphql")
.then()
.statusCode(200)
.and().body(containsString("type Book {\n author: String\n}"))
.and().body(containsString("input BookInput {\n author: String\n}"))
.and().body(containsString("type Customer {\n address: String\n}"))
.and().body(containsString("type Purchase {\n book: Book\n customer: Customer\n}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ public class SmallRyeGraphQLConfig {
@ConfigItem(defaultValue = "off")
public LogPayloadOption logPayload;

/**
* Set the Field visibility.
*/
@ConfigItem(defaultValue = "default")
public String fieldVisibility;

/**
* Exceptions that should be unwrapped (class names).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ public class SmallRyeGraphQLRuntimeConfig {
@ConfigItem(name = "ui.enable", defaultValue = "true")
boolean enable;

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

0 comments on commit 2bce110

Please sign in to comment.