Skip to content

Commit be05b03

Browse files
committed
Extract GraphQlRequest interface
See gh-332
1 parent 08597c9 commit be05b03

File tree

10 files changed

+151
-100
lines changed

10 files changed

+151
-100
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.springframework.core.ParameterizedTypeReference;
3636
import org.springframework.core.ResolvableType;
37+
import org.springframework.graphql.DefaultGraphQlRequest;
3738
import org.springframework.graphql.GraphQlRequest;
3839
import org.springframework.graphql.GraphQlResponse;
3940
import org.springframework.graphql.GraphQlResponseError;
@@ -179,7 +180,7 @@ public Subscription executeSubscription() {
179180
}
180181

181182
private GraphQlRequest request() {
182-
return new GraphQlRequest(this.document, this.operationName, this.variables);
183+
return new DefaultGraphQlRequest(this.document, this.operationName, this.variables);
183184
}
184185

185186
private DefaultResponse mapResponse(GraphQlResponse response, GraphQlRequest request) {
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql;
18+
19+
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.CollectionUtils;
26+
import org.springframework.util.ObjectUtils;
27+
28+
/**
29+
* Default implementation of {@link GraphQlRequest}.
30+
*
31+
* @author Rossen Stoyanchev
32+
* @since 1.0.0
33+
*/
34+
public class DefaultGraphQlRequest implements GraphQlRequest {
35+
36+
private final String document;
37+
38+
@Nullable
39+
private final String operationName;
40+
41+
private final Map<String, Object> variables;
42+
43+
44+
/**
45+
* Create a request.
46+
* @param document textual representation of the operation(s)
47+
*/
48+
public DefaultGraphQlRequest(String document) {
49+
this(document, null, null);
50+
}
51+
52+
/**
53+
* Create a request with a complete set of inputs.
54+
* @param document textual representation of the operation(s)
55+
* @param operationName optionally, the name of the operation to execute
56+
* @param variables variables by which the operation is parameterized
57+
*/
58+
public DefaultGraphQlRequest(
59+
String document, @Nullable String operationName, @Nullable Map<String, Object> variables) {
60+
61+
Assert.notNull(document, "'document' is required");
62+
this.document = document;
63+
this.operationName = operationName;
64+
this.variables = (variables != null ? variables : Collections.emptyMap());
65+
}
66+
67+
68+
@Override
69+
public String getDocument() {
70+
return this.document;
71+
}
72+
73+
@Override
74+
@Nullable
75+
public String getOperationName() {
76+
return this.operationName;
77+
}
78+
79+
@Override
80+
public Map<String, Object> getVariables() {
81+
return this.variables;
82+
}
83+
84+
@Override
85+
public Map<String, Object> toMap() {
86+
Map<String, Object> map = new LinkedHashMap<>(3);
87+
map.put("query", getDocument());
88+
if (getOperationName() != null) {
89+
map.put("operationName", getOperationName());
90+
}
91+
if (!CollectionUtils.isEmpty(getVariables())) {
92+
map.put("variables", new LinkedHashMap<>(getVariables()));
93+
}
94+
return map;
95+
}
96+
97+
98+
@Override
99+
public boolean equals(Object o) {
100+
if (! (o instanceof DefaultGraphQlRequest)) {
101+
return false;
102+
}
103+
DefaultGraphQlRequest other = (DefaultGraphQlRequest) o;
104+
return (getDocument().equals(other.getDocument()) &&
105+
ObjectUtils.nullSafeEquals(getOperationName(), other.getOperationName()) &&
106+
ObjectUtils.nullSafeEquals(getVariables(), other.getVariables()));
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
return this.document.hashCode() +
112+
31 * ObjectUtils.nullSafeHashCode(this.operationName) +
113+
31 * this.variables.hashCode();
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return "document='" + getDocument() + "'" +
119+
((getOperationName() != null) ? ", operationName='" + getOperationName() + "'" : "") +
120+
(!CollectionUtils.isEmpty(getVariables()) ? ", variables=" + getVariables() : "");
121+
}
122+
123+
}

spring-graphql/src/main/java/org/springframework/graphql/GraphQlRequest.java

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616

1717
package org.springframework.graphql;
1818

19-
import java.util.Collections;
20-
import java.util.LinkedHashMap;
2119
import java.util.Map;
2220

2321
import org.springframework.lang.Nullable;
24-
import org.springframework.util.Assert;
25-
import org.springframework.util.CollectionUtils;
26-
import org.springframework.util.ObjectUtils;
22+
2723

2824
/**
2925
* Represents a GraphQL request with the inputs to pass to a GraphQL service
@@ -36,62 +32,26 @@
3632
* @author Rossen Stoyanchev
3733
* @since 1.0.0
3834
*/
39-
public class GraphQlRequest {
40-
41-
private final String document;
42-
43-
@Nullable
44-
private final String operationName;
45-
46-
private final Map<String, Object> variables;
47-
48-
49-
/**
50-
* Create a request.
51-
* @param document textual representation of the operation(s)
52-
*/
53-
public GraphQlRequest(String document) {
54-
this(document, null, null);
55-
}
56-
57-
/**
58-
* Create a request with a complete set of inputs.
59-
* @param document textual representation of the operation(s)
60-
* @param operationName optionally, the name of the operation to execute
61-
* @param variables variables by which the operation is parameterized
62-
*/
63-
public GraphQlRequest(String document, @Nullable String operationName, @Nullable Map<String, Object> variables) {
64-
Assert.notNull(document, "'document' is required");
65-
this.document = document;
66-
this.operationName = operationName;
67-
this.variables = (variables != null ? variables : Collections.emptyMap());
68-
}
69-
35+
public interface GraphQlRequest {
7036

7137
/**
7238
* Return the GraphQL document which is the textual representation of an
7339
* operation (or operations) to perform, including any selection sets and
7440
* fragments.
7541
*/
76-
public String getDocument() {
77-
return this.document;
78-
}
42+
String getDocument();
7943

8044
/**
8145
* Return the name of the operation in the {@link #getDocument() document}
8246
* to execute, if the document contains multiple operations.
8347
*/
8448
@Nullable
85-
public String getOperationName() {
86-
return this.operationName;
87-
}
49+
String getOperationName();
8850

8951
/**
9052
* Return values for variable defined by the operation.
9153
*/
92-
public Map<String, Object> getVariables() {
93-
return this.variables;
94-
}
54+
Map<String, Object> getVariables();
9555

9656
/**
9757
* Convert the request to a {@link Map} as defined in
@@ -104,41 +64,6 @@ public Map<String, Object> getVariables() {
10464
* <tr><td>variables</td><td>{@link #getVariables() variables}</td></tr>
10565
* </table>
10666
*/
107-
public Map<String, Object> toMap() {
108-
Map<String, Object> map = new LinkedHashMap<>(3);
109-
map.put("query", getDocument());
110-
if (getOperationName() != null) {
111-
map.put("operationName", getOperationName());
112-
}
113-
if (!CollectionUtils.isEmpty(getVariables())) {
114-
map.put("variables", new LinkedHashMap<>(getVariables()));
115-
}
116-
return map;
117-
}
118-
119-
@Override
120-
public boolean equals(Object o) {
121-
if (! (o instanceof GraphQlRequest)) {
122-
return false;
123-
}
124-
GraphQlRequest other = (GraphQlRequest) o;
125-
return (getDocument().equals(other.getDocument()) &&
126-
ObjectUtils.nullSafeEquals(getOperationName(), other.getOperationName()) &&
127-
ObjectUtils.nullSafeEquals(getVariables(), other.getVariables()));
128-
}
129-
130-
@Override
131-
public int hashCode() {
132-
return this.document.hashCode() +
133-
31 * ObjectUtils.nullSafeHashCode(this.operationName) +
134-
31 * this.variables.hashCode();
135-
}
136-
137-
@Override
138-
public String toString() {
139-
return "document='" + getDocument() + "'" +
140-
((getOperationName() != null) ? ", operationName='" + getOperationName() + "'" : "") +
141-
(!CollectionUtils.isEmpty(getVariables()) ? ", variables=" + getVariables() : "");
142-
}
67+
Map<String, Object> toMap();
14368

14469
}

spring-graphql/src/main/java/org/springframework/graphql/RequestInput.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import org.springframework.util.Assert;
3030

3131
/**
32-
* Extension of {@link GraphQlRequest} for server side handling, adding the
33-
* transport (e.g. HTTP or WebSocket handler) assigned {@link #getId() id} and
34-
* {@link #getLocale() locale} in the addition to the {@link GraphQlRequest}
35-
* inputs.
32+
* {@link GraphQlRequest} for server side handling, adding the transport (e.g. HTTP
33+
* or WebSocket handler) assigned {@link #getId() id} and {@link #getLocale()
34+
* locale} in the addition to the {@code GraphQlRequest} inputs.
3635
*
3736
* <p>{@code RequestInput} supports the initialization of {@link ExecutionInput}
3837
* that is passed to {@link graphql.GraphQL}. You can customize that via
@@ -42,7 +41,7 @@
4241
* @author Brian Clozel
4342
* @since 1.0.0
4443
*/
45-
public class RequestInput extends GraphQlRequest {
44+
public class RequestInput extends DefaultGraphQlRequest {
4645

4746
private final String id;
4847

spring-graphql/src/main/java/org/springframework/graphql/client/DefaultGraphQlClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.core.ParameterizedTypeReference;
2828
import org.springframework.core.codec.Decoder;
2929
import org.springframework.core.codec.Encoder;
30+
import org.springframework.graphql.DefaultGraphQlRequest;
3031
import org.springframework.graphql.GraphQlRequest;
3132
import org.springframework.graphql.GraphQlResponse;
3233
import org.springframework.graphql.support.DocumentSource;
@@ -176,7 +177,7 @@ public Flux<ClientGraphQlResponse> executeSubscription() {
176177

177178
private Mono<GraphQlRequest> initRequest() {
178179
return this.documentMono.map(document ->
179-
new GraphQlRequest(document, this.operationName, this.variables));
180+
new DefaultGraphQlRequest(document, this.operationName, this.variables));
180181
}
181182

182183
private DefaultClientGraphQlResponse initResponse(GraphQlRequest request, GraphQlResponse response) {

spring-graphql/src/test/java/org/springframework/graphql/client/DefaultGraphQlClientResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
3030

31-
import org.springframework.graphql.GraphQlRequest;
31+
import org.springframework.graphql.DefaultGraphQlRequest;
3232
import org.springframework.graphql.GraphQlResponseError;
3333
import org.springframework.http.codec.json.Jackson2JsonDecoder;
3434
import org.springframework.http.codec.json.Jackson2JsonEncoder;
@@ -158,7 +158,7 @@ private ClientGraphQlResponseField getField(String path, GraphQLError... errors)
158158

159159
private ClientGraphQlResponse creatResponse(Map<String, Object> responseMap) {
160160
return new DefaultClientGraphQlResponse(
161-
new GraphQlRequest("{test}"), GraphQlTransport.wrapResponseMap(responseMap),
161+
new DefaultGraphQlRequest("{test}"), GraphQlTransport.wrapResponseMap(responseMap),
162162
new Jackson2JsonEncoder(), new Jackson2JsonDecoder());
163163
}
164164

spring-graphql/src/test/java/org/springframework/graphql/client/GraphQlClientBuilderTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.junit.jupiter.api.Test;
2020
import reactor.core.publisher.Mono;
2121

22-
import org.springframework.graphql.GraphQlRequest;
2322
import org.springframework.graphql.support.DocumentSource;
2423

2524
import static org.assertj.core.api.Assertions.assertThat;

spring-graphql/src/test/java/org/springframework/graphql/client/GraphQlClientTestSupport.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.mockito.ArgumentCaptor;
2929
import reactor.core.publisher.Mono;
3030

31+
import org.springframework.graphql.DefaultGraphQlRequest;
3132
import org.springframework.graphql.GraphQlRequest;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.ObjectUtils;
@@ -49,7 +50,7 @@ public class GraphQlClientTestSupport {
4950
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
5051

5152

52-
private final ArgumentCaptor<GraphQlRequest> requestCaptor = ArgumentCaptor.forClass(GraphQlRequest.class);
53+
private final ArgumentCaptor<GraphQlRequest> requestCaptor = ArgumentCaptor.forClass(DefaultGraphQlRequest.class);
5354

5455
private final GraphQlTransport transport = mock(GraphQlTransport.class);
5556

@@ -72,15 +73,15 @@ protected GraphQlRequest request() {
7273

7374

7475
protected void initDataResponse(String document, String responseData) {
75-
initResponse(new GraphQlRequest(document), responseData);
76+
initResponse(new DefaultGraphQlRequest(document), responseData);
7677
}
7778

7879
protected void initErrorResponse(String document, GraphQLError... errors) {
79-
initResponse(new GraphQlRequest(document), null, errors);
80+
initResponse(new DefaultGraphQlRequest(document), null, errors);
8081
}
8182

8283
protected void initResponse(String document, String responseData, GraphQLError... errors) {
83-
initResponse(new GraphQlRequest(document), responseData, errors);
84+
initResponse(new DefaultGraphQlRequest(document), responseData, errors);
8485
}
8586

8687
protected void initResponse(GraphQlRequest request, @Nullable String responseData, GraphQLError... errors) {

spring-graphql/src/test/java/org/springframework/graphql/client/GraphQlClientTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.junit.jupiter.api.Test;
3131

3232
import org.springframework.core.ParameterizedTypeReference;
33+
import org.springframework.graphql.DefaultGraphQlRequest;
3334
import org.springframework.graphql.GraphQlRequest;
3435

3536
import static org.assertj.core.api.Assertions.assertThat;
@@ -100,7 +101,7 @@ void retrieveWithOperationNameAndVariables() {
100101
vars.put("foo", "bar");
101102
vars.put("keyOnly", null);
102103

103-
GraphQlRequest request = new GraphQlRequest("mockRequest1", "HeroNameAndFriends", vars);
104+
GraphQlRequest request = new DefaultGraphQlRequest("mockRequest1", "HeroNameAndFriends", vars);
104105
initResponse(request, "{\"hero\": {\"name\":\"R2-D2\"}}");
105106

106107
MovieCharacter character = graphQlClient().document(document)

0 commit comments

Comments
 (0)