Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 67ed148

Browse files
authored
Merge pull request #161 from graphql-java-kickstart/feature/graphql-tools-5.4.0
Upgrade to graphql-tools 5.4 for type definition factory and directive
2 parents 0923ca1 + 3ec6820 commit 67ed148

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.module.kotlin.KotlinModule;
77
import graphql.schema.GraphQLScalarType;
88
import graphql.schema.GraphQLSchema;
9+
import graphql.schema.idl.SchemaDirectiveWiring;
910
import graphql.servlet.GraphQLSchemaProvider;
1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -41,6 +42,12 @@ public class GraphQLJavaToolsAutoConfiguration {
4142
@Autowired(required = false)
4243
private SchemaParserOptions options;
4344

45+
@Autowired(required = false)
46+
private List<SchemaDirective> directives;
47+
48+
@Autowired(required = false)
49+
private List<TypeDefinitionFactory> typeDefinitionFactories;
50+
4451
@Autowired
4552
private GraphQLToolsProperties props;
4653

@@ -67,15 +74,28 @@ public SchemaParser schemaParser(
6774
builder.scalars(scalars);
6875
}
6976

77+
// fixme: should we even support options directly like this? the combination with the builder makes it complex
7078
if (options != null) {
7179
builder.options(options);
72-
} else if (perFieldObjectMapperProvider != null) {
73-
final SchemaParserOptions.Builder optionsBuilder =
74-
newOptions().objectMapperProvider(perFieldObjectMapperProvider);
80+
} else {
81+
SchemaParserOptions.Builder optionsBuilder = SchemaParserOptions.newOptions();
82+
83+
if (perFieldObjectMapperProvider != null) {
84+
optionsBuilder.objectMapperProvider(perFieldObjectMapperProvider);
85+
}
7586
optionsBuilder.introspectionEnabled(props.isIntrospectionEnabled());
87+
88+
if (typeDefinitionFactories != null) {
89+
typeDefinitionFactories.forEach(optionsBuilder::typeDefinitionFactory);
90+
}
91+
7692
builder.options(optionsBuilder.build());
7793
}
7894

95+
if (directives != null) {
96+
directives.forEach(it -> builder.directive(it.getName(), it.getDirective()));
97+
}
98+
7999
return builder
80100
.resolvers(resolvers)
81101
.build();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.oembedler.moon.graphql.boot;
2+
3+
import graphql.schema.idl.SchemaDirectiveWiring;
4+
5+
public interface SchemaDirective {
6+
7+
String getName();
8+
9+
SchemaDirectiveWiring getDirective();
10+
11+
static SchemaDirective create(String name, SchemaDirectiveWiring directive) {
12+
return new SchemaDirectiveImpl(name, directive);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.oembedler.moon.graphql.boot;
2+
3+
import graphql.schema.idl.SchemaDirectiveWiring;
4+
5+
class SchemaDirectiveImpl implements SchemaDirective {
6+
7+
private final String name;
8+
private final SchemaDirectiveWiring directive;
9+
10+
SchemaDirectiveImpl(String name, SchemaDirectiveWiring directive) {
11+
this.name = name;
12+
this.directive = directive;
13+
}
14+
15+
@Override
16+
public String getName() {
17+
return name;
18+
}
19+
20+
@Override
21+
public SchemaDirectiveWiring getDirective() {
22+
return directive;
23+
}
24+
25+
}

graphql-spring-boot-autoconfigure/src/test/java/com/oembedler/moon/graphql/boot/test/graphqlJavaTools/GraphQLJavaToolsAutoConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ public void schemaWithInterfaceLoads() {
8585

8686
Assert.assertNotNull(this.getContext().getBean(GraphQLSchema.class));
8787
}
88+
89+
8890
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.oembedler.moon.graphql.boot.test.graphqlJavaTools;
2+
3+
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
4+
import com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration;
5+
import com.oembedler.moon.graphql.boot.SchemaDirective;
6+
import com.oembedler.moon.graphql.boot.test.AbstractAutoConfigurationTest;
7+
import graphql.schema.GraphQLObjectType;
8+
import graphql.schema.idl.SchemaDirectiveWiring;
9+
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;
10+
import graphql.schema.idl.SchemaParser;
11+
import org.junit.Test;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.stereotype.Component;
15+
16+
import static org.junit.Assert.assertNotNull;
17+
18+
public class GraphQLToolsDirectiveTest extends AbstractAutoConfigurationTest {
19+
20+
public GraphQLToolsDirectiveTest() {
21+
super(GraphQLJavaToolsAutoConfiguration.class);
22+
}
23+
24+
@Test
25+
public void directiveIsLoaded() {
26+
System.setProperty("graphql.tools.schemaLocationPattern", "graphql/schema-directive-test.graphql");
27+
load(BaseConfiguration.class);
28+
}
29+
30+
@Configuration
31+
static class BaseConfiguration {
32+
33+
@Component
34+
public class Query implements GraphQLQueryResolver {
35+
String schemaLocationTest(String id) {
36+
return id;
37+
}
38+
}
39+
40+
@Bean
41+
public SchemaDirective uppercaseDirective() {
42+
return SchemaDirective.create("uppercase", new SchemaDirectiveWiring() {
43+
@Override
44+
public GraphQLObjectType onObject(SchemaDirectiveWiringEnvironment<GraphQLObjectType> environment) {
45+
return null;
46+
}
47+
});
48+
}
49+
}
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
schemaLocationTest(id: ID!): String
3+
}

0 commit comments

Comments
 (0)