File tree Expand file tree Collapse file tree 4 files changed +57
-7
lines changed Expand file tree Collapse file tree 4 files changed +57
-7
lines changed Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 44import 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+ }
Original file line number Diff line number Diff line change 22
33import graphql .kickstart .tools .GraphQLMutationResolver ;
44import java .io .IOException ;
5+ import java .nio .charset .StandardCharsets ;
6+ import java .util .Map ;
57import javax .servlet .http .Part ;
68import lombok .extern .slf4j .Slf4j ;
79import org .springframework .stereotype .Service ;
1012@ Service
1113class 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}
Original file line number Diff line number Diff line change 11scalar 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
714type Mutation {
8- upload (file : Upload ): Boolean
15+ upload (file : Upload ): UploadedFile
916}
You can’t perform that action at this time.
0 commit comments