-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from samesystem/allow_to_send_files
Allow sending files using multipart requests
- Loading branch information
Showing
17 changed files
with
531 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
lib/active_graphql/client/actions/action/format_variable_inputs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveGraphql | ||
class Client | ||
module Actions | ||
class Action | ||
# converts ruby object in to varbiable stype grapqhl input | ||
class FormatVariableInputs | ||
include VariableDetectable | ||
|
||
def initialize(inputs) | ||
@initial_inputs = inputs | ||
end | ||
|
||
def call | ||
return '' if inputs.empty? | ||
|
||
formatted_attributes(inputs) | ||
end | ||
|
||
private | ||
|
||
attr_reader :initial_inputs | ||
|
||
def formatted_attributes(attributes) | ||
attributes = attributes.dup | ||
formatted_attributes = attributes.map do |key, val| | ||
formatted_key_and_type(key, val) | ||
end | ||
|
||
formatted_attributes.join(', ') | ||
end | ||
|
||
def inputs | ||
@inputs ||= variable_attributes(initial_inputs) | ||
end | ||
|
||
def formatted_key_and_type(key, value) | ||
"$#{key}: #{formatted_type(value)}" | ||
end | ||
|
||
def formatted_type(value) | ||
if value.is_a?(Array) | ||
'[File!]!' | ||
else | ||
'File!' | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveGraphql | ||
class Client | ||
module Actions | ||
# handles all action details which are specific for query type request | ||
module VariableDetectable | ||
def variable_attributes(attributes) | ||
variables_or_nil = attributes.transform_values do |value| | ||
if value.is_a?(Hash) | ||
variable_attributes(value) | ||
elsif variable_value?(value) | ||
value | ||
elsif value.is_a?(Array) | ||
variable_attributes(value.map.with_index { |val, i| [i, val] }.to_h) | ||
end | ||
end | ||
|
||
flatten_keys(variables_or_nil).select { |_, val| val.present? } | ||
end | ||
|
||
def variable_value?(value) | ||
kind_of_file?(value) || (value.is_a?(Array) && kind_of_file?(value.first)) | ||
end | ||
|
||
private | ||
|
||
def flatten_keys(attributes, parent_key: nil) | ||
flattened = {} | ||
attributes.each do |key, value| | ||
full_key = [parent_key, key].compact.join('_').to_sym | ||
if value.is_a?(Hash) | ||
flattened.merge!(flatten_keys(value, parent_key: full_key)) | ||
else | ||
flattened[full_key] = value | ||
end | ||
end | ||
flattened | ||
end | ||
|
||
def kind_of_file?(value) | ||
value.is_a?(File) | ||
end | ||
end | ||
end | ||
end | ||
end |
61 changes: 61 additions & 0 deletions
61
lib/active_graphql/client/adapters/format_multipart_variables.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'faraday' | ||
require 'mime/types' | ||
require 'active_graphql/errors' | ||
|
||
module ActiveGraphql | ||
class Client | ||
module Adapters | ||
class NoMimeTypeException < ActiveGraphql::Errors::Error; end | ||
|
||
# Converts deeply nested File instances to Faraday::UploadIO | ||
class FormatMultipartVariables | ||
def initialize(variables) | ||
@variables = variables | ||
end | ||
|
||
def call | ||
deep_transform_values(variables) do |variable| | ||
variable_value(variable) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :variables | ||
|
||
def deep_transform_values(hash, &block) | ||
return hash unless hash.is_a?(Hash) | ||
|
||
hash.transform_values do |val| | ||
if val.is_a?(Hash) | ||
deep_transform_values(val, &block) | ||
else | ||
yield(val) | ||
end | ||
end | ||
end | ||
|
||
def variable_value(variable) | ||
if variable.is_a?(Array) | ||
variable.map { |it| variable_value(it) } | ||
elsif variable.is_a?(Hash) | ||
variable.transform_values { |it| variable_value(it) } | ||
elsif variable.is_a?(File) | ||
file_variable_value(variable) | ||
else | ||
variable | ||
end | ||
end | ||
|
||
def file_variable_value(file) | ||
content_type = MIME::Types.type_for(file.path).first | ||
return Faraday::UploadIO.new(file.path, content_type) if content_type | ||
|
||
raise NoMimeTypeException, "Unable to determine mime type for #{file.path}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.