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

Image Uploading with apollo #938

Closed
DeekshaApptunix opened this issue Dec 9, 2019 · 15 comments
Closed

Image Uploading with apollo #938

DeekshaApptunix opened this issue Dec 9, 2019 · 15 comments

Comments

@DeekshaApptunix
Copy link

DeekshaApptunix commented Dec 9, 2019

I need to upload user image at signup time, Here is my code
let name = UUID().uuidString
if let imageData = userImgVew.image?.jpegData(compressionQuality: 0.7) {
let file = GraphQLFile(fieldName: "(name).png", originalName: "(name)", mimeType: "image/jpeg", data:imageData)

apollo.perform(mutation:UpdateUserProfileMutation.init(userId:id, userImage:file, userDetail: userDict))

I am getting following error :

Cannot convert value of type 'GraphQLFile' to expected argument type 'String?'

I also tried it with upload method
let name = UUID().uuidString
if let imageData = userImgVew.image?.jpegData(compressionQuality: 0.7) {
let file = GraphQLFile(fieldName: "(name).png", originalName: "(name)", mimeType: "image/jpeg", data:imageData)
let uploadData = UpdateUserProfileMutation(userId:id, userImage:file.fieldName, userDetail: userDict)
apollo.upload(operation:uploadData,files:[file]){ result in

I am getting following error :

createReadStream is not a function

@designatednerd
Copy link
Contributor

Hey @DeekshaApptunix - where are you receiving that error, in Swift or in an error returned from the server? If it's the latter, it sounds like something's not working properly on the server.

@designatednerd
Copy link
Contributor

Another thing to look out for is that fieldName needs to be the same as the field the file is being uploaded for - right now you're putting a file name in that, so your server may not be getting the file properly.

@designatednerd
Copy link
Contributor

(also you need to use the upload function and not the perform(mutation function, as uploading has special considerations not present for most mutations)

@DeekshaApptunix
Copy link
Author

I need to upload in this mutation

if let logoData = btnCommunity.currentImage?.jpegData(compressionQuality: 0.8) , let coverData = coverImage.image?.jpegData(compressionQuality: 0.8) {
let coverFile = GraphQLFile(fieldName:"file", originalName:"cover.png", mimeType: "image/jpeg", data:coverData)
let logoFile = GraphQLFile(fieldName:"logo", originalName:"logo.png", mimeType: "image/jpeg", data:logoData)
}

let uploadData = CreateCommunityMutation(communityDetail:communityDetail, invitedusers: invitedUser, file: "(file)", communityLogo: "(logo)")
apollo.upload(operation:uploadData, files:[]){ result in

Here is my mutation for the same

mutation createCommunity($communityDetail:CommunityInput!,$invitedusers:[ID],$file:Upload,$communityLogo:Upload){
createCommunity(community:$communityDetail , users:$invitedusers , file:$file , logo:$communityLogo ){
id
name
}
}

I am getting following error

createReadStream is not a function

@designatednerd
Copy link
Contributor

Hi Deesha, Where is the createReadStream is not a function coming from, is it from something within Swift or is it an error coming back from the server?

I would also caution that I don't think our current handling of multipart form uploads will allow more than a single file per operation to be uploaded, you'll likely need to break things out a bit.

@DeekshaApptunix
Copy link
Author

@designatednerd createReadStream is not a function is coming from backend. I need to send two different images in single mutation with different parameters.

createCommunity(community:CommunityInput! , users:[ID], file: Upload , logo: Upload ){
id
name
}

I am sending it like this:

if let logoData = btnCommunity.currentImage?.jpegData(compressionQuality: 0.8) , let coverData = coverImage.image?.jpegData(compressionQuality: 0.8) {
let coverFile = GraphQLFile(fieldName:"file", originalName:"cover.png", mimeType: "image/jpeg", data:coverData)
let logoFile = GraphQLFile(fieldName:"logo", originalName:"logo.png", mimeType: "image/jpeg", data:logoData)
}

let uploadData = CreateCommunityMutation(communityDetail:communityDetail, invitedusers: invitedUser, file: "(file)", communityLogo: "(logo)")
apollo.upload(operation:uploadData, files:[]){ result in

Now please let me know what I need to send in files array ?

@designatednerd
Copy link
Contributor

You need to send the created GraphQLFile objects in the array you pass to the upload function.

Based on what you're doing above, it looks like what you need to do is:

apollo.upload(operation:uploadData, files:[coverFile, logoFile]) { result in

@DeekshaApptunix
Copy link
Author

If I am sending data like you suggested then I am getting following error

Error while create user: Received error response (400 bad request):

<title>Error</title>
BadRequestError: Invalid object path for the ‘map’ multipart field entry key ‘0’ array index ‘0’ value ‘variables.file.0’ (https://github.com/jaydenseric/graphql-multipart-request-spec).
   at Busboy.parser.on (/home/apptunix/Documents/huntSwap/node_modules/graphql-upload/lib/processRequest.js:178:43)
   at emitMany (events.js:147:13)
   at Busboy.emit (events.js:224:7)
   at Busboy.emit (/home/apptunix/Documents/huntSwap/node_modules/busboy/lib/main.js:37:33)
   at PartStream.onEnd (/home/apptunix/Documents/huntSwap/node_modules/busboy/lib/types/multipart.js:262:15)
   at emitNone (events.js:111:20)
   at PartStream.emit (events.js:208:7)
   at Dicer.onPart (/home/apptunix/Documents/huntSwap/node_modules/busboy/l

@DeekshaApptunix
Copy link
Author

@designatednerd also let me know how to upload multiple images with graphql in swift 4

@designatednerd
Copy link
Contributor

Uploading is not supported in versions of the library which support Swift 4 - Uploading was added in v0.13.0, and Swift 5 has been the supported version since 0.11.0.

@designatednerd
Copy link
Contributor

Looks like your server is using this upload plugin - can you confirm that's the case, and if so, what version it's using? The current version is 9.0.0, and supports v2.0 of the GraphQL upload spec, which is what we support.

@designatednerd
Copy link
Contributor

@DeekshaApptunix Were you able to find out what version of the upload plugin your server team is using?

@designatednerd
Copy link
Contributor

I'm going to close this out, but @DeekshaApptunix please feel free to reopen if you get further information. Anyone else with a similar problem, please open a new issue. Thank you!

@ghost
Copy link

ghost commented Jun 20, 2020

@designatednerd Upload functionality is perfectly working for a single image. For multiple images, it's returning 400 error.

Can you help me with this?

@designatednerd
Copy link
Contributor

@MeriGarala Please see #1279 for what should be a solution to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants