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

Commit 8ee1479

Browse files
authored
Merge pull request #301 from graphql-java-kickstart/feature/separate-graphql-tools-starter
Separate graphql tools starter
2 parents 32db152 + 3ca0361 commit 8ee1479

File tree

81 files changed

+1929
-1088
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1929
-1088
lines changed

example-graphql-tools/src/main/java/com/graphql/sample/boot/GraphQLToolsSampleApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.graphql.sample.boot;
22

33
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
4-
import graphql.servlet.config.ObjectMapperConfigurer;
4+
import graphql.kickstart.execution.config.ObjectMapperConfigurer;
55
import org.springframework.boot.SpringApplication;
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
77
import org.springframework.context.annotation.Bean;
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
package graphql.servlet.examples.dataloader.requestscope;
22

3-
import graphql.servlet.context.DefaultGraphQLContext;
3+
import graphql.kickstart.execution.context.DefaultGraphQLContext;
4+
import graphql.kickstart.execution.context.GraphQLContext;
45
import graphql.servlet.context.DefaultGraphQLServletContext;
56
import graphql.servlet.context.DefaultGraphQLWebSocketContext;
6-
import graphql.servlet.context.GraphQLContext;
7-
import graphql.servlet.context.GraphQLContextBuilder;
8-
import org.dataloader.DataLoader;
9-
import org.dataloader.DataLoaderRegistry;
10-
import org.springframework.stereotype.Component;
11-
7+
import graphql.servlet.context.GraphQLServletContextBuilder;
8+
import java.util.concurrent.CompletableFuture;
129
import javax.servlet.http.HttpServletRequest;
1310
import javax.servlet.http.HttpServletResponse;
1411
import javax.websocket.Session;
1512
import javax.websocket.server.HandshakeRequest;
16-
import java.util.concurrent.CompletableFuture;
13+
import org.dataloader.DataLoader;
14+
import org.dataloader.DataLoaderRegistry;
15+
import org.springframework.stereotype.Component;
1716

1817
@Component
19-
public class CustomGraphQLContextBuilder implements GraphQLContextBuilder {
20-
21-
private final CustomerRepository customerRepository;
22-
23-
public CustomGraphQLContextBuilder(CustomerRepository customerRepository) {
24-
this.customerRepository = customerRepository;
25-
}
26-
27-
@Override
28-
public GraphQLContext build(HttpServletRequest req, HttpServletResponse response) {
29-
return DefaultGraphQLServletContext.createServletContext(buildDataLoaderRegistry(), null).with(req).with(response).build();
30-
}
31-
32-
@Override
33-
public GraphQLContext build() {
34-
return new DefaultGraphQLContext(buildDataLoaderRegistry(), null);
35-
}
36-
37-
@Override
38-
public GraphQLContext build(Session session, HandshakeRequest request) {
39-
return DefaultGraphQLWebSocketContext.createWebSocketContext(buildDataLoaderRegistry(), null).with(session).with(request).build();
40-
}
41-
42-
private DataLoaderRegistry buildDataLoaderRegistry() {
43-
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
44-
dataLoaderRegistry.register("customerDataLoader",
45-
new DataLoader<Integer, String>(customerIds ->
46-
CompletableFuture.supplyAsync(() ->
47-
customerRepository.getUserNamesForIds(customerIds))));
48-
return dataLoaderRegistry;
49-
}
18+
public class CustomGraphQLContextBuilder implements GraphQLServletContextBuilder {
19+
20+
private final CustomerRepository customerRepository;
21+
22+
public CustomGraphQLContextBuilder(CustomerRepository customerRepository) {
23+
this.customerRepository = customerRepository;
24+
}
25+
26+
@Override
27+
public GraphQLContext build(HttpServletRequest req, HttpServletResponse response) {
28+
return DefaultGraphQLServletContext.createServletContext(buildDataLoaderRegistry(), null).with(req).with(response)
29+
.build();
30+
}
31+
32+
@Override
33+
public GraphQLContext build() {
34+
return new DefaultGraphQLContext(buildDataLoaderRegistry(), null);
35+
}
36+
37+
@Override
38+
public GraphQLContext build(Session session, HandshakeRequest request) {
39+
return DefaultGraphQLWebSocketContext.createWebSocketContext(buildDataLoaderRegistry(), null).with(session)
40+
.with(request).build();
41+
}
42+
43+
private DataLoaderRegistry buildDataLoaderRegistry() {
44+
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
45+
dataLoaderRegistry.register("customerDataLoader",
46+
new DataLoader<Integer, String>(customerIds ->
47+
CompletableFuture.supplyAsync(() ->
48+
customerRepository.getUserNamesForIds(customerIds))));
49+
return dataLoaderRegistry;
50+
}
5051
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package graphql.servlet.examples.dataloader.requestscope;
22

33
import com.coxautodev.graphql.tools.GraphQLResolver;
4+
import graphql.kickstart.execution.context.GraphQLContext;
45
import graphql.schema.DataFetchingEnvironment;
5-
import graphql.servlet.context.GraphQLContext;
6+
import java.util.concurrent.CompletableFuture;
67
import org.dataloader.DataLoader;
78
import org.springframework.stereotype.Component;
89

9-
import java.util.concurrent.CompletableFuture;
10-
1110
@Component
1211
public class CustomerResolver implements GraphQLResolver<Customer> {
1312

14-
public CompletableFuture<String> getName(Customer customer, DataFetchingEnvironment dfe) {
15-
final DataLoader<Integer, String> dataloader = ((GraphQLContext) dfe.getContext())
16-
.getDataLoaderRegistry().get()
17-
.getDataLoader("customerDataLoader");
13+
public CompletableFuture<String> getName(Customer customer, DataFetchingEnvironment dfe) {
14+
final DataLoader<Integer, String> dataloader = ((GraphQLContext) dfe.getContext())
15+
.getDataLoaderRegistry().get()
16+
.getDataLoader("customerDataLoader");
17+
18+
return dataloader.load(customer.getCustomerId());
19+
}
1820

19-
return dataloader.load(customer.getCustomerId());
20-
}
2121
}

gradle.properties

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
#
1919

20-
version = 5.11.1
20+
version = 6.0.0
2121

2222
PROJECT_GROUP = com.graphql-java-kickstart
2323
PROJECT_NAME = graphql-spring-boot
@@ -39,13 +39,11 @@ TARGET_COMPATIBILITY = 1.8
3939

4040
LIB_GRAPHQL_JAVA_VER = 13.0
4141
LIB_JUNIT_VER = 4.12
42-
LIB_SPRING_CORE_VER = 5.0.4.RELEASE
43-
LIB_SPRING_BOOT_VER = 2.1.6.RELEASE
44-
LIB_GRAPHQL_SERVLET_VER = 8.0.0
42+
LIB_SPRING_CORE_VER = 5.2.1.RELEASE
43+
LIB_SPRING_BOOT_VER = 2.2.1.RELEASE
44+
LIB_GRAPHQL_SERVLET_VER = 9.0.0
4545
LIB_GRAPHQL_JAVA_TOOLS_VER = 5.7.1
4646
LIB_COMMONS_IO_VER = 2.6
47-
LIB_TRANSACTIONS_API_VERSION=1.3
48-
LIB_INTERCEPTOR_API_VERSION=1.2.2
4947
kotlin.version=1.3.31
5048

5149
GRADLE_WRAPPER_VER = 4.10.3
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
dependencies {
2+
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor:$LIB_SPRING_BOOT_VER"
3+
compileOnly "org.springframework.boot:spring-boot-configuration-processor:$LIB_SPRING_BOOT_VER"
4+
5+
compile "com.graphql-java-kickstart:graphql-java-tools:$LIB_GRAPHQL_JAVA_TOOLS_VER"
6+
compile "org.springframework.boot:spring-boot-autoconfigure:$LIB_SPRING_BOOT_VER"
7+
8+
compileOnly "com.graphql-java-kickstart:graphql-java-servlet:$LIB_GRAPHQL_SERVLET_VER"
9+
10+
testCompile "com.graphql-java-kickstart:graphql-java-servlet:$LIB_GRAPHQL_SERVLET_VER"
11+
testCompile "com.graphql-java:graphql-java:$LIB_GRAPHQL_JAVA_VER"
12+
testCompile "org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER"
13+
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"
14+
testCompile(project(":graphql-spring-boot-test"))
15+
}
16+
17+
compileJava.dependsOn(processResources)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graphql.kickstart.tools.boot;
2+
3+
import static java.util.stream.Collectors.joining;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.stream.Collectors;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.context.ApplicationContext;
15+
import org.springframework.core.io.Resource;
16+
17+
public class ClasspathResourceSchemaStringProvider implements SchemaStringProvider {
18+
19+
@Autowired
20+
private ApplicationContext applicationContext;
21+
private String schemaLocationPattern;
22+
23+
public ClasspathResourceSchemaStringProvider(String schemaLocationPattern) {
24+
this.schemaLocationPattern = schemaLocationPattern;
25+
}
26+
27+
@Override
28+
public List<String> schemaStrings() throws IOException {
29+
Resource[] resources = applicationContext.getResources("classpath*:" + schemaLocationPattern);
30+
if (resources.length <= 0) {
31+
throw new IllegalStateException(
32+
"No graphql schema files found on classpath with location pattern '"
33+
+ schemaLocationPattern
34+
+ "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context.");
35+
}
36+
37+
return Arrays.stream(resources)
38+
.map(this::readSchema)
39+
.collect(Collectors.toList());
40+
}
41+
42+
private String readSchema(Resource resource) {
43+
try (
44+
InputStream inputStream = resource.getInputStream();
45+
InputStreamReader bufferedInputStream = new InputStreamReader(inputStream, StandardCharsets.UTF_8.name());
46+
BufferedReader reader = new BufferedReader(bufferedInputStream)
47+
) {
48+
return reader.lines().collect(joining("\n"));
49+
} catch (IOException e) {
50+
throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e);
51+
}
52+
}
53+
54+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.oembedler.moon.graphql.boot;
1+
package graphql.kickstart.tools.boot;
22

33
import com.coxautodev.graphql.tools.GraphQLResolver;
44
import com.coxautodev.graphql.tools.PerFieldObjectMapperProvider;
@@ -10,12 +10,13 @@
1010
import com.fasterxml.jackson.databind.ObjectMapper;
1111
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
1212
import com.fasterxml.jackson.module.kotlin.KotlinModule;
13+
import graphql.kickstart.execution.config.GraphQLSchemaProvider;
1314
import graphql.schema.GraphQLScalarType;
1415
import graphql.schema.GraphQLSchema;
1516
import graphql.schema.idl.SchemaDirectiveWiring;
16-
import graphql.servlet.config.GraphQLSchemaProvider;
1717
import java.io.IOException;
1818
import java.util.List;
19+
import lombok.extern.slf4j.Slf4j;
1920
import org.springframework.beans.factory.annotation.Autowired;
2021
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -31,6 +32,7 @@
3132
/**
3233
* @author Andrew Potter
3334
*/
35+
@Slf4j
3436
@Configuration
3537
@ConditionalOnClass(SchemaParser.class)
3638
@AutoConfigureAfter({JacksonAutoConfiguration.class})

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/GraphQLToolsProperties.java renamed to graphql-kickstart-spring-boot-autoconfigure-tools/src/main/java/graphql/kickstart/tools/boot/GraphQLToolsProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.oembedler.moon.graphql.boot;
1+
package graphql.kickstart.tools.boot;
22

33
import lombok.Data;
44
import org.springframework.boot.context.properties.ConfigurationProperties;

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/ListSchemaStringProvider.java renamed to graphql-kickstart-spring-boot-autoconfigure-tools/src/main/java/graphql/kickstart/tools/boot/ListSchemaStringProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.oembedler.moon.graphql.boot;
1+
package graphql.kickstart.tools.boot;
22

33
import java.io.IOException;
44
import java.util.ArrayList;

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaDirective.java renamed to graphql-kickstart-spring-boot-autoconfigure-tools/src/main/java/graphql/kickstart/tools/boot/SchemaDirective.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.oembedler.moon.graphql.boot;
1+
package graphql.kickstart.tools.boot;
22

33
import graphql.schema.idl.SchemaDirectiveWiring;
44

graphql-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/graphql/boot/SchemaStringProvider.java renamed to graphql-kickstart-spring-boot-autoconfigure-tools/src/main/java/graphql/kickstart/tools/boot/SchemaStringProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.oembedler.moon.graphql.boot;
1+
package graphql.kickstart.tools.boot;
22

33
import java.io.IOException;
44
import java.util.List;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package graphql.kickstart.tools.boot;
2+
3+
import static org.mockito.Mockito.mock;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.websocket.server.ServerContainer;
7+
import org.junit.After;
8+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
9+
import org.springframework.boot.test.util.TestPropertyValues;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.springframework.context.annotation.AnnotationConfigRegistry;
12+
import org.springframework.context.support.AbstractApplicationContext;
13+
import org.springframework.mock.web.MockServletContext;
14+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
15+
16+
/**
17+
* @author Andrew Potter
18+
*/
19+
public abstract class AbstractAutoConfigurationTest {
20+
21+
private final Class<? extends AbstractApplicationContext> contextClass;
22+
private final Class<?> autoConfiguration;
23+
24+
private AbstractApplicationContext context;
25+
26+
protected AbstractAutoConfigurationTest(Class<?> autoConfiguration) {
27+
this(AnnotationConfigApplicationContext.class, autoConfiguration);
28+
}
29+
30+
protected AbstractAutoConfigurationTest(Class<? extends AbstractApplicationContext> contextClass,
31+
Class<?> autoConfiguration) {
32+
assert AnnotationConfigRegistry.class.isAssignableFrom(contextClass);
33+
this.contextClass = contextClass;
34+
this.autoConfiguration = autoConfiguration;
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
if (this.context != null) {
40+
this.context.close();
41+
this.context = null;
42+
}
43+
}
44+
45+
protected void load(Class<?> config, String... environment) {
46+
try {
47+
this.context = contextClass.newInstance();
48+
} catch (InstantiationException | IllegalAccessException e) {
49+
throw new RuntimeException("Failed to instantiate testing context", e);
50+
}
51+
52+
if (environment != null && environment.length > 0) {
53+
TestPropertyValues.of(environment).applyTo(context);
54+
}
55+
56+
getRegistry().register(config);
57+
getRegistry().register(autoConfiguration);
58+
getRegistry().register(JacksonAutoConfiguration.class);
59+
60+
loadServletContext();
61+
getContext().refresh();
62+
}
63+
64+
private void loadServletContext() {
65+
if (context instanceof AnnotationConfigWebApplicationContext) {
66+
ServerContainer serverContainer = mock(ServerContainer.class);
67+
ServletContext servletContext = new MockServletContext();
68+
servletContext.setAttribute("javax.websocket.server.ServerContainer", serverContainer);
69+
((AnnotationConfigWebApplicationContext) context).setServletContext(servletContext);
70+
}
71+
}
72+
73+
public AnnotationConfigRegistry getRegistry() {
74+
return (AnnotationConfigRegistry) context;
75+
}
76+
77+
public AbstractApplicationContext getContext() {
78+
return context;
79+
}
80+
}

0 commit comments

Comments
 (0)