Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example-graphql-tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {

// testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"

testCompile(project(":graphql-spring-boot-starter-test"))
}

jar.enabled = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.embedler.moon.graphql.boot.resolvers;

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import java.util.Random;
import org.springframework.stereotype.Component;

@Component
public class Mutation implements GraphQLMutationResolver {

public Post createPost(String text){
Post post = new Post(new Random().nextLong());
post.setText(text);
return post;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
class Post {

private Long id;
private String text;

Post(Long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
type Query {
post(id: ID!): Post
}

type Mutation {
createPost(text:String):Post
}
type Post {
id: ID!
text: String
comments: [Comment!]!
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.embedler.moon.graphql.boot;

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

@Test
public void create_post() throws IOException {
ObjectNode variables = new ObjectMapper().createObjectNode();
variables.put("text", "lorem ipsum dolor sit amet");
JsonNode parsedResponse = graphQLTestUtils.perform("graphql/create-post.graphql",variables);
assertNotNull(parsedResponse);
assertNotNull(parsedResponse.get("data"));
assertNotNull(parsedResponse.get("data").get("createPost"));
assertNotNull(parsedResponse.get("data").get("createPost").get("id").asText());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation CreatePost($text:String){
createPost(text:$text){
id
}
}

This file was deleted.

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

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.core.util.BufferRecyclers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.web.client.TestRestTemplate;
Expand All @@ -17,34 +19,26 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Component
public class GraphQLTestUtils {

@Value("classpath:graphql/query-wrapper.json")
private Resource queryWrapperFile;
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private TestRestTemplate restTemplate;
private String queryWrapper;

@PostConstruct
public void initQueryWrapper() throws IOException {
queryWrapper = loadResource(queryWrapperFile);
}
private ObjectMapper objectMapper = new ObjectMapper();

private String createJsonQuery(String graphql) {
return queryWrapper.replace("__payload__", escapeQuery(graphql));
}
private String createJsonQuery(String graphql, ObjectNode variables)
throws JsonProcessingException {

private String escapeQuery(String graphql) {
StringBuilder result = new StringBuilder();
BufferRecyclers.getJsonStringEncoder().quoteAsString(graphql, result);
return result.toString();
ObjectNode wrapper = objectMapper.createObjectNode();
wrapper.put("query", graphql);
wrapper.set("variables", variables);
return objectMapper.writeValueAsString(wrapper);
}

private JsonNode parse(String payload) throws IOException {
return new ObjectMapper().readTree(payload);
return objectMapper.readTree(payload);
}

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

public JsonNode perform(String graphqlResource) throws IOException {
return perform(graphqlResource,null);
}

public JsonNode perform(String graphqlResource, ObjectNode variables) throws IOException {
String graphql = loadQuery(graphqlResource);
String payload = createJsonQuery(graphql);
String payload = createJsonQuery(graphql,variables);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.embedler.moon.graphql.testing;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestingConfiguration {

@Bean
public GraphQLTestUtils graphQLTestUtils(){
return new GraphQLTestUtils();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.embedler.moon.graphql.testing.TestingConfiguration
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ include ":graphiql-spring-boot-starter"
include ":voyager-spring-boot-autoconfigure"
include ":voyager-spring-boot-starter"

include ":graphql-spring-boot-starter-test"

include ":example"
include ":example-spring-common"
include ':example-graphql-tools'
Expand Down