diff --git a/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.java b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.java index c4363bc2..afa799a6 100644 --- a/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.java +++ b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.module.kotlin.KotlinModule; import graphql.schema.GraphQLScalarType; import graphql.schema.GraphQLSchema; +import graphql.schema.idl.SchemaDirectiveWiring; import graphql.servlet.GraphQLSchemaProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -41,6 +42,12 @@ public class GraphQLJavaToolsAutoConfiguration { @Autowired(required = false) private SchemaParserOptions options; + @Autowired(required = false) + private List directives; + + @Autowired(required = false) + private List typeDefinitionFactories; + @Autowired private GraphQLToolsProperties props; @@ -67,15 +74,28 @@ public SchemaParser schemaParser( builder.scalars(scalars); } + // fixme: should we even support options directly like this? the combination with the builder makes it complex if (options != null) { builder.options(options); - } else if (perFieldObjectMapperProvider != null) { - final SchemaParserOptions.Builder optionsBuilder = - newOptions().objectMapperProvider(perFieldObjectMapperProvider); + } else { + SchemaParserOptions.Builder optionsBuilder = SchemaParserOptions.newOptions(); + + if (perFieldObjectMapperProvider != null) { + optionsBuilder.objectMapperProvider(perFieldObjectMapperProvider); + } optionsBuilder.introspectionEnabled(props.isIntrospectionEnabled()); + + if (typeDefinitionFactories != null) { + typeDefinitionFactories.forEach(optionsBuilder::typeDefinitionFactory); + } + builder.options(optionsBuilder.build()); } + if (directives != null) { + directives.forEach(it -> builder.directive(it.getName(), it.getDirective())); + } + return builder .resolvers(resolvers) .build(); diff --git a/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirective.java b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirective.java new file mode 100644 index 00000000..ea227a97 --- /dev/null +++ b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirective.java @@ -0,0 +1,15 @@ +package com.oembedler.moon.graphql.boot; + +import graphql.schema.idl.SchemaDirectiveWiring; + +public interface SchemaDirective { + + String getName(); + + SchemaDirectiveWiring getDirective(); + + static SchemaDirective create(String name, SchemaDirectiveWiring directive) { + return new SchemaDirectiveImpl(name, directive); + } + +} diff --git a/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirectiveImpl.java b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirectiveImpl.java new file mode 100644 index 00000000..dca83b27 --- /dev/null +++ b/graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirectiveImpl.java @@ -0,0 +1,25 @@ +package com.oembedler.moon.graphql.boot; + +import graphql.schema.idl.SchemaDirectiveWiring; + +class SchemaDirectiveImpl implements SchemaDirective { + + private final String name; + private final SchemaDirectiveWiring directive; + + SchemaDirectiveImpl(String name, SchemaDirectiveWiring directive) { + this.name = name; + this.directive = directive; + } + + @Override + public String getName() { + return name; + } + + @Override + public SchemaDirectiveWiring getDirective() { + return directive; + } + +} diff --git a/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLJavaToolsAutoConfigurationTest.java b/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLJavaToolsAutoConfigurationTest.java index 9ce49cc5..c62cb6b6 100644 --- a/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLJavaToolsAutoConfigurationTest.java +++ b/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLJavaToolsAutoConfigurationTest.java @@ -85,4 +85,6 @@ public void schemaWithInterfaceLoads() { Assert.assertNotNull(this.getContext().getBean(GraphQLSchema.class)); } + + } diff --git a/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLToolsDirectiveTest.java b/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLToolsDirectiveTest.java new file mode 100644 index 00000000..680761fb --- /dev/null +++ b/graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLToolsDirectiveTest.java @@ -0,0 +1,50 @@ +package com.oembedler.moon.graphql.boot.test.graphqlJavaTools; + +import com.coxautodev.graphql.tools.GraphQLQueryResolver; +import com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration; +import com.oembedler.moon.graphql.boot.SchemaDirective; +import com.oembedler.moon.graphql.boot.test.AbstractAutoConfigurationTest; +import graphql.schema.GraphQLObjectType; +import graphql.schema.idl.SchemaDirectiveWiring; +import graphql.schema.idl.SchemaDirectiveWiringEnvironment; +import graphql.schema.idl.SchemaParser; +import org.junit.Test; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import static org.junit.Assert.assertNotNull; + +public class GraphQLToolsDirectiveTest extends AbstractAutoConfigurationTest { + + public GraphQLToolsDirectiveTest() { + super(GraphQLJavaToolsAutoConfiguration.class); + } + + @Test + public void directiveIsLoaded() { + System.setProperty("graphql.tools.schemaLocationPattern", "graphql/schema-directive-test.graphql"); + load(BaseConfiguration.class); + } + + @Configuration + static class BaseConfiguration { + + @Component + public class Query implements GraphQLQueryResolver { + String schemaLocationTest(String id) { + return id; + } + } + + @Bean + public SchemaDirective uppercaseDirective() { + return SchemaDirective.create("uppercase", new SchemaDirectiveWiring() { + @Override + public GraphQLObjectType onObject(SchemaDirectiveWiringEnvironment environment) { + return null; + } + }); + } + } +} diff --git a/graphql-spring-boot-autoconfigure/src/test/resources/graphql/schema-directive-test.graphql b/graphql-spring-boot-autoconfigure/src/test/resources/graphql/schema-directive-test.graphql new file mode 100644 index 00000000..d96cd734 --- /dev/null +++ b/graphql-spring-boot-autoconfigure/src/test/resources/graphql/schema-directive-test.graphql @@ -0,0 +1,3 @@ +type Query { + schemaLocationTest(id: ID!): String +}