Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions graphql-jpa-query-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
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;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import graphql.GraphQL;
import graphql.schema.GraphQLSchema;

@Configuration
@ConditionalOnClass(GraphQL.class)
@EnableConfigurationProperties(GraphQLJpaQueryProperties.class)
public class GraphQLSchemaAutoConfiguration {

private final List<GraphQLSchemaConfigurer> graphQLSchemaConfigurers = new ArrayList<>();

@Autowired
private GraphQLJpaQueryProperties properties;

@Autowired(required = true)
public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurers) {
Expand All @@ -28,13 +34,16 @@ public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurer
@Bean
@ConditionalOnMissingBean(GraphQLSchema.class)
public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() {
GraphQLShemaRegistration graphQLShemaRegistration = new GraphQLShemaRegistration();
GraphQLShemaRegistrationImpl graphQLShemaRegistration = new GraphQLShemaRegistrationImpl();

for (GraphQLSchemaConfigurer configurer : graphQLSchemaConfigurers) {
configurer.configure(graphQLShemaRegistration);
}

return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas());
return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas())
.setQueryName(properties.getName())
.setQueryDescription(properties.getDescription());


};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,34 @@
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<GraphQLSchema>{

private final GraphQLSchema[] managedGraphQLSchemas;
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 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;
}
Expand Down Expand Up @@ -46,13 +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("Query").fields(queries));
schemaBuilder.query(GraphQLObjectType.newObject()
.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();
}
Expand All @@ -62,4 +90,40 @@ public Class<?> getObjectType() {
return GraphQLSchema.class;
}

public GraphQLSchemaFactoryBean setQueryName(String name) {
this.queryName = name;

return this;
}

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;
}

}
Original file line number Diff line number Diff line change
@@ -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<GraphQLSchema> managedGraphQLSchemas = new LinkedHashSet<GraphQLSchema>();
public interface GraphQLShemaRegistration {

public void register(GraphQLSchema graphQLSchema) {
managedGraphQLSchemas.add(graphQLSchema);
}
public void register(GraphQLSchema graphQLSchema);

public GraphQLSchema[] getManagedGraphQLSchemas() {
return managedGraphQLSchemas.toArray(new GraphQLSchema[] {});
}

}
Original file line number Diff line number Diff line change
@@ -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<GraphQLSchema> managedGraphQLSchemas = new LinkedHashSet<GraphQLSchema>();

public void register(GraphQLSchema graphQLSchema) {
managedGraphQLSchemas.add(graphQLSchema);
}

public GraphQLSchema[] getManagedGraphQLSchemas() {
return managedGraphQLSchemas.toArray(new GraphQLSchema[] {});
}

}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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");

}


Expand Down
14 changes: 14 additions & 0 deletions graphql-jpa-query-autoconfigure/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

Loading