Skip to content
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

Uploading files. Change variables map #1081

Closed
Closed
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
33 changes: 16 additions & 17 deletions Sources/Apollo/RequestCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,22 @@ extension RequestCreator {

// Make sure all fields for files are set to null, or the server won't look
// for the files in the rest of the form data
let fieldsForFiles = Set(files.map { $0.fieldName })
var fields = requestBody(for: operation, sendOperationIdentifiers: sendOperationIdentifiers)
var variables = fields["variables"] as? GraphQLMap ?? GraphQLMap()
for fieldName in fieldsForFiles {
if
let value = variables[fieldName],
let arrayValue = value as? [JSONEncodable] {
let updatedArray: [JSONEncodable?] = arrayValue.map { _ in nil }
variables.updateValue(updatedArray, forKey: fieldName)
var map = [String]()

for file in files {
let fieldName = file.fieldName
let variable = variables[fieldName]

if let values = variable as? [JSONEncodable?] {
values.compactMap { $0 }.enumerated().forEach { (index, _) in
map.append("variables.\(file.fieldName).\(index)")
}
let updatedArray: [JSONEncodable?] = values.map { _ in nil }
variables.updateValue(updatedArray, forKey: fieldName)
} else {
map.append("variables.\(file.fieldName)")
variables.updateValue(nil, forKey: fieldName)
}
}
Expand All @@ -124,17 +130,10 @@ extension RequestCreator {
let operationData = try serializationFormat.serialize(value: fields)
formData.appendPart(data: operationData, name: "operations")

var map = [String: [String]]()
if files.count == 1 {
let firstFile = files.first!
map["0"] = ["variables.\(firstFile.fieldName)"]
} else {
for (index, file) in files.enumerated() {
map["\(index)"] = ["variables.\(file.fieldName).\(index)"]
}
}
// Map field name variables array ["variables.fieldName"] to JSONEncodable dictionary ["0" : "variables.fieldName"]
let value = Dictionary(uniqueKeysWithValues: map.enumerated().map { ("\($0.0)", [$0.1]) })

let mapData = try serializationFormat.serialize(value: map)
let mapData = try serializationFormat.serialize(value: value)
formData.appendPart(data: mapData, name: "map")

for (index, file) in files.enumerated() {
Expand Down