Skip to content

fix: update file-upload example #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}