Skip to content

Commit 909a296

Browse files
fix: update file-upload example (#321)
- Fixed the file-upload example: it is necessary to declare at least one query, otherwise the graphql-java-tools throws exceptions during the schema parsing - Replaced the return boolean type with a UploadedFile type containing the filename, type and the content of the textual file uploaded - Added a README explaining how to run it Refs: #94
1 parent 0d196d6 commit 909a296

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

Diff for: file-upload/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# File Upload
2+
This Spring example shows how the upload operation works using GraphQL and the Apollo Upload type.
3+
4+
## How to run
5+
- Create a file: `echo 'Hello World!' > hello-world.txt`
6+
- Run the example: `../gradlew bootRun`
7+
- Make a GraphQL request:
8+
```sh
9+
curl --location 'http://localhost:9000/graphql' \
10+
--form 'operations="{\"query\": \"mutation upload($file:Upload){ upload(file: $file){filename type content}}\"}"' \
11+
--form 'map="{\"blob\": [\"variables.file\"]}"' \
12+
--form 'blob=@"./hello-world.txt"'
13+
```
14+
15+
If the file is successfully uploaded, this is the response received and the log shown:
16+
```json
17+
{
18+
"data": {
19+
"upload": {
20+
"filename": "hello-world.txt",
21+
"type": "text/plain",
22+
"content": "Hello World!\n"
23+
}
24+
}
25+
}
26+
27+
```
28+
29+
```log
30+
INFO 1584868 --- [nio-9000-exec-7] upload.UploadMutation: File uploaded: {type=text/plain, filename=hello-world.txt, content=Hello World!}
31+
```

Diff for: file-upload/src/main/java/upload/Query.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
import org.springframework.stereotype.Service;
55

66
@Service
7-
class Query implements GraphQLQueryResolver {}
7+
class Query implements GraphQLQueryResolver {
8+
9+
// This query is necessary because the graphql-java-tool requires at least one query
10+
public String getHello() {
11+
return "Hello World";
12+
}
13+
}

Diff for: file-upload/src/main/java/upload/UploadMutation.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import graphql.kickstart.tools.GraphQLMutationResolver;
44
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.util.Map;
57
import javax.servlet.http.Part;
68
import lombok.extern.slf4j.Slf4j;
79
import org.springframework.stereotype.Service;
@@ -10,9 +12,13 @@
1012
@Service
1113
class UploadMutation implements GraphQLMutationResolver {
1214

13-
boolean upload(Part part) throws IOException {
14-
log.info("Part: {}", part.getSubmittedFileName());
15-
part.write(part.getSubmittedFileName());
16-
return true;
15+
Map<String, String> upload(Part part) throws IOException {
16+
Map<String, String> uploadedFile = Map.of(
17+
"filename", part.getSubmittedFileName(),
18+
"type", part.getContentType(),
19+
"content", new String(part.getInputStream().readAllBytes(), StandardCharsets.UTF_8)
20+
);
21+
log.info("File uploaded: {}", uploadedFile);
22+
return uploadedFile;
1723
}
1824
}

Diff for: file-upload/src/main/resources/schema.graphqls

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
scalar Upload
22

3-
type Query {
3+
type UploadedFile {
4+
filename: String
5+
type: String
6+
content: String
7+
}
48

9+
# The hello query declaration is necessary since the graphql-java-tools requires at least one query
10+
type Query {
11+
hello: String
512
}
613

714
type Mutation {
8-
upload(file: Upload): Boolean
15+
upload(file: Upload): UploadedFile
916
}

0 commit comments

Comments
 (0)