Skip to content

Commit 98a4295

Browse files
authored
Merge pull request #123 from graphql-java-kickstart/feature/122
Deprecated getFiles and added clearer replacements
2 parents bca69e6 + 8e08d84 commit 98a4295

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141
testRuntime "cglib:cglib-nodep:3.2.4"
4242
testRuntime "org.objenesis:objenesis:2.5.1"
4343
testCompile 'org.slf4j:slf4j-simple:1.7.24'
44-
testCompile 'org.springframework:spring-test:4.3.7.RELEASE' // MockHttpServletRequest, MockHttpServletResponse
44+
testCompile 'org.springframework:spring-test:4.3.7.RELEASE'
4545
testRuntime 'org.springframework:spring-web:4.3.7.RELEASE'
4646

4747
// OSGi

src/main/java/graphql/servlet/AbstractGraphQLHttpServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
142142
variablesMap.ifPresent(map -> graphQLRequests.forEach(r -> mapMultipartVariables(r, map, fileItems)));
143143
GraphQLBatchedInvocationInput invocationInput =
144144
invocationInputFactory.create(graphQLRequests, request, response);
145-
invocationInput.getContext().setFiles(fileItems);
145+
invocationInput.getContext().setParts(fileItems);
146146
queryBatched(queryInvoker, graphQLObjectMapper, invocationInput, response);
147147
return;
148148
} else {
@@ -156,7 +156,7 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
156156
variablesMap.ifPresent(m -> mapMultipartVariables(graphQLRequest, m, fileItems));
157157
GraphQLSingleInvocationInput invocationInput =
158158
invocationInputFactory.create(graphQLRequest, request, response);
159-
invocationInput.getContext().setFiles(fileItems);
159+
invocationInput.getContext().setParts(fileItems);
160160
query(queryInvoker, graphQLObjectMapper, invocationInput, response);
161161
return;
162162
}

src/main/java/graphql/servlet/GraphQLContext.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import javax.servlet.http.Part;
99
import javax.websocket.Session;
1010
import javax.websocket.server.HandshakeRequest;
11+
import java.util.Collection;
12+
import java.util.HashMap;
1113
import java.util.List;
1214
import java.util.Map;
1315
import java.util.Optional;
16+
import java.util.stream.Collectors;
1417

1518
public class GraphQLContext {
1619

@@ -20,7 +23,7 @@ public class GraphQLContext {
2023
private HandshakeRequest handshakeRequest;
2124

2225
private Subject subject;
23-
private Map<String, List<Part>> files;
26+
private Map<String, List<Part>> parts;
2427

2528
private DataLoaderRegistry dataLoaderRegistry;
2629

@@ -75,12 +78,37 @@ public Optional<HandshakeRequest> getHandshakeRequest() {
7578
return Optional.ofNullable(handshakeRequest);
7679
}
7780

81+
/**
82+
* @return list of all parts representing files
83+
*/
84+
public List<Part> getFileParts() {
85+
return getParts().values()
86+
.stream()
87+
.flatMap(Collection::stream)
88+
.filter(part -> part.getContentType() != null)
89+
.collect(Collectors.toList());
90+
}
91+
92+
/**
93+
* Contrary what the name implies this method returns all parts and not just the ones that represent actual files.
94+
* That's why this method has been deprecated in favor of the ones that communicate their intent more clearly.
95+
*
96+
* @deprecated use {@link #getParts()} or {@link #getFileParts()} instead
97+
*/
98+
@Deprecated
7899
public Optional<Map<String, List<Part>>> getFiles() {
79-
return Optional.ofNullable(files);
100+
return Optional.ofNullable(parts);
101+
}
102+
103+
/**
104+
* @return map representing all form fields
105+
*/
106+
public Map<String, List<Part>> getParts() {
107+
return parts != null ? parts : new HashMap<>();
80108
}
81109

82-
public void setFiles(Map<String, List<Part>> files) {
83-
this.files = files;
110+
public void setParts(Map<String, List<Part>> parts) {
111+
this.parts = parts;
84112
}
85113

86114
public Optional<DataLoaderRegistry> getDataLoaderRegistry() {

0 commit comments

Comments
 (0)