forked from graphql-java-kickstart/graphql-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
231 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,12 @@ | |
package com.oembedler.moon.graphql.boot; | ||
|
||
import graphql.execution.ExecutionStrategy; | ||
import graphql.execution.SimpleExecutionStrategy; | ||
import graphql.execution.instrumentation.Instrumentation; | ||
import graphql.schema.GraphQLSchema; | ||
import graphql.servlet.GraphQLOperationListener; | ||
import graphql.servlet.DefaultExecutionStrategyProvider; | ||
import graphql.servlet.DefaultGraphQLSchemaProvider; | ||
import graphql.servlet.ExecutionStrategyProvider; | ||
import graphql.servlet.GraphQLSchemaProvider; | ||
import graphql.servlet.GraphQLServlet; | ||
import graphql.servlet.GraphQLServletListener; | ||
import graphql.servlet.SimpleGraphQLServlet; | ||
|
@@ -42,34 +44,37 @@ | |
import org.springframework.web.filter.CorsFilter; | ||
import org.springframework.web.servlet.DispatcherServlet; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistryWorkaround; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
import javax.servlet.Servlet; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">oEmbedler Inc.</a> | ||
*/ | ||
@Configuration | ||
@ConditionalOnWebApplication | ||
@ConditionalOnClass({Servlet.class, DispatcherServlet.class}) | ||
@ConditionalOnBean(GraphQLSchema.class) | ||
@ConditionalOnClass(DispatcherServlet.class) | ||
@ConditionalOnBean({GraphQLSchema.class, GraphQLSchemaProvider.class}) | ||
@ConditionalOnProperty(value = "graphql.servlet.enabled", havingValue = "true", matchIfMissing = true) | ||
@AutoConfigureAfter({GraphQLJavaToolsAutoConfiguration.class, SpringGraphQLCommonAutoConfiguration.class, WebMvcConfigurerAdapter.class}) | ||
@AutoConfigureAfter({GraphQLJavaToolsAutoConfiguration.class, SpringGraphQLCommonAutoConfiguration.class}) | ||
@EnableConfigurationProperties(GraphQLServletProperties.class) | ||
public class GraphQLWebAutoConfiguration { | ||
|
||
public static final String QUERY_EXECUTION_STRATEGY = "queryExecutionStrategy"; | ||
public static final String MUTATION_EXECUTION_STRATEGY = "mutationExecutionStrategy"; | ||
public static final String SUBSCRIPTION_EXECUTION_STRATEGY = "subscriptionExecutionStrategy"; | ||
|
||
@Autowired | ||
private GraphQLServletProperties graphQLServletProperties; | ||
|
||
@Autowired(required = false) | ||
private List<GraphQLOperationListener> operationListeners; | ||
private List<GraphQLServletListener> listeners; | ||
|
||
@Autowired(required = false) | ||
private List<GraphQLServletListener> servletListeners; | ||
private Instrumentation instrumentation; | ||
|
||
@Autowired(required = false) | ||
private Instrumentation instrumentation; | ||
private Map<String, ExecutionStrategy> executionStrategies; | ||
|
||
@Bean | ||
@ConditionalOnClass(CorsFilter.class) | ||
|
@@ -84,14 +89,47 @@ public CorsFilter corsConfigurer() { | |
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public ExecutionStrategy executionStrategy() { | ||
return new SimpleExecutionStrategy(); | ||
public GraphQLSchemaProvider graphQLSchemaProvider(GraphQLSchema schema) { | ||
return new DefaultGraphQLSchemaProvider(schema); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public ExecutionStrategyProvider executionStrategyProvider() { | ||
if(executionStrategies == null || executionStrategies.isEmpty()) { | ||
return new DefaultExecutionStrategyProvider(); | ||
} else if(executionStrategies.entrySet().size() == 1) { | ||
return new DefaultExecutionStrategyProvider(executionStrategies.entrySet().stream().findFirst().get().getValue()); | ||
} else { | ||
|
||
if(!executionStrategies.containsKey(QUERY_EXECUTION_STRATEGY)) { | ||
throwIncorrectExecutionStrategyNameException(); | ||
} | ||
|
||
if(executionStrategies.size() == 2 && !(executionStrategies.containsKey(MUTATION_EXECUTION_STRATEGY) || executionStrategies.containsKey(SUBSCRIPTION_EXECUTION_STRATEGY))) { | ||
throwIncorrectExecutionStrategyNameException(); | ||
} | ||
|
||
if(executionStrategies.size() >= 3 && !(executionStrategies.containsKey(MUTATION_EXECUTION_STRATEGY) && executionStrategies.containsKey(SUBSCRIPTION_EXECUTION_STRATEGY))) { | ||
throwIncorrectExecutionStrategyNameException(); | ||
} | ||
|
||
return new DefaultExecutionStrategyProvider( | ||
executionStrategies.get(QUERY_EXECUTION_STRATEGY), | ||
executionStrategies.get(MUTATION_EXECUTION_STRATEGY), | ||
executionStrategies.get(SUBSCRIPTION_EXECUTION_STRATEGY) | ||
); | ||
} | ||
} | ||
|
||
private void throwIncorrectExecutionStrategyNameException() { | ||
throw new IllegalStateException(String.format("When defining more than one execution strategy, they must be named %s, %s, or %s", QUERY_EXECUTION_STRATEGY, MUTATION_EXECUTION_STRATEGY, SUBSCRIPTION_EXECUTION_STRATEGY)); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public GraphQLServlet graphQLServlet(GraphQLSchema schema, ExecutionStrategy executionStrategy) { | ||
return new SimpleGraphQLServlet(schema, executionStrategy, operationListeners, servletListeners, instrumentation); | ||
public GraphQLServlet graphQLServlet(GraphQLSchemaProvider schemaProvider, ExecutionStrategyProvider executionStrategyProvider) { | ||
return new SimpleGraphQLServlet(schemaProvider, executionStrategyProvider, listeners, instrumentation); | ||
} | ||
|
||
@Bean | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ure/src/test/java/com/oembedler/moon/graphql/boot/test/AbstractAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.oembedler.moon.graphql.boot.test; | ||
|
||
import org.junit.After; | ||
import org.springframework.boot.test.util.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigRegistry; | ||
import org.springframework.context.support.AbstractApplicationContext; | ||
|
||
/** | ||
* @author Andrew Potter | ||
*/ | ||
public abstract class AbstractAutoConfigurationTest { | ||
|
||
private final Class<? extends AbstractApplicationContext> contextClass; | ||
private final Class<?> autoConfiguration; | ||
|
||
private AbstractApplicationContext context; | ||
|
||
protected AbstractAutoConfigurationTest(Class<?> autoConfiguration) { | ||
this(AnnotationConfigApplicationContext.class, autoConfiguration); | ||
} | ||
|
||
protected AbstractAutoConfigurationTest(Class<? extends AbstractApplicationContext> contextClass, Class<?> autoConfiguration) { | ||
assert AnnotationConfigRegistry.class.isAssignableFrom(contextClass); | ||
this.contextClass = contextClass; | ||
this.autoConfiguration = autoConfiguration; | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
if (this.context != null) { | ||
this.context.close(); | ||
this.context = null; | ||
} | ||
} | ||
|
||
protected void load(Class<?> config, String... environment) { | ||
try { | ||
this.context = contextClass.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (environment != null && environment.length > 0) { | ||
EnvironmentTestUtils.addEnvironment(getContext(), environment); | ||
} | ||
|
||
getRegistry().register(config); | ||
getRegistry().register(autoConfiguration); | ||
getContext().refresh(); | ||
} | ||
|
||
public AnnotationConfigRegistry getRegistry() { | ||
return (AnnotationConfigRegistry) context; | ||
} | ||
|
||
public AbstractApplicationContext getContext() { | ||
return context; | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
...toconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/AutoConfigurationTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.oembedler.moon.graphql.boot.test.graphqlJavaTools; | ||
|
||
import com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration; | ||
import com.oembedler.moon.graphql.boot.test.AutoConfigurationTest; | ||
import com.oembedler.moon.graphql.boot.test.AbstractAutoConfigurationTest; | ||
import graphql.schema.GraphQLSchema; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
@@ -11,7 +11,7 @@ | |
/** | ||
* @author <a href="mailto:[email protected]">oEmbedler Inc.</a> | ||
*/ | ||
public class GraphQLJavaToolsAutoConfigurationTest extends AutoConfigurationTest { | ||
public class GraphQLJavaToolsAutoConfigurationTest extends AbstractAutoConfigurationTest { | ||
|
||
public GraphQLJavaToolsAutoConfigurationTest() { | ||
super(GraphQLJavaToolsAutoConfiguration.class); | ||
|
@@ -26,6 +26,6 @@ static class BaseConfiguration { | |
public void appContextLoads() { | ||
load(BaseConfiguration.class); | ||
|
||
Assert.assertNotNull(this.context.getBean(GraphQLSchema.class)); | ||
Assert.assertNotNull(this.getContext().getBean(GraphQLSchema.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,21 @@ | |
|
||
import com.oembedler.moon.graphql.GraphQLSchemaBeanFactory; | ||
import com.oembedler.moon.graphql.boot.SpringGraphQLCommonAutoConfiguration; | ||
import com.oembedler.moon.graphql.boot.test.AutoConfigurationTest; | ||
import com.oembedler.moon.graphql.boot.test.AbstractAutoConfigurationTest; | ||
import com.oembedler.moon.graphql.engine.GraphQLSchemaBuilder; | ||
import com.oembedler.moon.graphql.engine.GraphQLSchemaConfig; | ||
import graphql.schema.GraphQLSchema; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.springframework.boot.test.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">oEmbedler Inc.</a> | ||
*/ | ||
public class SpringGraphQLCommonAutoConfigurationTest extends AutoConfigurationTest { | ||
@Ignore | ||
public class SpringGraphQLCommonAutoConfigurationTest extends AbstractAutoConfigurationTest { | ||
|
||
public SpringGraphQLCommonAutoConfigurationTest() { | ||
super(SpringGraphQLCommonAutoConfiguration.class); | ||
|
@@ -31,11 +30,11 @@ static class EmptyConfiguration { | |
@Test | ||
public void appContextLoads() { | ||
load(EmptyConfiguration.class); | ||
GraphQLSchemaBeanFactory graphQLSchemaBeanFactory = this.context.getBean(GraphQLSchemaBeanFactory.class); | ||
GraphQLSchemaConfig graphQLSchemaConfig = this.context.getBean(GraphQLSchemaConfig.class); | ||
GraphQLSchemaBuilder graphQLSchemaBuilder = this.context.getBean(GraphQLSchemaBuilder.class); | ||
GraphQLSchemaBeanFactory graphQLSchemaBeanFactory = this.getContext().getBean(GraphQLSchemaBeanFactory.class); | ||
GraphQLSchemaConfig graphQLSchemaConfig = this.getContext().getBean(GraphQLSchemaConfig.class); | ||
GraphQLSchemaBuilder graphQLSchemaBuilder = this.getContext().getBean(GraphQLSchemaBuilder.class); | ||
|
||
Assert.assertNotNull(this.context.getBean(GraphQLSchema.class)); | ||
Assert.assertNotNull(this.getContext().getBean(GraphQLSchema.class)); | ||
Assert.assertTrue(graphQLSchemaBeanFactory.containsBean(GraphQLSchemaBeanFactory.class)); | ||
Assert.assertEquals(graphQLSchemaBeanFactory, graphQLSchemaBeanFactory.getBeanByType(GraphQLSchemaBeanFactory.class)); | ||
Assert.assertEquals(graphQLSchemaBeanFactory, graphQLSchemaBuilder.getGraphQLSchemaBeanFactory()); | ||
|
Oops, something went wrong.