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

GraphQL error: Variable "image" not used #9

Closed
srosset81 opened this issue Dec 21, 2016 · 3 comments
Closed

GraphQL error: Variable "image" not used #9

srosset81 opened this issue Dec 21, 2016 · 3 comments

Comments

@srosset81
Copy link

srosset81 commented Dec 21, 2016

I'm trying to upload a file like this:

const ATTACH_IMAGE = gql`
  mutation attachImage($entityID: Int!, $mappingName: String!, $image: UploadedFile) {
    attachImage(entityID:$entityID, mappingName:$mappingName) {
      __typename
      id
      url
    }
}`;

this.props.client.mutate({
   mutation: ATTACH_IMAGE,
   variables: {
      image: this.state.uploadedFile,
      entityID: 3,
      mappingName: "organization_background"
   }

But I get this error message:

GraphQL error: Variable "image" not used

It seems that Apollo checks the mutation and notices that the $image variable is not used anywhere, even though I need to set it if I want the apollo-upload-network-interface to take care of it. I already had this problem when I did a custom implementation of a network interface, and the only way I found is to manually remove the $image: UploadedFile bit from the mutation string, not a great solution!

Any idea of what might go wrong? Do you have a working example somewhere?

@HriBB
Copy link
Owner

HriBB commented Dec 23, 2016

Hey @srosset81, I think it has something to do with #3. Try passing FileList instead of a single File. I will try to fix this after NY, but ATM I have no time to do this.

@srosset81
Copy link
Author

srosset81 commented Dec 24, 2016 via email

@srosset81
Copy link
Author

srosset81 commented Feb 1, 2017

Hello,

Actually just removing $image: UploadedFile from the query solved this problem.
As for handling File in addition to FileList, it is quite simple:

On the isUpload() function:

if (request.variables[key] instanceof FileList || request.variables[key] instanceof File) {
    return true
}

and on the getUploadOptions() function:

        for (let key in request.variables) {
            let v = request.variables[key]
            if (v instanceof FileList) {
                Array.from(v).forEach(f => body.append(key, f))
            } else if (v instanceof File) {
                body.append(key, v);
            } else {
                variables[key] = v
            }
        }

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

No branches or pull requests

2 participants