I have some problem when I work with apollo client. Please help me figure out it.
mutation Register($name: String!, $email: String!, $password: String!, $avatar: Upload) {
register(name: $name, email: $email, password: $password, avatar: $avatar) {
_id
name
email
}
}
And in file swift generate automatic by Apollo, I see avatar as type String. I don't understand that.
public init(name: String, email: String, password: String, company: String? = nil, description: String? = nil, birthday: String? = nil, address: String? = nil, avatar: String? = nil) {
self.name = name
self.email = email
self.password = password
self.company = company
self.description = description
self.birthday = birthday
self.address = address
self.avatar = avatar
}
I see in document have a method apollo.upload(file) but i can't see any method can upload and send data together. i need use UploadNetworkTransport to do that. is that right?
I work with authenticate server so I need to put access token into header but when I update to version 0.15.0 I can't put URLSessionConfig into HTTPNetworkTransporter (in 0.13.0 can). I must custom that class to put header it really complicated for newbie. So i want to know has another way to do that?
For number 3:
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = headers
let session = URLSession(configuration: config)
let transport = HTTPNetworkTransport(url: url, session: session, sendOperationIdentifiers: false)
@victormihaita
Thank you so much.
Thanks @victormihaita!
@vankien96 Regarding why it's typed as a String, it's a bit of a long story, but the short version is that an Upload is a custom scalar that eventually is String under the hood. That String eventually gets replaced with a multipart-form object under the hood.
Does that answer your questions?
@designatednerd
I solved my problem. Thank you for your support.
@vankien96 I have a very similar issue. Can you tell me how you reach the solution?
@designatednerd why not share your solution?
Hi @duffek I think you meant to ask @vankien96. If you're having issues similar to this on a current version of the SDK, please open a new issue. Thanks!
For anyone who finds this same thread and is banging their head against the wall because the Upload type is a scalar and issues understanding the upload.
Here is the mutation I am using:
mutation UploadPhotoFile($file: Upload!) {
photoUpload(file: $file) {
id
}
}
And my swift code. Make sure on your file that your: fieldName in the GraphQLFile() function matches the file param on your UploadPhotoFileMutation. If you set this up correctly you don't have to pass any file object over to the upload function.
guard let fileURL = Bundle.main.url(forResource: "picture", withExtension: "jpg") else { return }
let file = try? GraphQLFile(fieldName: "someString",
originalName: "picture",
mimeType: "image/jpeg",
fileURL: fileURL)
apollo.upload(operation: UploadPhotoFileMutation(file: "someString", files: [file!]) { result in
switch result {
case .success(let graphQLResult):
// handle result
case .failure(let error):
print("Error: ", error.localizedDescription)
}
}
Also note that if you bury the $file param inside a query you can still deconstruct it. Here is an example thats not pretty but works:
mutation UploadPhotoFile($file: Upload!, $title: String, $userId: Int!) {
photoFileCreate(
fileInput: {
file: $file,
title: $title
},
userId: $userId
) {
id
}
}
If you see an issue or have trouble, tag me in a comment and I'll respond. If something is wrong I'll happily correct it. I'm loving GraphQL and feel the Upload functionality docs are lacking in explanation and examples BUT they are very clear that a typical REST or other upload function is most likely preferred if you have the choice in it.
Most helpful comment
For number 3: