From f64d797a7ecb5761c545c0523df125f6246e2a0d Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Sun, 10 Mar 2019 21:37:19 -0700 Subject: [PATCH 1/4] fix: extract GraphQLShemaRegistration interface --- .../GraphQLSchemaAutoConfiguration.java | 7 ++++--- .../GraphQLShemaRegistration.java | 14 ++----------- .../GraphQLShemaRegistrationImpl.java | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistrationImpl.java diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java index 2842940b3..32b684a09 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java @@ -3,8 +3,6 @@ import java.util.ArrayList; import java.util.List; -import graphql.GraphQL; -import graphql.schema.GraphQLSchema; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -12,6 +10,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.util.CollectionUtils; +import graphql.GraphQL; +import graphql.schema.GraphQLSchema; + @Configuration @ConditionalOnClass(GraphQL.class) public class GraphQLSchemaAutoConfiguration { @@ -28,7 +29,7 @@ public void setGraphQLSchemaConfigurers(List configurer @Bean @ConditionalOnMissingBean(GraphQLSchema.class) public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() { - GraphQLShemaRegistration graphQLShemaRegistration = new GraphQLShemaRegistration(); + GraphQLShemaRegistrationImpl graphQLShemaRegistration = new GraphQLShemaRegistrationImpl(); for (GraphQLSchemaConfigurer configurer : graphQLSchemaConfigurers) { configurer.configure(graphQLShemaRegistration); diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistration.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistration.java index b91e17632..296734e59 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistration.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistration.java @@ -1,20 +1,10 @@ package com.introproventures.graphql.jpa.query.autoconfigure; -import java.util.LinkedHashSet; -import java.util.Set; - import graphql.schema.GraphQLSchema; -public class GraphQLShemaRegistration { - - Set managedGraphQLSchemas = new LinkedHashSet(); +public interface GraphQLShemaRegistration { - public void register(GraphQLSchema graphQLSchema) { - managedGraphQLSchemas.add(graphQLSchema); - } + public void register(GraphQLSchema graphQLSchema); - public GraphQLSchema[] getManagedGraphQLSchemas() { - return managedGraphQLSchemas.toArray(new GraphQLSchema[] {}); - } } diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistrationImpl.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistrationImpl.java new file mode 100644 index 000000000..a0a17f3b7 --- /dev/null +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistrationImpl.java @@ -0,0 +1,20 @@ +package com.introproventures.graphql.jpa.query.autoconfigure; + +import java.util.LinkedHashSet; +import java.util.Set; + +import graphql.schema.GraphQLSchema; + +public class GraphQLShemaRegistrationImpl implements GraphQLShemaRegistration { + + Set managedGraphQLSchemas = new LinkedHashSet(); + + public void register(GraphQLSchema graphQLSchema) { + managedGraphQLSchemas.add(graphQLSchema); + } + + public GraphQLSchema[] getManagedGraphQLSchemas() { + return managedGraphQLSchemas.toArray(new GraphQLSchema[] {}); + } + +} From 061e927bad2f665e220e53bd00a0374db32bf9dd Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Sun, 10 Mar 2019 22:21:21 -0700 Subject: [PATCH 2/4] fix: configuration properties are ignored when merging --- graphql-jpa-query-autoconfigure/pom.xml | 10 +++ .../GraphQLJpaQueryProperties.java | 11 ++- .../GraphQLSchemaAutoConfiguration.java | 10 ++- .../GraphQLSchemaFactoryBean.java | 28 +++++++- .../boot/autoconfigure/default.properties | 4 ++ .../GraphQLSchemaAutoConfigurationTest.java | 16 +++-- .../src/test/resources/application.yml | 14 ++++ .../autoconfigure/EnableGraphQLJpaQuery.java | 37 ---------- .../GraphQLJpaQueryAutoConfiguration.java | 39 +++-------- .../EnableGraphQLJpaQueryTest.java | 69 ------------------- .../GraphQLJpaQueryAutoConfigurationTest.java | 18 ++--- .../schema/impl/GraphQLJpaSchemaBuilder.java | 2 +- 12 files changed, 99 insertions(+), 159 deletions(-) rename {graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot => graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query}/autoconfigure/GraphQLJpaQueryProperties.java (83%) create mode 100644 graphql-jpa-query-autoconfigure/src/main/resources/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties create mode 100644 graphql-jpa-query-autoconfigure/src/test/resources/application.yml delete mode 100644 graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java delete mode 100644 graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQueryTest.java diff --git a/graphql-jpa-query-autoconfigure/pom.xml b/graphql-jpa-query-autoconfigure/pom.xml index 65878745d..c749bf258 100644 --- a/graphql-jpa-query-autoconfigure/pom.xml +++ b/graphql-jpa-query-autoconfigure/pom.xml @@ -19,7 +19,17 @@ org.springframework.boot spring-boot-autoconfigure + + + javax.validation + validation-api + + + org.hibernate.validator + hibernate-validator + test + \ No newline at end of file diff --git a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryProperties.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLJpaQueryProperties.java similarity index 83% rename from graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryProperties.java rename to graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLJpaQueryProperties.java index 4c3ee3a03..5383630ee 100644 --- a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryProperties.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLJpaQueryProperties.java @@ -13,13 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.introproventures.graphql.jpa.query.boot.autoconfigure; +package com.introproventures.graphql.jpa.query.autoconfigure; + +import javax.validation.constraints.NotEmpty; -import org.hibernate.validator.constraints.NotEmpty; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; import org.springframework.validation.annotation.Validated; @ConfigurationProperties(prefix="spring.graphql.jpa.query") +@PropertySources(value= { + @PropertySource("classpath:/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties"), + @PropertySource(value = "classpath:graphql-jpa-autoconfigure.properties", ignoreResourceNotFound = true) +}) @Validated public class GraphQLJpaQueryProperties { diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java index 32b684a09..15898b1d9 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.CollectionUtils; @@ -15,9 +16,13 @@ @Configuration @ConditionalOnClass(GraphQL.class) +@EnableConfigurationProperties(GraphQLJpaQueryProperties.class) public class GraphQLSchemaAutoConfiguration { private final List graphQLSchemaConfigurers = new ArrayList<>(); + + @Autowired + private GraphQLJpaQueryProperties properties; @Autowired(required = true) public void setGraphQLSchemaConfigurers(List configurers) { @@ -35,7 +40,10 @@ public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() { configurer.configure(graphQLShemaRegistration); } - return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas()); + return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas()) + .setName(properties.getName()) + .setDescription(properties.getDescription()); + }; diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java index 02499123b..99dda1d47 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java @@ -5,14 +5,21 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.springframework.beans.factory.config.AbstractFactoryBean; + import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; -import org.springframework.beans.factory.config.AbstractFactoryBean; public class GraphQLSchemaFactoryBean extends AbstractFactoryBean{ - private final GraphQLSchema[] managedGraphQLSchemas; + private static final String QUERY_NAME = "Query"; + private static final String QUERY_DESCRIPTION = ""; + + private final GraphQLSchema[] managedGraphQLSchemas; + + private String name = QUERY_NAME; + private String description = QUERY_DESCRIPTION; public GraphQLSchemaFactoryBean(GraphQLSchema[] managedGraphQLSchemas) { this.managedGraphQLSchemas = managedGraphQLSchemas; @@ -49,7 +56,10 @@ protected GraphQLSchema createInstance() throws Exception { schemaBuilder.mutation(GraphQLObjectType.newObject().name("Mutation").fields(mutations)); if(!queries.isEmpty()) - schemaBuilder.query(GraphQLObjectType.newObject().name("Query").fields(queries)); + schemaBuilder.query(GraphQLObjectType.newObject() + .name(this.name) + .description(this.description) + .fields(queries)); if(!subscriptions.isEmpty()) schemaBuilder.subscription(GraphQLObjectType.newObject().name("Subscription").fields(subscriptions)); @@ -62,4 +72,16 @@ public Class getObjectType() { return GraphQLSchema.class; } + public GraphQLSchemaFactoryBean setName(String name) { + this.name = name; + + return this; + } + + public GraphQLSchemaFactoryBean setDescription(String description) { + this.description = description; + + return this; + } + } diff --git a/graphql-jpa-query-autoconfigure/src/main/resources/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties b/graphql-jpa-query-autoconfigure/src/main/resources/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties new file mode 100644 index 000000000..8a9deba1d --- /dev/null +++ b/graphql-jpa-query-autoconfigure/src/main/resources/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties @@ -0,0 +1,4 @@ +spring.graphql.jpa.query.name=Query +spring.graphql.jpa.query.description= +spring.graphql.jpa.query.enabled=true +spring.graphql.jpa.query.path=/graphql \ No newline at end of file diff --git a/graphql-jpa-query-autoconfigure/src/test/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfigurationTest.java b/graphql-jpa-query-autoconfigure/src/test/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfigurationTest.java index 09129b729..e8cd94c6c 100644 --- a/graphql-jpa-query-autoconfigure/src/test/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfigurationTest.java +++ b/graphql-jpa-query-autoconfigure/src/test/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfigurationTest.java @@ -4,11 +4,6 @@ import java.util.Map; -import graphql.GraphQL; -import graphql.Scalars; -import graphql.schema.GraphQLFieldDefinition; -import graphql.schema.GraphQLObjectType; -import graphql.schema.GraphQLSchema; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +13,12 @@ import org.springframework.stereotype.Component; import org.springframework.test.context.junit4.SpringRunner; +import graphql.GraphQL; +import graphql.Scalars; +import graphql.schema.GraphQLFieldDefinition; +import graphql.schema.GraphQLObjectType; +import graphql.schema.GraphQLSchema; + @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.NONE) public class GraphQLSchemaAutoConfigurationTest { @@ -87,6 +88,11 @@ public void contextLoads() { // then assertThat(result.toString()).isEqualTo("{hello=world}"); assertThat(result2.toString()).isEqualTo("{greet=hello world}"); + + assertThat(graphQLSchema.getQueryType()) + .extracting(GraphQLObjectType::getName, GraphQLObjectType::getDescription) + .containsExactly("GraphQLBooks", "GraphQL Books Schema Description"); + } diff --git a/graphql-jpa-query-autoconfigure/src/test/resources/application.yml b/graphql-jpa-query-autoconfigure/src/test/resources/application.yml new file mode 100644 index 000000000..97b45f456 --- /dev/null +++ b/graphql-jpa-query-autoconfigure/src/test/resources/application.yml @@ -0,0 +1,14 @@ +spring: + jpa: + hibernate.ddl-auto: create-drop + show-sql: true + h2: + console.enabled: true + + graphql: + jpa: + query: + name: GraphQLBooks + description: GraphQL Books Schema Description + enabled: true + path: /graphql \ No newline at end of file diff --git a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java b/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java deleted file mode 100644 index 2e3ce63ce..000000000 --- a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.introproventures.graphql.jpa.query.boot.autoconfigure; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import com.introproventures.graphql.jpa.query.boot.autoconfigure.GraphQLJpaQueryAutoConfiguration.DefaultGraphQLJpaQueryConfiguration; -import com.introproventures.graphql.jpa.query.boot.autoconfigure.GraphQLJpaQueryAutoConfiguration.GraphQLJpaQuerySchemaConfigurer; -import org.springframework.context.annotation.Import; -import org.springframework.context.annotation.PropertySource; - -@Documented -@Retention( RUNTIME ) -@Target( TYPE ) -@Import({DefaultGraphQLJpaQueryConfiguration.class, GraphQLJpaQuerySchemaConfigurer.class}) -@PropertySource("classpath:/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties") -public @interface EnableGraphQLJpaQuery { - -} diff --git a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java b/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java index 5b63fe8fd..d4d755fb6 100644 --- a/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java +++ b/graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java @@ -17,30 +17,25 @@ import javax.persistence.EntityManager; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer; import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration; import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor; import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder; import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor; import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder; + import graphql.GraphQL; import graphql.schema.GraphQLSchema; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportAware; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.type.AnnotationMetadata; -import org.springframework.util.Assert; @Configuration -@PropertySource("classpath:/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties") @ConditionalOnClass(GraphQL.class) -@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=false) +@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true) public class GraphQLJpaQueryAutoConfiguration { @Configuration @@ -54,18 +49,13 @@ public GraphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder @Override public void configure(GraphQLShemaRegistration registry) { - registry.register(graphQLSchemaBuilder.build()); } } @Configuration - @EnableConfigurationProperties(GraphQLJpaQueryProperties.class) - public static class DefaultGraphQLJpaQueryConfiguration implements ImportAware { + public static class DefaultGraphQLJpaQueryConfiguration { - @Autowired - GraphQLJpaQueryProperties properties; - @Bean @ConditionalOnMissingBean(GraphQLExecutor.class) public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) { @@ -75,17 +65,8 @@ public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) { @Bean @ConditionalOnMissingBean(GraphQLSchemaBuilder.class) public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) { - Assert.notNull(properties.getName(), "GraphQL schema name cannot be null."); - Assert.notNull(properties.getDescription(), "GraphQL schema description cannot be null."); - - return new GraphQLJpaSchemaBuilder(entityManager) - .name(properties.getName()) - .description(properties.getDescription()); + return new GraphQLJpaSchemaBuilder(entityManager); } - @Override - public void setImportMetadata(AnnotationMetadata importMetadata) { - properties.setEnabled(true); - } } } diff --git a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQueryTest.java b/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQueryTest.java deleted file mode 100644 index 44d6d9079..000000000 --- a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQueryTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.introproventures.graphql.jpa.query.boot.autoconfigure; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.test.context.junit4.SpringRunner; - -import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor; -import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder; -import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor; -import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder; -import com.introproventures.graphql.jpa.query.starter.model.Author; - -@RunWith(SpringRunner.class) -@SpringBootTest( - properties={"spring.graphql.jpa.query.enabled=false"}, - webEnvironment = WebEnvironment.RANDOM_PORT -) -public class EnableGraphQLJpaQueryTest { - - @SpringBootApplication(exclude=GraphQLJpaQueryAutoConfiguration.class) - @EntityScan(basePackageClasses=Author.class) - @EnableGraphQLJpaQuery - static class Application { - } - - @Autowired - GraphQLJpaQueryProperties graphQLJpaQueryProperties; - - @Autowired - GraphQLExecutor graphQLExecutor; - - @Autowired - GraphQLSchemaBuilder graphQLSchemaBuilder; - - @Test - public void contextIsConfigured() { - assertThat(graphQLExecutor).isInstanceOf(GraphQLJpaExecutor.class); - assertThat(graphQLSchemaBuilder).isInstanceOf(GraphQLJpaSchemaBuilder.class); - - assertThat(graphQLJpaQueryProperties.getName()).isEqualTo("GraphQLBooks"); - assertThat(graphQLJpaQueryProperties.getDescription()).isEqualTo("GraphQL Books Schema Description"); - assertThat(graphQLJpaQueryProperties.getPath()).isEqualTo("/graphql"); - assertThat(graphQLJpaQueryProperties.isEnabled()).isEqualTo(true); - - } - -} diff --git a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java b/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java index 73b89d99c..7facc8c5a 100644 --- a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java +++ b/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java @@ -41,24 +41,18 @@ public class GraphQLJpaQueryAutoConfigurationTest { static class Application { } - @Autowired - GraphQLJpaQueryProperties graphQLJpaQueryProperties; - - @Autowired + @Autowired(required=false) GraphQLExecutor graphQLExecutor; - @Autowired + @Autowired(required=false) GraphQLSchemaBuilder graphQLSchemaBuilder; @Test public void contextIsAutoConfigured() { - assertThat(graphQLExecutor).isInstanceOf(GraphQLJpaExecutor.class); - assertThat(graphQLSchemaBuilder).isInstanceOf(GraphQLJpaSchemaBuilder.class); - - assertThat(graphQLJpaQueryProperties.getName()).isEqualTo("GraphQLBooks"); - assertThat(graphQLJpaQueryProperties.getDescription()).isEqualTo("GraphQL Books Schema Description"); - assertThat(graphQLJpaQueryProperties.getPath()).isEqualTo("/graphql"); - assertThat(graphQLJpaQueryProperties.isEnabled()).isEqualTo(true); + assertThat(graphQLExecutor).isNotNull() + .isInstanceOf(GraphQLJpaExecutor.class); + assertThat(graphQLSchemaBuilder).isNotNull() + .isInstanceOf(GraphQLJpaSchemaBuilder.class); } } \ No newline at end of file diff --git a/graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java b/graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java index 7a00249b9..72947aabf 100644 --- a/graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java +++ b/graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java @@ -106,7 +106,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder { private EntityManager entityManager; - private String name = "GraphQL JPA Schema"; + private String name = "GraphQLJPAQuery"; private String description = "GraphQL Schema for all entities in this JPA application"; From cf4eca6735f0d945c2462c05a75ee84ab285ceaa Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Sun, 10 Mar 2019 22:25:53 -0700 Subject: [PATCH 3/4] fix: add graphQLSchema query type configuration test --- .../GraphQLJpaQueryAutoConfigurationTest.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java b/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java index 7facc8c5a..189d47c3e 100644 --- a/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java +++ b/graphql-jpa-query-boot-starter/src/test/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfigurationTest.java @@ -32,6 +32,9 @@ import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder; import com.introproventures.graphql.jpa.query.starter.model.Author; +import graphql.schema.GraphQLObjectType; +import graphql.schema.GraphQLSchema; + @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class GraphQLJpaQueryAutoConfigurationTest { @@ -42,10 +45,13 @@ static class Application { } @Autowired(required=false) - GraphQLExecutor graphQLExecutor; + private GraphQLExecutor graphQLExecutor; @Autowired(required=false) - GraphQLSchemaBuilder graphQLSchemaBuilder; + private GraphQLSchemaBuilder graphQLSchemaBuilder; + + @Autowired + private GraphQLSchema graphQLSchema; @Test public void contextIsAutoConfigured() { @@ -54,5 +60,10 @@ public void contextIsAutoConfigured() { assertThat(graphQLSchemaBuilder).isNotNull() .isInstanceOf(GraphQLJpaSchemaBuilder.class); + + + assertThat(graphQLSchema.getQueryType()) + .extracting(GraphQLObjectType::getName, GraphQLObjectType::getDescription) + .containsExactly("GraphQLBooks", "GraphQL Books Schema Description"); } } \ No newline at end of file From fe03fc65d4aa17977df4a90ec1b739d0c028c3e9 Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Sun, 10 Mar 2019 22:33:24 -0700 Subject: [PATCH 4/4] feat: provide name configs for GraphQLSchemaFactoryBean query types --- .../GraphQLSchemaAutoConfiguration.java | 4 +- .../GraphQLSchemaFactoryBean.java | 62 ++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java index 15898b1d9..21c524178 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java @@ -41,8 +41,8 @@ public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() { } return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas()) - .setName(properties.getName()) - .setDescription(properties.getDescription()); + .setQueryName(properties.getName()) + .setQueryDescription(properties.getDescription()); }; diff --git a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java index 99dda1d47..92837b666 100644 --- a/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java +++ b/graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java @@ -15,12 +15,24 @@ public class GraphQLSchemaFactoryBean extends AbstractFactoryBean private static final String QUERY_NAME = "Query"; private static final String QUERY_DESCRIPTION = ""; + private static final String SUBSCRIPTION_NAME = "Subscription"; + private static final String SUBSCRIPTION_DESCRIPTION = ""; + private static final String MUTATION_NAME = "Mutation"; + private static final String MUTATION_DESCRIPTION = ""; + private final GraphQLSchema[] managedGraphQLSchemas; - private String name = QUERY_NAME; - private String description = QUERY_DESCRIPTION; + private String queryName = QUERY_NAME; + private String queryDescription = QUERY_DESCRIPTION; + private String subscriptionName = SUBSCRIPTION_NAME; + private String subscriptionDescription = SUBSCRIPTION_DESCRIPTION; + + private String mutationName = MUTATION_NAME; + private String mutationDescription = MUTATION_DESCRIPTION; + + public GraphQLSchemaFactoryBean(GraphQLSchema[] managedGraphQLSchemas) { this.managedGraphQLSchemas = managedGraphQLSchemas; } @@ -53,16 +65,22 @@ protected GraphQLSchema createInstance() throws Exception { .collect(Collectors.toList()); if(!mutations.isEmpty()) - schemaBuilder.mutation(GraphQLObjectType.newObject().name("Mutation").fields(mutations)); + schemaBuilder.mutation(GraphQLObjectType.newObject() + .name(this.mutationName) + .description(this.mutationDescription) + .fields(mutations)); if(!queries.isEmpty()) schemaBuilder.query(GraphQLObjectType.newObject() - .name(this.name) - .description(this.description) + .name(this.queryName) + .description(this.queryDescription) .fields(queries)); if(!subscriptions.isEmpty()) - schemaBuilder.subscription(GraphQLObjectType.newObject().name("Subscription").fields(subscriptions)); + schemaBuilder.subscription(GraphQLObjectType.newObject() + .name(this.subscriptionName) + .description(this.subscriptionDescription) + .fields(subscriptions)); return schemaBuilder.build(); } @@ -72,16 +90,40 @@ public Class getObjectType() { return GraphQLSchema.class; } - public GraphQLSchemaFactoryBean setName(String name) { - this.name = name; + public GraphQLSchemaFactoryBean setQueryName(String name) { + this.queryName = name; return this; } - public GraphQLSchemaFactoryBean setDescription(String description) { - this.description = description; + public GraphQLSchemaFactoryBean setQueryDescription(String description) { + this.queryDescription = description; return this; } + public GraphQLSchemaFactoryBean setSubscriptionName(String subscriptionName) { + this.subscriptionName = subscriptionName; + + return this; + } + + public GraphQLSchemaFactoryBean setSubscriptionDescription(String subscriptionDescription) { + this.subscriptionDescription = subscriptionDescription; + + return this; + } + + public GraphQLSchemaFactoryBean setMutationName(String mutationName) { + this.mutationName = mutationName; + + return this; + } + + public GraphQLSchemaFactoryBean setMutationDescription(String mutationDescription) { + this.mutationDescription = mutationDescription; + + return this; + } + }