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

Commit fd2129a

Browse files
authored
Merge pull request #98 from mxab/feature/graphql-spring-boot-starter-test
Add graphiql-spring-boot-starter project
2 parents 68504d0 + 4736bc6 commit fd2129a

File tree

12 files changed

+105
-24
lines changed

12 files changed

+105
-24
lines changed

example-graphql-tools/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ dependencies {
3535

3636
// testCompile group: 'junit', name: 'junit', version: '4.12'
3737
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"
38+
39+
testCompile(project(":graphql-spring-boot-starter-test"))
3840
}
3941

4042
jar.enabled = false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.embedler.moon.graphql.boot.resolvers;
2+
3+
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
4+
import java.util.Random;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class Mutation implements GraphQLMutationResolver {
9+
10+
public Post createPost(String text){
11+
Post post = new Post(new Random().nextLong());
12+
post.setText(text);
13+
return post;
14+
}
15+
}

example-graphql-tools/src/main/java/com/embedler/moon/graphql/boot/resolvers/Post.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
class Post {
44

55
private Long id;
6+
private String text;
67

78
Post(Long id) {
89
this.id = id;
910
}
1011

12+
public String getText() {
13+
return text;
14+
}
15+
16+
public void setText(String text) {
17+
this.text = text;
18+
}
1119
}

example-graphql-tools/src/main/resources/graphql-tools.graphqls

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
type Query {
22
post(id: ID!): Post
33
}
4-
4+
type Mutation {
5+
createPost(text:String):Post
6+
}
57
type Post {
68
id: ID!
9+
text: String
710
comments: [Comment!]!
811
}
912

example-graphql-tools/src/test/java/com/embedler/moon/graphql/boot/GraphQLToolsSampleApplicationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.embedler.moon.graphql.boot;
22

3+
import com.embedler.moon.graphql.testing.GraphQLTestUtils;
34
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
47
import org.junit.Test;
58
import org.junit.runner.RunWith;
69
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,4 +31,15 @@ public void get_comments() throws IOException {
2831
assertEquals("1", parsedResponse.get("data").get("post").get("id").asText());
2932
}
3033

34+
@Test
35+
public void create_post() throws IOException {
36+
ObjectNode variables = new ObjectMapper().createObjectNode();
37+
variables.put("text", "lorem ipsum dolor sit amet");
38+
JsonNode parsedResponse = graphQLTestUtils.perform("graphql/create-post.graphql",variables);
39+
assertNotNull(parsedResponse);
40+
assertNotNull(parsedResponse.get("data"));
41+
assertNotNull(parsedResponse.get("data").get("createPost"));
42+
assertNotNull(parsedResponse.get("data").get("createPost").get("id").asText());
43+
}
44+
3145
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mutation CreatePost($text:String){
2+
createPost(text:$text){
3+
id
4+
}
5+
}

example-graphql-tools/src/test/resources/graphql/query-wrapper.json

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 oEmbedler Inc. and Contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
9+
* persons to whom the Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
dependencies {
20+
compile("org.springframework:spring-web:$LIB_SPRING_CORE_VER")
21+
compile("org.springframework.boot:spring-boot-test:$LIB_SPRING_BOOT_VER")
22+
compile("com.fasterxml.jackson.core:jackson-databind:2.5.4")
23+
}

example-graphql-tools/src/test/java/com/embedler/moon/graphql/boot/GraphQLTestUtils.java renamed to graphql-spring-boot-starter-test/src/main/java/com/embedler/moon/graphql/testing/GraphQLTestUtils.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package com.embedler.moon.graphql.boot;
1+
package com.embedler.moon.graphql.testing;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
24

3-
import com.fasterxml.jackson.core.util.BufferRecyclers;
45
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.node.ObjectNode;
68
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.beans.factory.annotation.Value;
810
import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -17,34 +19,26 @@
1719
import java.io.InputStream;
1820
import java.nio.charset.StandardCharsets;
1921

20-
@Component
2122
public class GraphQLTestUtils {
2223

23-
@Value("classpath:graphql/query-wrapper.json")
24-
private Resource queryWrapperFile;
2524
@Autowired
2625
private ResourceLoader resourceLoader;
2726
@Autowired
2827
private TestRestTemplate restTemplate;
29-
private String queryWrapper;
3028

31-
@PostConstruct
32-
public void initQueryWrapper() throws IOException {
33-
queryWrapper = loadResource(queryWrapperFile);
34-
}
29+
private ObjectMapper objectMapper = new ObjectMapper();
3530

36-
private String createJsonQuery(String graphql) {
37-
return queryWrapper.replace("__payload__", escapeQuery(graphql));
38-
}
31+
private String createJsonQuery(String graphql, ObjectNode variables)
32+
throws JsonProcessingException {
3933

40-
private String escapeQuery(String graphql) {
41-
StringBuilder result = new StringBuilder();
42-
BufferRecyclers.getJsonStringEncoder().quoteAsString(graphql, result);
43-
return result.toString();
34+
ObjectNode wrapper = objectMapper.createObjectNode();
35+
wrapper.put("query", graphql);
36+
wrapper.set("variables", variables);
37+
return objectMapper.writeValueAsString(wrapper);
4438
}
4539

4640
private JsonNode parse(String payload) throws IOException {
47-
return new ObjectMapper().readTree(payload);
41+
return objectMapper.readTree(payload);
4842
}
4943

5044
private String loadQuery(String location) throws IOException {
@@ -59,8 +53,12 @@ private String loadResource(Resource resource) throws IOException {
5953
}
6054

6155
public JsonNode perform(String graphqlResource) throws IOException {
56+
return perform(graphqlResource,null);
57+
}
58+
59+
public JsonNode perform(String graphqlResource, ObjectNode variables) throws IOException {
6260
String graphql = loadQuery(graphqlResource);
63-
String payload = createJsonQuery(graphql);
61+
String payload = createJsonQuery(graphql,variables);
6462

6563
HttpHeaders headers = new HttpHeaders();
6664
headers.setContentType(MediaType.APPLICATION_JSON);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.embedler.moon.graphql.testing;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class TestingConfiguration {
8+
9+
@Bean
10+
public GraphQLTestUtils graphQLTestUtils(){
11+
return new GraphQLTestUtils();
12+
}
13+
}
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+
com.embedler.moon.graphql.testing.TestingConfiguration

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ include ":graphiql-spring-boot-starter"
2626
include ":voyager-spring-boot-autoconfigure"
2727
include ":voyager-spring-boot-starter"
2828

29+
include ":graphql-spring-boot-starter-test"
30+
2931
include ":example"
3032
include ":example-spring-common"
3133
include ':example-graphql-tools'

0 commit comments

Comments
 (0)