Skip to content

Commit fe0d847

Browse files
add more documentation about uploading things
1 parent 35390bb commit fe0d847

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

docs/source/mutations.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ To upload a file, you will need:
101101
Here is an example of a GraphQL query for a mutation that accepts a single upload, and then returns the `id` for that upload:
102102

103103
```graphql
104-
mutation UploadFile($upload:Upload!) {
105-
singleUpload(file:$upload) {
104+
mutation UploadFile($file:Upload!) {
105+
singleUpload(file:$file) {
106106
id
107107
}
108108
}
@@ -115,7 +115,7 @@ If you wanted to use this to upload a file called `a.txt`, it would look somethi
115115
guard
116116
let fileURL = Bundle.main.url(forResource: "a",
117117
withExtension: "txt"),
118-
let file = GraphQLFile(fieldName: "upload",
118+
let file = GraphQLFile(fieldName: "file",
119119
originalName: "a.txt",
120120
mimeType: "text/plain", // <-defaults to "application/octet-stream"
121121
fileURL: fileURL) else {
@@ -124,7 +124,7 @@ guard
124124
}
125125

126126
// Actually upload the file
127-
client.upload(operation: UploadFileMutation(uploads: ["a"]),
127+
client.upload(operation: UploadFileMutation(file: "a"), // <-- `Upload` is a custom scalar that's a `String` under the hood.
128128
files: [file]) { result in
129129
switch result {
130130
case .success(let graphQLResult):
@@ -133,4 +133,27 @@ client.upload(operation: UploadFileMutation(uploads: ["a"]),
133133
print("error: \(error)")
134134
}
135135
}
136-
```
136+
```
137+
138+
A few other notes:
139+
140+
- Due to some limitations around the spec, whatever the file is being added for should be at the root of your GraphQL query. So if you have something like:
141+
142+
```graphql
143+
mutation AvatarUpload($userID: GraphQLID!, $file: Upload!) {
144+
id
145+
}
146+
```
147+
148+
it will work, but if you have some kind of object encompassing both of those fields like this:
149+
150+
```graphql
151+
// Assumes AvatarObject(userID: GraphQLID, file: Upload) exists
152+
mutation AvatarUpload($avatarObject: AvatarObject!) {
153+
id
154+
}
155+
```
156+
157+
it will not. Generally you should be able to deconstruct upload objects to allow you to send the appropriate
158+
- If you are uploading an array of files, you need to use the same field name for each file. These will be updated at send time.
159+
- If you are uploading an array of files, the array of `String`s passed into the query must be the same number as the array of files.

0 commit comments

Comments
 (0)