Skip to content

New coerce_input class type check breaks tests #37

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

Open
mathieugagne opened this issue Sep 8, 2020 · 3 comments
Open

New coerce_input class type check breaks tests #37

mathieugagne opened this issue Sep 8, 2020 · 3 comments

Comments

@mathieugagne
Copy link

I'm trying to upgrade from 2.0.1 to 2.0.3. There was a check added in coerce_input that force the value to be of a certain type. My tests work fine prior to that.

Here's my working solution on 2.0.1

let(:query_string) do
  %|
    mutation($input: UpdateSurveyInput!) {
      updateSurvey(input: $input) {
        id
        clientLogoUrl
      }
    }
  |
end

let(:variables) do
  {
    input: {
      id: survey.id,
      attributes: {
        clientLogo: ActionDispatch::Http::UploadedFile.new(
          filename: "lolcat.png",
          type: "image/png",
          tempfile: File.open(Rails.root.join("spec", "data", "lolcat.png"), "rb")
        )
      }
    }
  }
end

Schema.execute(query, {}, variables)

After 2.0.3 I get the following error:

"#<ActionDispatch::Http::UploadedFile:0x0000562e5512a028 @tempfile=#<File:/home/circleci/project/spec/data/lolcat.png>, @original_filename=\"lolcat.png\", @content_type=\"image/png\", @headers=nil> is not a valid upload"

I tried wrapping my helper in your wrapper class like such:

ApolloUploadServer::Wrappers::UploadedFile.new(filename: "lolcat.png", type: "image/png", tempfile: File.open(Rails.root.join("spec", "data", "lolcat.png"), "rb"),

But it yields a different error:

NoMethodError: undefined method `original_filename' for #<Hash:0x000056250eab5ca0>

Willing to provide more information if needed. Hoping it's a quick fix on your end. I'm unsure why there's a is_a? class check. If it quacks like a duck, it should just be fine. Unsure about the as_json override either.

@mathieugagne mathieugagne changed the title coerce_input class type check breaks rspec file_upload helper method New coerce_input class type check breaks tests Sep 8, 2020
@fuelen
Copy link
Collaborator

fuelen commented Sep 8, 2020

If it quacks like a duck, it should just be fine

I agree, we have to think about other way of doing this. Maybe just check existence of some core methods for uploaded file object.

Or rollback this feature at all.

@hshimoyama
Copy link

[workaround] I tried to wrap ActionDispatch::Http::UploadedFile object as an argument (not Hash argment.) Then it worked fine.

before

query = "..."
variables = {
  file: fixture_file_upload("image.png")
}
Schema.execute(query, {}, variables)

after

query = "..."
variables = {
  file: ::ApolloUploadServer::Wrappers::UploadedFile.new(fixture_file_upload("image.png"))
}
Schema.execute(query, {}, variables)

@TheRusskiy
Copy link

if someone wants to actually use a file passed in variables to attach to ActiveStorage, I had to resolve to the following hack:

file = ::ApolloUploadServer::Wrappers::UploadedFile.new(Rack::Test::UploadedFile.new("<file_path>", 'image/png'))

allow(ApolloUploadServer::Upload).to receive(:coerce_input).and_return(file)

variables = {
  ...
  file: file
},

Schema.execute(query, {}, variables)

otherwise, it comes as an array

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

4 participants