basic spring mvc and spring mvc starter integration which exposes a GraphQL endpoint
This commit is contained in:
committed by
Rossen Stoyanchev
parent
e8db755430
commit
7b549d7932
8
graphql-java-spring-boot-starter-webmvc/build.gradle
Normal file
8
graphql-java-spring-boot-starter-webmvc/build.gradle
Normal file
@@ -0,0 +1,8 @@
|
||||
description = "GraphQL Java Spring Boot starter Webmvc"
|
||||
dependencies {
|
||||
compile "org.springframework.boot:spring-boot-autoconfigure:$springBootVersion"
|
||||
compile project(':graphql-java-spring-webmvc')
|
||||
|
||||
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
|
||||
testCompile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.graphql.components.GraphQLController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
@ComponentScan(basePackageClasses = GraphQLController.class)
|
||||
public class GraphQLEndpointConfiguration {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.graphql.GraphQLEndpointConfiguration
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResultImpl;
|
||||
import graphql.GraphQL;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class IntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
GraphQL graphql;
|
||||
|
||||
@Test
|
||||
public void endpointIsAvailable() {
|
||||
String query = "{foo}";
|
||||
|
||||
ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult()
|
||||
.data("bar")
|
||||
.build();
|
||||
CompletableFuture cf = CompletableFuture.completedFuture(executionResult);
|
||||
ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class);
|
||||
Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf);
|
||||
|
||||
Map<String, String> body = new LinkedHashMap<>();
|
||||
body.put("query", query);
|
||||
|
||||
ResponseEntity responseEntity = this.restTemplate.postForEntity("/graphql", body, Map.class);
|
||||
|
||||
Map responseBody = (Map) responseEntity.getBody();
|
||||
assertThat(responseBody.get("data"), is("bar"));
|
||||
assertThat(captor.getValue().getQuery(), is(query));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.springframework.graphql;
|
||||
|
||||
import graphql.GraphQL;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IntegrationTestConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public GraphQL graphQL() {
|
||||
return Mockito.mock(GraphQL.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user