Skip to content

Commit f838056

Browse files
authored
fix: Configuration properties are ignored when merging bug (#88)
* fix: extract GraphQLShemaRegistration interface * fix: configuration properties are ignored when merging * fix: add graphQLSchema query type configuration test * feat: provide name configs for GraphQLSchemaFactoryBean query types
1 parent ee0aa6c commit f838056

File tree

14 files changed

+179
-175
lines changed

14 files changed

+179
-175
lines changed

graphql-jpa-query-autoconfigure/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
<groupId>org.springframework.boot</groupId>
2020
<artifactId>spring-boot-autoconfigure</artifactId>
2121
</dependency>
22+
23+
<dependency>
24+
<groupId>javax.validation</groupId>
25+
<artifactId>validation-api</artifactId>
26+
</dependency>
2227

28+
<dependency>
29+
<groupId>org.hibernate.validator</groupId>
30+
<artifactId>hibernate-validator</artifactId>
31+
<scope>test</scope>
32+
</dependency>
2333
</dependencies>
2434

2535
</project>
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.introproventures.graphql.jpa.query.boot.autoconfigure;
16+
package com.introproventures.graphql.jpa.query.autoconfigure;
17+
18+
import javax.validation.constraints.NotEmpty;
1719

18-
import org.hibernate.validator.constraints.NotEmpty;
1920
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
import org.springframework.context.annotation.PropertySource;
22+
import org.springframework.context.annotation.PropertySources;
2023
import org.springframework.validation.annotation.Validated;
2124

2225
@ConfigurationProperties(prefix="spring.graphql.jpa.query")
26+
@PropertySources(value= {
27+
@PropertySource("classpath:/com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties"),
28+
@PropertySource(value = "classpath:graphql-jpa-autoconfigure.properties", ignoreResourceNotFound = true)
29+
})
2330
@Validated
2431
public class GraphQLJpaQueryProperties {
2532

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import graphql.GraphQL;
7-
import graphql.schema.GraphQLSchema;
86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
108
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1110
import org.springframework.context.annotation.Bean;
1211
import org.springframework.context.annotation.Configuration;
1312
import org.springframework.util.CollectionUtils;
1413

14+
import graphql.GraphQL;
15+
import graphql.schema.GraphQLSchema;
16+
1517
@Configuration
1618
@ConditionalOnClass(GraphQL.class)
19+
@EnableConfigurationProperties(GraphQLJpaQueryProperties.class)
1720
public class GraphQLSchemaAutoConfiguration {
1821

1922
private final List<GraphQLSchemaConfigurer> graphQLSchemaConfigurers = new ArrayList<>();
23+
24+
@Autowired
25+
private GraphQLJpaQueryProperties properties;
2026

2127
@Autowired(required = true)
2228
public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurers) {
@@ -28,13 +34,16 @@ public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurer
2834
@Bean
2935
@ConditionalOnMissingBean(GraphQLSchema.class)
3036
public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() {
31-
GraphQLShemaRegistration graphQLShemaRegistration = new GraphQLShemaRegistration();
37+
GraphQLShemaRegistrationImpl graphQLShemaRegistration = new GraphQLShemaRegistrationImpl();
3238

3339
for (GraphQLSchemaConfigurer configurer : graphQLSchemaConfigurers) {
3440
configurer.configure(graphQLShemaRegistration);
3541
}
3642

37-
return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas());
43+
return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas())
44+
.setQueryName(properties.getName())
45+
.setQueryDescription(properties.getDescription());
46+
3847

3948
};
4049

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaFactoryBean.java

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@
55
import java.util.stream.Collectors;
66
import java.util.stream.Stream;
77

8+
import org.springframework.beans.factory.config.AbstractFactoryBean;
9+
810
import graphql.schema.GraphQLFieldDefinition;
911
import graphql.schema.GraphQLObjectType;
1012
import graphql.schema.GraphQLSchema;
11-
import org.springframework.beans.factory.config.AbstractFactoryBean;
1213

1314
public class GraphQLSchemaFactoryBean extends AbstractFactoryBean<GraphQLSchema>{
1415

15-
private final GraphQLSchema[] managedGraphQLSchemas;
16+
private static final String QUERY_NAME = "Query";
17+
private static final String QUERY_DESCRIPTION = "";
18+
private static final String SUBSCRIPTION_NAME = "Subscription";
19+
private static final String SUBSCRIPTION_DESCRIPTION = "";
20+
private static final String MUTATION_NAME = "Mutation";
21+
private static final String MUTATION_DESCRIPTION = "";
22+
23+
24+
private final GraphQLSchema[] managedGraphQLSchemas;
25+
26+
private String queryName = QUERY_NAME;
27+
private String queryDescription = QUERY_DESCRIPTION;
28+
29+
private String subscriptionName = SUBSCRIPTION_NAME;
30+
private String subscriptionDescription = SUBSCRIPTION_DESCRIPTION;
1631

32+
private String mutationName = MUTATION_NAME;
33+
private String mutationDescription = MUTATION_DESCRIPTION;
34+
35+
1736
public GraphQLSchemaFactoryBean(GraphQLSchema[] managedGraphQLSchemas) {
1837
this.managedGraphQLSchemas = managedGraphQLSchemas;
1938
}
@@ -46,13 +65,22 @@ protected GraphQLSchema createInstance() throws Exception {
4665
.collect(Collectors.toList());
4766

4867
if(!mutations.isEmpty())
49-
schemaBuilder.mutation(GraphQLObjectType.newObject().name("Mutation").fields(mutations));
68+
schemaBuilder.mutation(GraphQLObjectType.newObject()
69+
.name(this.mutationName)
70+
.description(this.mutationDescription)
71+
.fields(mutations));
5072

5173
if(!queries.isEmpty())
52-
schemaBuilder.query(GraphQLObjectType.newObject().name("Query").fields(queries));
74+
schemaBuilder.query(GraphQLObjectType.newObject()
75+
.name(this.queryName)
76+
.description(this.queryDescription)
77+
.fields(queries));
5378

5479
if(!subscriptions.isEmpty())
55-
schemaBuilder.subscription(GraphQLObjectType.newObject().name("Subscription").fields(subscriptions));
80+
schemaBuilder.subscription(GraphQLObjectType.newObject()
81+
.name(this.subscriptionName)
82+
.description(this.subscriptionDescription)
83+
.fields(subscriptions));
5684

5785
return schemaBuilder.build();
5886
}
@@ -62,4 +90,40 @@ public Class<?> getObjectType() {
6290
return GraphQLSchema.class;
6391
}
6492

93+
public GraphQLSchemaFactoryBean setQueryName(String name) {
94+
this.queryName = name;
95+
96+
return this;
97+
}
98+
99+
public GraphQLSchemaFactoryBean setQueryDescription(String description) {
100+
this.queryDescription = description;
101+
102+
return this;
103+
}
104+
105+
public GraphQLSchemaFactoryBean setSubscriptionName(String subscriptionName) {
106+
this.subscriptionName = subscriptionName;
107+
108+
return this;
109+
}
110+
111+
public GraphQLSchemaFactoryBean setSubscriptionDescription(String subscriptionDescription) {
112+
this.subscriptionDescription = subscriptionDescription;
113+
114+
return this;
115+
}
116+
117+
public GraphQLSchemaFactoryBean setMutationName(String mutationName) {
118+
this.mutationName = mutationName;
119+
120+
return this;
121+
}
122+
123+
public GraphQLSchemaFactoryBean setMutationDescription(String mutationDescription) {
124+
this.mutationDescription = mutationDescription;
125+
126+
return this;
127+
}
128+
65129
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
package com.introproventures.graphql.jpa.query.autoconfigure;
22

3-
import java.util.LinkedHashSet;
4-
import java.util.Set;
5-
63
import graphql.schema.GraphQLSchema;
74

8-
public class GraphQLShemaRegistration {
9-
10-
Set<GraphQLSchema> managedGraphQLSchemas = new LinkedHashSet<GraphQLSchema>();
5+
public interface GraphQLShemaRegistration {
116

12-
public void register(GraphQLSchema graphQLSchema) {
13-
managedGraphQLSchemas.add(graphQLSchema);
14-
}
7+
public void register(GraphQLSchema graphQLSchema);
158

16-
public GraphQLSchema[] getManagedGraphQLSchemas() {
17-
return managedGraphQLSchemas.toArray(new GraphQLSchema[] {});
18-
}
199

2010
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.introproventures.graphql.jpa.query.autoconfigure;
2+
3+
import java.util.LinkedHashSet;
4+
import java.util.Set;
5+
6+
import graphql.schema.GraphQLSchema;
7+
8+
public class GraphQLShemaRegistrationImpl implements GraphQLShemaRegistration {
9+
10+
Set<GraphQLSchema> managedGraphQLSchemas = new LinkedHashSet<GraphQLSchema>();
11+
12+
public void register(GraphQLSchema graphQLSchema) {
13+
managedGraphQLSchemas.add(graphQLSchema);
14+
}
15+
16+
public GraphQLSchema[] getManagedGraphQLSchemas() {
17+
return managedGraphQLSchemas.toArray(new GraphQLSchema[] {});
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.graphql.jpa.query.name=Query
2+
spring.graphql.jpa.query.description=
3+
spring.graphql.jpa.query.enabled=true
4+
spring.graphql.jpa.query.path=/graphql

graphql-jpa-query-autoconfigure/src/test/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfigurationTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
import java.util.Map;
66

7-
import graphql.GraphQL;
8-
import graphql.Scalars;
9-
import graphql.schema.GraphQLFieldDefinition;
10-
import graphql.schema.GraphQLObjectType;
11-
import graphql.schema.GraphQLSchema;
127
import org.junit.Test;
138
import org.junit.runner.RunWith;
149
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +13,12 @@
1813
import org.springframework.stereotype.Component;
1914
import org.springframework.test.context.junit4.SpringRunner;
2015

16+
import graphql.GraphQL;
17+
import graphql.Scalars;
18+
import graphql.schema.GraphQLFieldDefinition;
19+
import graphql.schema.GraphQLObjectType;
20+
import graphql.schema.GraphQLSchema;
21+
2122
@RunWith(SpringRunner.class)
2223
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
2324
public class GraphQLSchemaAutoConfigurationTest {
@@ -87,6 +88,11 @@ public void contextLoads() {
8788
// then
8889
assertThat(result.toString()).isEqualTo("{hello=world}");
8990
assertThat(result2.toString()).isEqualTo("{greet=hello world}");
91+
92+
assertThat(graphQLSchema.getQueryType())
93+
.extracting(GraphQLObjectType::getName, GraphQLObjectType::getDescription)
94+
.containsExactly("GraphQLBooks", "GraphQL Books Schema Description");
95+
9096
}
9197

9298

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spring:
2+
jpa:
3+
hibernate.ddl-auto: create-drop
4+
show-sql: true
5+
h2:
6+
console.enabled: true
7+
8+
graphql:
9+
jpa:
10+
query:
11+
name: GraphQLBooks
12+
description: GraphQL Books Schema Description
13+
enabled: true
14+
path: /graphql

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/EnableGraphQLJpaQuery.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)