Polishing contribution
Closes gh-742
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2022 the original author or authors.
|
||||
* Copyright 2020-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -43,8 +43,8 @@ public class RSocketGraphQlRequest extends DefaultExecutionGraphQlRequest implem
|
||||
* @param locale the locale from the HTTP request, if any
|
||||
*/
|
||||
public RSocketGraphQlRequest(Map<String, Object> body, String id, @Nullable Locale locale) {
|
||||
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
|
||||
getKey("extensions", body), id, locale);
|
||||
super(getKey(QUERY_KEY, body), getKey(OPERATION_NAME_KEY, body),
|
||||
getKey(VARIABLES_KEY, body), getKey(EXTENSIONS_KEY, body), id, locale);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -84,7 +84,8 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
|
||||
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
|
||||
Map<String, Object> attributes, Map<String, Object> body, String id, @Nullable Locale locale) {
|
||||
|
||||
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
|
||||
super(getQuery(body), getOperation(body),
|
||||
getMap(VARIABLES_KEY, body), getMap(EXTENSIONS_KEY, body), id, locale);
|
||||
|
||||
Assert.notNull(uri, "URI is required'");
|
||||
Assert.notNull(headers, "HttpHeaders is required'");
|
||||
@@ -96,18 +97,18 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
|
||||
}
|
||||
|
||||
private static String getQuery(Map<String, Object> body) {
|
||||
Object value = body.get("query");
|
||||
Object value = body.get(QUERY_KEY);
|
||||
if (!(value instanceof String query) || !StringUtils.hasText(query)) {
|
||||
throw new ServerWebInputException("Invalid value for 'query'");
|
||||
throw new ServerWebInputException("Invalid value for '" + QUERY_KEY + "'");
|
||||
}
|
||||
return (String) value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getOperation(Map<String, Object> body) {
|
||||
Object value = body.get("operationName");
|
||||
Object value = body.get(OPERATION_NAME_KEY);
|
||||
if (value != null && !(value instanceof String)) {
|
||||
throw new ServerWebInputException("Invalid value for 'operationName'");
|
||||
throw new ServerWebInputException("Invalid value for '" + OPERATION_NAME_KEY + "'");
|
||||
}
|
||||
return (String) value;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,15 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public class DefaultGraphQlRequest implements GraphQlRequest {
|
||||
|
||||
protected static final String QUERY_KEY = "query";
|
||||
|
||||
protected static final String OPERATION_NAME_KEY = "operationName";
|
||||
|
||||
protected static final String VARIABLES_KEY = "variables";
|
||||
|
||||
protected static final String EXTENSIONS_KEY = "extensions";
|
||||
|
||||
|
||||
private final String document;
|
||||
|
||||
@Nullable
|
||||
@@ -96,15 +105,15 @@ public class DefaultGraphQlRequest implements GraphQlRequest {
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new LinkedHashMap<>(3);
|
||||
map.put("query", getDocument());
|
||||
map.put(QUERY_KEY, getDocument());
|
||||
if (getOperationName() != null) {
|
||||
map.put("operationName", getOperationName());
|
||||
map.put(OPERATION_NAME_KEY, getOperationName());
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(getVariables())) {
|
||||
map.put("variables", new LinkedHashMap<>(getVariables()));
|
||||
map.put(VARIABLES_KEY, new LinkedHashMap<>(getVariables()));
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(getExtensions())) {
|
||||
map.put("extensions", new LinkedHashMap<>(getExtensions()));
|
||||
map.put(EXTENSIONS_KEY, new LinkedHashMap<>(getExtensions()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2022 the original author or authors.
|
||||
* Copyright 2020-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,12 @@
|
||||
|
||||
package org.springframework.graphql.support;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.graphql.GraphQlRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -31,19 +32,24 @@ class DefaultGraphQlRequestTests {
|
||||
|
||||
@Test
|
||||
void requestAsMapShouldContainAllEntries() {
|
||||
|
||||
String document = "query HeroNameAndFriends($episode: Episode) {" +
|
||||
" hero(episode: $episode) {" +
|
||||
" name"
|
||||
+ " }" +
|
||||
"}";
|
||||
Map<String, Object> variables = Collections.singletonMap("episode", "JEDI");
|
||||
Map<String, Object> extensions = Collections.singletonMap("myExtension", "value");
|
||||
|
||||
DefaultExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest(document, "HeroNameAndFriends",
|
||||
variables, extensions, "1", null);
|
||||
Map<String, Object> variables = Map.of("episode", "JEDI");
|
||||
Map<String, Object> extensions = Map.of("myExtension", "value");
|
||||
|
||||
assertThat(request.toMap()).containsEntry("query", document).containsEntry("operationName", "HeroNameAndFriends")
|
||||
.containsEntry("variables", variables).containsEntry("extensions", extensions);
|
||||
GraphQlRequest request = new DefaultExecutionGraphQlRequest(
|
||||
document, "HeroNameAndFriends", variables, extensions, "1", null);
|
||||
|
||||
assertThat(request.toMap())
|
||||
.containsEntry("query", document)
|
||||
.containsEntry("operationName", "HeroNameAndFriends")
|
||||
.containsEntry("variables", variables)
|
||||
.containsEntry("extensions", extensions);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user