Skip to content

Commit

Permalink
fix: update file-upload example (#321)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
federicorispo authored Jan 13, 2024
1 parent 0d196d6 commit 909a296
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
31 changes: 31 additions & 0 deletions file-upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# File Upload
This Spring example shows how the upload operation works using GraphQL and the Apollo Upload type.

## How to run
- Create a file: `echo 'Hello World!' > hello-world.txt`
- Run the example: `../gradlew bootRun`
- Make a GraphQL request:
```sh
curl --location 'http://localhost:9000/graphql' \
--form 'operations="{\"query\": \"mutation upload($file:Upload){ upload(file: $file){filename type content}}\"}"' \
--form 'map="{\"blob\": [\"variables.file\"]}"' \
--form 'blob=@"./hello-world.txt"'
```

If the file is successfully uploaded, this is the response received and the log shown:
```json
{
"data": {
"upload": {
"filename": "hello-world.txt",
"type": "text/plain",
"content": "Hello World!\n"
}
}
}

```

```log
INFO 1584868 --- [nio-9000-exec-7] upload.UploadMutation: File uploaded: {type=text/plain, filename=hello-world.txt, content=Hello World!}
```
8 changes: 7 additions & 1 deletion file-upload/src/main/java/upload/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
import org.springframework.stereotype.Service;

@Service
class Query implements GraphQLQueryResolver {}
class Query implements GraphQLQueryResolver {

// This query is necessary because the graphql-java-tool requires at least one query
public String getHello() {
return "Hello World";
}
}
14 changes: 10 additions & 4 deletions file-upload/src/main/java/upload/UploadMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import graphql.kickstart.tools.GraphQLMutationResolver;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -10,9 +12,13 @@
@Service
class UploadMutation implements GraphQLMutationResolver {

boolean upload(Part part) throws IOException {
log.info("Part: {}", part.getSubmittedFileName());
part.write(part.getSubmittedFileName());
return true;
Map<String, String> upload(Part part) throws IOException {
Map<String, String> uploadedFile = Map.of(
"filename", part.getSubmittedFileName(),
"type", part.getContentType(),
"content", new String(part.getInputStream().readAllBytes(), StandardCharsets.UTF_8)
);
log.info("File uploaded: {}", uploadedFile);
return uploadedFile;
}
}
11 changes: 9 additions & 2 deletions file-upload/src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
scalar Upload

type Query {
type UploadedFile {
filename: String
type: String
content: String
}

# The hello query declaration is necessary since the graphql-java-tools requires at least one query
type Query {
hello: String
}

type Mutation {
upload(file: Upload): Boolean
upload(file: Upload): UploadedFile
}

0 comments on commit 909a296

Please sign in to comment.