88import javax .servlet .http .Part ;
99import javax .websocket .Session ;
1010import javax .websocket .server .HandshakeRequest ;
11+ import java .util .Collection ;
12+ import java .util .HashMap ;
1113import java .util .List ;
1214import java .util .Map ;
1315import java .util .Optional ;
16+ import java .util .stream .Collectors ;
1417
1518public 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