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

Commit 32fe34e

Browse files
feat: add support for named operations in GraphQLTestTemplate
1 parent fac33ab commit 32fe34e

File tree

10 files changed

+164
-4
lines changed

10 files changed

+164
-4
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Arrays;
2525
import java.util.List;
2626

27+
import static java.util.Objects.nonNull;
28+
2729
public class GraphQLTestTemplate {
2830

2931
private final ResourceLoader resourceLoader;
@@ -46,11 +48,14 @@ public GraphQLTestTemplate(
4648
this.objectMapper = objectMapper;
4749
}
4850

49-
private String createJsonQuery(String graphql, ObjectNode variables)
51+
private String createJsonQuery(String graphql, String operation, ObjectNode variables)
5052
throws JsonProcessingException {
5153

5254
ObjectNode wrapper = objectMapper.createObjectNode();
5355
wrapper.put("query", graphql);
56+
if (nonNull(operation)) {
57+
wrapper.put("operationName", operation);
58+
}
5459
wrapper.set("variables", variables);
5560
return objectMapper.writeValueAsString(wrapper);
5661
}
@@ -196,18 +201,30 @@ public GraphQLResponse perform(String graphqlResource) throws IOException {
196201
}
197202

198203
public GraphQLResponse perform(String graphqlResource, ObjectNode variables) throws IOException {
204+
return perform(graphqlResource, null, variables);
205+
}
206+
207+
public GraphQLResponse perform(String graphqlResource, String operationName) throws IOException {
208+
return perform(graphqlResource, operationName, null);
209+
}
210+
211+
public GraphQLResponse perform(String graphqlResource, String operation, ObjectNode variables) throws IOException {
199212
String graphql = loadQuery(graphqlResource);
200-
String payload = createJsonQuery(graphql, variables);
213+
String payload = createJsonQuery(graphql, operation, variables);
201214
return post(payload);
202215
}
203216

204217
public GraphQLResponse perform(String graphqlResource, ObjectNode variables, List<String> fragmentResources) throws IOException {
218+
return perform(graphqlResource, null, variables, fragmentResources);
219+
}
220+
221+
public GraphQLResponse perform(String graphqlResource, String operationName, ObjectNode variables, List<String> fragmentResources) throws IOException {
205222
StringBuilder sb = new StringBuilder();
206223
for (String fragmentResource : fragmentResources) {
207224
sb.append(loadQuery(fragmentResource));
208225
}
209226
String graphql = sb.append(loadQuery(graphqlResource)).toString();
210-
String payload = createJsonQuery(graphql, variables);
227+
String payload = createJsonQuery(graphql, operationName, variables);
211228
return post(payload);
212229
}
213230

@@ -219,7 +236,7 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables, Lis
219236
* @throws IOException if the resource cannot be loaded from the classpath
220237
*/
221238
public GraphQLResponse postForResource(String graphqlResource) throws IOException {
222-
return perform(graphqlResource, null);
239+
return perform(graphqlResource, (String) null, null);
223240
}
224241

225242
/**
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
import com.graphql.spring.boot.test.beans.FooBar;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.core.io.ResourceLoader;
13+
14+
import java.io.IOException;
15+
import java.util.Collections;
16+
17+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
18+
public class GraphQLTestTemplateIntegrationTest {
19+
20+
private static final String SIMPLE_TEST_QUERY = "simple-test-query.graphql";
21+
private static final String SIMPLE_TEST_QUERY_WITH_FRAGMENTS = "simple-test-query-with-fragments.graphql";
22+
private static final String TEST_FRAGMENT_FILE = "foo-bar-fragment.graphql";
23+
private static final String QUERY_WITH_VARIABLES = "query-with-variables.graphql";
24+
private static final String MULTIPLE_QUERIES = "multiple-queries.graphql";
25+
26+
@Autowired
27+
private ResourceLoader resourceLoader;
28+
29+
@Autowired
30+
private TestRestTemplate testRestTemplate;
31+
32+
@Autowired
33+
private ObjectMapper objectMapper;
34+
35+
private GraphQLTestTemplate graphQLTestTemplate;
36+
37+
@BeforeEach
38+
void setUp() {
39+
graphQLTestTemplate = new GraphQLTestTemplate(resourceLoader, testRestTemplate, "/graphql", objectMapper);
40+
}
41+
42+
@Test
43+
@DisplayName("Test postForResource with only the GraphQL resource provided.")
44+
void testPostForResource() throws IOException {
45+
graphQLTestTemplate.postForResource(SIMPLE_TEST_QUERY)
46+
.assertThatNoErrorsArePresent()
47+
.assertThatField("$.data.otherQuery").asString().isEqualTo("TEST");
48+
}
49+
50+
@Test
51+
@DisplayName("Test postForResource with fragments.")
52+
void testPostForResourceWithFragments() throws IOException {
53+
graphQLTestTemplate.postForResource(SIMPLE_TEST_QUERY_WITH_FRAGMENTS,
54+
Collections.singletonList(TEST_FRAGMENT_FILE))
55+
.assertThatNoErrorsArePresent()
56+
.assertThatField("$.data.fooBar").as(FooBar.class)
57+
.usingRecursiveComparison()
58+
.ignoringAllOverriddenEquals()
59+
.isEqualTo(FooBar.builder().foo("FOO").bar("BAR").build());
60+
}
61+
62+
@Test
63+
@DisplayName("Test perform with variables.")
64+
void testPerformWithVariables() throws IOException {
65+
// GIVEN
66+
final String inputString = "test-input-string";
67+
final ObjectNode variables = objectMapper.createObjectNode();
68+
variables.put("input", inputString);
69+
// WHEN - THEN
70+
graphQLTestTemplate.perform(QUERY_WITH_VARIABLES, variables)
71+
.assertThatNoErrorsArePresent()
72+
.assertThatField("$.data.queryWithVariables").asString().isEqualTo(inputString);
73+
}
74+
75+
@Test
76+
@DisplayName("Test perform with operation name.")
77+
void testPerformWithOperationName() throws IOException {
78+
// WHEN - THEN
79+
graphQLTestTemplate.perform(MULTIPLE_QUERIES, "testQuery1")
80+
.assertThatNoErrorsArePresent()
81+
.assertThatField("$.data.dummy").asBoolean().isTrue();
82+
graphQLTestTemplate.perform(MULTIPLE_QUERIES, "testQuery2")
83+
.assertThatNoErrorsArePresent()
84+
.assertThatField("$.data.otherQuery").asString().isEqualTo("TEST");
85+
}
86+
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/beans/DummyQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@ public class DummyQuery implements GraphQLQueryResolver {
99
public boolean dummy() {
1010
return true;
1111
}
12+
13+
public String otherQuery() {
14+
return "TEST";
15+
}
16+
17+
public FooBar fooBar() {
18+
return FooBar.builder().bar("BAR").foo("FOO").build();
19+
}
20+
21+
public String queryWithVariables(final String input) {
22+
return input;
23+
}
1224
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.graphql.spring.boot.test.beans;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FooBar {
13+
private String foo;
14+
private String bar;
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fragment FooBarFragment on FooBar {
2+
foo
3+
bar
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query testQuery1 {
2+
dummy
3+
}
4+
5+
query testQuery2 {
6+
otherQuery
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query ($input: String!) {
2+
queryWithVariables(input: $input)
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query {
2+
fooBar {
3+
...FooBarFragment
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
otherQuery
3+
}

graphql-spring-boot-test/src/test/resources/test-schema.graphqls

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
type FooBar {
2+
foo: String!
3+
bar: String!
4+
}
5+
16
type Subscription {
27
timer: Float!
38
subscriptionWithParameter(param: String!): String!
@@ -8,4 +13,7 @@ type Subscription {
813

914
type Query {
1015
dummy: Boolean!
16+
otherQuery: String!
17+
fooBar: FooBar!
18+
queryWithVariables(input: String!): String!
1119
}

0 commit comments

Comments
 (0)