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
55 changes: 55 additions & 0 deletions graphql-jpa-query-autoconfigure/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
25 changes: 25 additions & 0 deletions graphql-jpa-query-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-build</artifactId>
<version>0.3.12-SNAPSHOT</version>
<relativePath>../graphql-jpa-query-build</relativePath>
</parent>
<artifactId>graphql-jpa-query-autoconfigure</artifactId>

<dependencies>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.introproventures.graphql.jpa.query.autoconfigure;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

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

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

@Autowired(required = true)
public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
graphQLSchemaConfigurers.addAll(configurers);
}
}

@Bean
@ConditionalOnMissingBean(GraphQLSchema.class)
public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean() {
GraphQLShemaRegistration graphQLShemaRegistration = new GraphQLShemaRegistration();

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

return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas());

};


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.introproventures.graphql.jpa.query.autoconfigure;

public interface GraphQLSchemaConfigurer {

void configure(GraphQLShemaRegistration registry);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.introproventures.graphql.jpa.query.autoconfigure;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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;

public GraphQLSchemaFactoryBean(GraphQLSchema[] managedGraphQLSchemas) {
this.managedGraphQLSchemas = managedGraphQLSchemas;
}

@Override
protected GraphQLSchema createInstance() throws Exception {

GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();

List<GraphQLFieldDefinition> mutations = Stream.of(managedGraphQLSchemas)
.map(GraphQLSchema::getMutationType)
.filter(Objects::nonNull)
.map(GraphQLObjectType::getFieldDefinitions)
.flatMap(children -> children.stream())
.collect(Collectors.toList());

List<GraphQLFieldDefinition> queries = Stream.of(managedGraphQLSchemas)
.map(GraphQLSchema::getQueryType)
.filter(Objects::nonNull)
.filter(it -> !it.getName().equals("null")) // filter out null placeholders
.map(GraphQLObjectType::getFieldDefinitions)
.flatMap(children -> children.stream())
.collect(Collectors.toList());

List<GraphQLFieldDefinition> subscriptions = Stream.of(managedGraphQLSchemas)
.map(GraphQLSchema::getSubscriptionType)
.filter(Objects::nonNull)
.map(GraphQLObjectType::getFieldDefinitions)
.flatMap(children -> children.stream())
.collect(Collectors.toList());

if(!mutations.isEmpty())
schemaBuilder.mutation(GraphQLObjectType.newObject().name("Mutation").fields(mutations));

if(!queries.isEmpty())
schemaBuilder.query(GraphQLObjectType.newObject().name("Query").fields(queries));

if(!subscriptions.isEmpty())
schemaBuilder.subscription(GraphQLObjectType.newObject().name("Subscription").fields(subscriptions));

return schemaBuilder.build();
}

@Override
public Class<?> getObjectType() {
return GraphQLSchema.class;
}

}
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 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,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.introproventures.graphql.jpa.query.autoconfigure;

import static org.assertj.core.api.Assertions.assertThat;

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;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
public class GraphQLSchemaAutoConfigurationTest {

@Autowired
private GraphQLSchema graphQLSchema;

@SpringBootApplication
static class Application {

@Component
static class MutationGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer {

@Override
public void configure(GraphQLShemaRegistration registry) {
GraphQLObjectType mutation = GraphQLObjectType.newObject()
.name("mutation")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("greet")
.type(Scalars.GraphQLString)
.dataFetcher(environment -> {
return "hello world";
}))
.build();

GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
.query(GraphQLObjectType.newObject().name("null"))
.mutation(mutation)
.build();

registry.register(graphQLSchema);
}
}
@Component
static class QueryGraphQLSchemaConfigurer implements GraphQLSchemaConfigurer {

@Override
public void configure(GraphQLShemaRegistration registry) {
GraphQLObjectType query = GraphQLObjectType.newObject()
.name("query")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("hello")
.type(Scalars.GraphQLString)
.dataFetcher(environment -> {
return "world";
}))
.build();

GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
.query(query)
.build();

registry.register(graphQLSchema);
}
}
}

@Test
public void contextLoads() {
// given
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

// when
Map<String, Object> result = graphQL.execute("query {hello}").getData();
Map<String, Object> result2 = graphQL.execute("mutation {greet}").getData();

// then
assertThat(result.toString()).isEqualTo("{hello=world}");
assertThat(result2.toString()).isEqualTo("{greet=hello world}");
}



}
5 changes: 5 additions & 0 deletions graphql-jpa-query-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>graphql-jpa-query-schema</artifactId>
</dependency>

<dependency>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

import javax.persistence.EntityManager;

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 com.introproventures.graphql.jpa.query.web.GraphQLController;
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;
Expand All @@ -30,19 +39,27 @@
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;

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.web.GraphQLController;

import graphql.GraphQL;

@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)
public class GraphQLJpaQueryAutoConfiguration {

@Configuration
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {

private final GraphQLSchemaBuilder graphQLSchemaBuilder;

public GraphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {
this.graphQLSchemaBuilder = graphQLSchemaBuilder;
}

@Override
public void configure(GraphQLShemaRegistration registry) {

registry.register(graphQLSchemaBuilder.build());
}
}

@Configuration
@Import(GraphQLController.class)
Expand All @@ -54,8 +71,8 @@ public static class DefaultActivitiGraphQLJpaConfiguration implements ImportAwar

@Bean
@ConditionalOnMissingBean(GraphQLExecutor.class)
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) {
return new GraphQLJpaExecutor(graphQLSchemaBuilder.build());
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) {
return new GraphQLJpaExecutor(graphQLSchema);
}

@Bean
Expand Down
Loading