diff --git a/altair-spring-boot-autoconfigure/build.gradle b/altair-spring-boot-autoconfigure/build.gradle index 93a0ca6f..780c3f24 100644 --- a/altair-spring-boot-autoconfigure/build.gradle +++ b/altair-spring-boot-autoconfigure/build.gradle @@ -17,8 +17,9 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ dependencies{ + implementation(project(':graphql-kickstart-starter-utils')) annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" - + implementation "org.springframework.boot:spring-boot-autoconfigure" implementation "org.apache.commons:commons-text:$LIB_APACHE_COMMONS_TEXT" compileOnly "org.springframework.boot:spring-boot-starter-web" diff --git a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairController.java b/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairController.java index c4054bf8..3cb0280d 100644 --- a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairController.java +++ b/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairController.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import graphql.kickstart.util.PropertyGroupReader; +import graphql.kickstart.util.PropsLoader; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; @@ -55,11 +57,11 @@ private void loadTemplate() throws IOException { } private void loadProps() throws IOException { - props = new PropsLoader(environment).load(); + props = new PropsLoader(environment, "altair.props.resources.", "altair.props.values.").load(); } private void loadHeaders() throws JsonProcessingException { - PropertyGroupReader propertyReader = new PropertyGroupReader(environment, "graphiql.headers."); + PropertyGroupReader propertyReader = new PropertyGroupReader(environment, "altair.headers."); Properties headerProperties = propertyReader.load(); this.headers = new ObjectMapper().writeValueAsString(headerProperties); } @@ -86,7 +88,7 @@ private Map getReplacements(String graphqlEndpoint, replacements.put("pageTitle", altairProperties.getPageTitle()); replacements.put("pageFavicon", getResourceUrl("favicon.ico", "favicon.ico")); replacements.put("altairBaseUrl", getResourceUrl( - StringUtils.join(altairProperties.getSTATIC().getBasePath(), "/vendor/altair/"), + StringUtils.join(altairProperties.getBasePath(), "/vendor/altair/"), joinJsUnpkgPath(ALTAIR, altairProperties.getCdn().getVersion(), "build/dist/"))); replacements .put("altairLogoUrl", getResourceUrl("assets/img/logo_350.svg", "assets/img/logo_350.svg")); diff --git a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairProperties.java b/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairProperties.java index 085fda4b..8c5384de 100644 --- a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairProperties.java +++ b/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/AltairProperties.java @@ -9,10 +9,10 @@ public class AltairProperties { private boolean enabled = true; private Endpoint endpoint = new Endpoint(); - private Static STATIC = new Static(); private Cdn cdn = new Cdn(); private String pageTitle = "Altair"; private String mapping = "/altair"; + private String basePath = ""; @Data static class Endpoint { @@ -21,12 +21,6 @@ static class Endpoint { private String subscriptions = "/subscriptions"; } - @Data - static class Static { - - private String basePath = ""; - } - @Data static class Cdn { diff --git a/example-graphql-subscription/src/main/java/graphql/kickstart/spring/web/boot/resolvers/Query.java b/example-graphql-subscription/src/main/java/graphql/kickstart/spring/web/boot/resolvers/Query.java index 4a5a55cb..a94a2485 100644 --- a/example-graphql-subscription/src/main/java/graphql/kickstart/spring/web/boot/resolvers/Query.java +++ b/example-graphql-subscription/src/main/java/graphql/kickstart/spring/web/boot/resolvers/Query.java @@ -6,8 +6,10 @@ @Component class Query implements GraphQLQueryResolver { - public String hello() { - return "Hello world!"; + private static final String HELLO = "Hello world"; + + String hello() { + return HELLO; } } diff --git a/graphiql-spring-boot-autoconfigure/build.gradle b/graphiql-spring-boot-autoconfigure/build.gradle index 76c43043..e8aa6892 100644 --- a/graphiql-spring-boot-autoconfigure/build.gradle +++ b/graphiql-spring-boot-autoconfigure/build.gradle @@ -17,6 +17,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ dependencies{ + implementation(project(':graphql-kickstart-starter-utils')) annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" implementation "org.springframework.boot:spring-boot-autoconfigure" diff --git a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLController.java b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLController.java index b3b1a15c..d26277ec 100644 --- a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLController.java +++ b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLController.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import graphql.kickstart.util.PropertyGroupReader; +import graphql.kickstart.util.PropsLoader; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; @@ -53,7 +55,8 @@ private void loadTemplate() throws IOException { } private void loadProps() throws IOException { - props = new PropsLoader(environment).load(); + props = new PropsLoader(environment, "graphiql.props.resources.", "graphiql.props.variables.") + .load(); } private void loadHeaders() { @@ -71,7 +74,7 @@ public byte[] graphiql(String contextPath, @PathVariable Map par Map replacements = getReplacements( constructGraphQlEndpoint(contextPath, params), contextPath + graphiQLProperties.getEndpoint().getSubscriptions(), - contextPath + graphiQLProperties.getSTATIC().getBasePath() + contextPath + graphiQLProperties.getBasePath() ); String populatedTemplate = StringSubstitutor.replace(template, replacements); diff --git a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLProperties.java b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLProperties.java index 654f50e9..58f16e68 100644 --- a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLProperties.java +++ b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/GraphiQLProperties.java @@ -8,13 +8,13 @@ class GraphiQLProperties { private Endpoint endpoint = new Endpoint(); - private Static STATIC = new Static(); private CodeMirror codeMirror = new CodeMirror(); private Props props = new Props(); private String pageTitle = "GraphiQL"; private String mapping = "/graphiql"; private Subscriptions subscriptions = new Subscriptions(); private Cdn cdn = new Cdn(); + private String basePath = "/"; @Data static class Endpoint { @@ -23,12 +23,6 @@ static class Endpoint { private String subscriptions = "/subscriptions"; } - @Data - static class Static { - - private String basePath = "/"; - } - @Data static class CodeMirror { diff --git a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropertyGroupReader.java b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropertyGroupReader.java deleted file mode 100644 index b4ab1aa0..00000000 --- a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropertyGroupReader.java +++ /dev/null @@ -1,66 +0,0 @@ -package graphql.kickstart.graphiql.boot; - -import java.util.Arrays; -import java.util.Iterator; -import java.util.Objects; -import java.util.Optional; -import java.util.Properties; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.Environment; -import org.springframework.core.env.PropertySource; - -class PropertyGroupReader { - - private Environment environment; - private String prefix; - private Properties props; - - PropertyGroupReader(Environment environment, String prefix) { - this.environment = Objects.requireNonNull(environment); - this.prefix = Optional.ofNullable(prefix).orElse(""); - } - - Properties load() { - if (props == null) { - props = new Properties(); - loadProps(); - } - return props; - } - - private void loadProps() { - streamOfPropertySources().forEach(propertySource -> - Arrays.stream(propertySource.getPropertyNames()) - .filter(this::isWanted) - .forEach(key -> add(propertySource, key))); - } - - private Stream streamOfPropertySources() { - if (environment instanceof ConfigurableEnvironment) { - Iterator> iterator = ((ConfigurableEnvironment) environment) - .getPropertySources().iterator(); - Iterable> iterable = () -> iterator; - return StreamSupport.stream(iterable.spliterator(), false) - .filter(EnumerablePropertySource.class::isInstance) - .map(EnumerablePropertySource.class::cast); - } - return Stream.empty(); - } - - private String withoutPrefix(String key) { - return key.replace(prefix, ""); - } - - private boolean isWanted(String key) { - return key.startsWith(prefix); - } - - private Object add(EnumerablePropertySource propertySource, String key) { - return props.put(withoutPrefix(key), propertySource.getProperty(key)); - } - -} - diff --git a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropsLoader.java b/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropsLoader.java deleted file mode 100644 index f9e8ce70..00000000 --- a/graphiql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/graphiql/boot/PropsLoader.java +++ /dev/null @@ -1,54 +0,0 @@ -package graphql.kickstart.graphiql.boot; - -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import java.util.Properties; -import org.springframework.core.env.Environment; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.util.StreamUtils; - -class PropsLoader { - - private static final String GRAPHIQL_PROPS_PREFIX = "graphiql.props."; - private static final String GRAPHIQL_PROPS_RESOURCES_PREFIX = - GRAPHIQL_PROPS_PREFIX + "resources."; - private static final String GRAPHIQL_PROPS_VALUES_PREFIX = GRAPHIQL_PROPS_PREFIX + "variables."; - - private Environment environment; - - PropsLoader(Environment environment) { - this.environment = environment; - } - - String load() throws IOException { - PropertyGroupReader reader = new PropertyGroupReader(environment, GRAPHIQL_PROPS_VALUES_PREFIX); - Properties props = reader.load(); - - ObjectMapper objectMapper = new ObjectMapper(); - loadPropFromResource("defaultQuery").ifPresent(it -> props.put("defaultQuery", it)); - loadPropFromResource("query").ifPresent(it -> props.put("query", it)); - loadPropFromResource("variables").ifPresent(it -> props.put("variables", it)); - return objectMapper.writeValueAsString(props); - } - - private Optional loadPropFromResource(String prop) throws IOException { - String property = GRAPHIQL_PROPS_RESOURCES_PREFIX + prop; - if (environment.containsProperty(property)) { - String location = environment.getProperty(property); - Resource resource = new ClassPathResource(location); - return Optional.of(loadResource(resource)); - } - return Optional.empty(); - } - - private String loadResource(Resource resource) throws IOException { - try (InputStream inputStream = resource.getInputStream()) { - return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); - } - } - -} diff --git a/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolvers.java b/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolversTest.java similarity index 88% rename from graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolvers.java rename to graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolversTest.java index 0b70f053..03772fa9 100644 --- a/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolvers.java +++ b/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleQueryResolversTest.java @@ -9,11 +9,11 @@ import org.springframework.context.ApplicationContextException; @DisplayName("Test exception if multiple query resolvers are defined.") -public class GraphQLAnnotationsErrorMultipleQueryResolvers { +class GraphQLAnnotationsErrorMultipleQueryResolversTest { @Test @DisplayName("Assert that MultipleQueryResolversException is thrown when multiple query resolvers are found.") - public void testMultipleQueryResolversExceptionIsThrown() { + void testMultipleQueryResolversExceptionIsThrown() { // GIVEN final SpringApplication app = new SpringApplication(TestApplication.class); app.setAdditionalProfiles("test", "test-multiple-query-resolvers-exception"); diff --git a/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolvers.java b/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolversTest.java similarity index 88% rename from graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolvers.java rename to graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolversTest.java index a2db72e3..f82401f3 100644 --- a/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolvers.java +++ b/graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/test/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsErrorMultipleSubscriptionResolversTest.java @@ -9,11 +9,11 @@ import org.springframework.context.ApplicationContextException; @DisplayName("Test exception if multiple subscription resolvers are defined.") -public class GraphQLAnnotationsErrorMultipleSubscriptionResolvers { +class GraphQLAnnotationsErrorMultipleSubscriptionResolversTest { @Test @DisplayName("Assert that MultipleSubscriptionResolversException is thrown when multiple subscription resolvers are found.") - public void testMultipleSubscriptionResolversExceptionIsThrown() { + void testMultipleSubscriptionResolversExceptionIsThrown() { // GIVEN final SpringApplication app = new SpringApplication(TestApplication.class); app.setAdditionalProfiles("test", "test-multiple-subscription-resolvers-exception"); diff --git a/graphql-kickstart-starter-utils/build.gradle b/graphql-kickstart-starter-utils/build.gradle new file mode 100644 index 00000000..6b14728f --- /dev/null +++ b/graphql-kickstart-starter-utils/build.gradle @@ -0,0 +1,3 @@ +dependencies { + compileOnly "org.springframework.boot:spring-boot-starter-web" +} diff --git a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropertyGroupReader.java b/graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropertyGroupReader.java similarity index 89% rename from altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropertyGroupReader.java rename to graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropertyGroupReader.java index 0d194719..6c9431e8 100644 --- a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropertyGroupReader.java +++ b/graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropertyGroupReader.java @@ -1,4 +1,4 @@ -package graphql.kickstart.altair.boot; +package graphql.kickstart.util; import java.util.Arrays; import java.util.Iterator; @@ -12,18 +12,18 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; -class PropertyGroupReader { +public class PropertyGroupReader { private final Environment environment; private final String prefix; private Properties props; - PropertyGroupReader(Environment environment, String prefix) { + public PropertyGroupReader(Environment environment, String prefix) { this.environment = Objects.requireNonNull(environment); this.prefix = Optional.ofNullable(prefix).orElse(""); } - Properties load() { + public Properties load() { if (props == null) { props = new Properties(); loadProps(); @@ -46,7 +46,7 @@ private Stream> streamOfPropertySources() { Iterable> iterable = () -> iterator; return StreamSupport.stream(iterable.spliterator(), false) .filter(EnumerablePropertySource.class::isInstance) - .map(it -> (EnumerablePropertySource) it); + .map(EnumerablePropertySource.class::cast); } return Stream.empty(); } diff --git a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropsLoader.java b/graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropsLoader.java similarity index 73% rename from altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropsLoader.java rename to graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropsLoader.java index cf28ac6e..f093114d 100644 --- a/altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropsLoader.java +++ b/graphql-kickstart-starter-utils/src/main/java/graphql/kickstart/util/PropsLoader.java @@ -1,4 +1,4 @@ -package graphql.kickstart.altair.boot; +package graphql.kickstart.util; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @@ -12,20 +12,20 @@ import org.springframework.core.io.Resource; import org.springframework.util.StreamUtils; -class PropsLoader { - - private static final String ALTAIR_PROPS_PREFIX = "altair.props."; - private static final String ALTAIR_PROPS_RESOURCES_PREFIX = ALTAIR_PROPS_PREFIX + "resources."; - private static final String ALTAIR_PROPS_VALUES_PREFIX = ALTAIR_PROPS_PREFIX + "values."; +public class PropsLoader { private final Environment environment; + private final String resourcesPrefix; + private final String valuesPrefix; - PropsLoader(Environment environment) { + public PropsLoader(Environment environment, String resourcesPrefix, String valuesPrefix) { this.environment = environment; + this.resourcesPrefix = resourcesPrefix; + this.valuesPrefix = valuesPrefix; } - String load() throws IOException { - PropertyGroupReader reader = new PropertyGroupReader(environment, ALTAIR_PROPS_VALUES_PREFIX); + public String load() throws IOException { + PropertyGroupReader reader = new PropertyGroupReader(environment, valuesPrefix); Properties props = reader.load(); ObjectMapper objectMapper = new ObjectMapper(); @@ -36,7 +36,7 @@ String load() throws IOException { } private Optional loadPropFromResource(String prop) { - String property = ALTAIR_PROPS_RESOURCES_PREFIX + prop; + String property = resourcesPrefix + prop; return Optional.ofNullable(environment.getProperty(property)) .map(ClassPathResource::new) .map(this::loadResource); diff --git a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsJavaTypeTest.java b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsJavaTypeTest.java index 8799da1f..b252ff5a 100644 --- a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsJavaTypeTest.java +++ b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsJavaTypeTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; -public class GraphQLFieldAssertAsJavaTypeTest extends GraphQLFieldAssertTestBase { +class GraphQLFieldAssertAsJavaTypeTest extends GraphQLFieldAssertTestBase { private static final JavaType FOO_TYPE = TypeFactory.defaultInstance().constructType(Foo.class); diff --git a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsListTest.java b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsListTest.java index 5bb55b5c..69fdf9c8 100644 --- a/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsListTest.java +++ b/graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLFieldAssertAsListTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; -public class GraphQLFieldAssertAsListTest extends GraphQLFieldAssertTestBase { +class GraphQLFieldAssertAsListTest extends GraphQLFieldAssertTestBase { @Test @DisplayName("Should return a String list assertion (value at specific path is valid list).") diff --git a/settings.gradle b/settings.gradle index 3e81034f..9b81e8b9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -59,3 +59,4 @@ include ":example-webflux" include ':graphql-spring-boot-test' include ':graphql-spring-boot-test-autoconfigure' +include ':graphql-kickstart-starter-utils'