Class: GraphQL::Stitching::HttpExecutable

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/http_executable.rb

Instance Method Summary collapse

Constructor Details

#initialize(url:, headers: {}, upload_types: nil) ⇒ HttpExecutable

Returns a new instance of HttpExecutable.



10
11
12
13
14
# File 'lib/graphql/stitching/http_executable.rb', line 10

def initialize(url:, headers:{}, upload_types: nil)
  @url = url
  @headers = { "Content-Type" => "application/json" }.merge!(headers)
  @upload_types = upload_types
end

Instance Method Details

#call(request, document, variables) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/stitching/http_executable.rb', line 16

def call(request, document, variables)
  multipart_form = if request.variable_definitions.any? && variables&.any?
    extract_multipart_form(request, document, variables)
  end

  response = if multipart_form
    post_multipart(multipart_form)
  else
    post(document, variables)
  end

  JSON.parse(response.body)
end

#extract_multipart_form(request, document, variables) ⇒ Object

extract multipart upload forms spec: github.com/jaydenseric/graphql-multipart-request-spec



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/graphql/stitching/http_executable.rb', line 53

def extract_multipart_form(request, document, variables)
  return unless @upload_types

  path = []
  files_by_path = {}

  # extract all upload scalar values mapped by their input path
  variables.each do |key, value|
    ast_node = request.variable_definitions[key]
    path << key
    extract_ast_node(ast_node, value, files_by_path, path, request) if ast_node
    path.pop
  end

  return if files_by_path.none?

  map = {}
  files = files_by_path.values.tap(&:uniq!)
  variables_copy = variables.dup

  files_by_path.keys.each do |path|
    orig = variables
    copy = variables_copy
    path.each_with_index do |key, i|
      if i == path.length - 1
        map_key = files.index(copy[key]).to_s
        map[map_key] ||= []
        map[map_key] << "variables.#{path.join(".")}"
        copy[key] = nil
      elsif orig[key].object_id == copy[key].object_id
        copy[key] = copy[key].dup
      end
      orig = orig[key]
      copy = copy[key]
    end
  end

  form = {
    "operations" => JSON.generate({
      "query" => document,
      "variables" => variables_copy,
    }),
    "map" => JSON.generate(map),
  }

  files.each_with_object(form).with_index do |(file, memo), index|
    memo[index.to_s] = file.respond_to?(:tempfile) ? file.tempfile : file
  end
end

#post(document, variables) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/graphql/stitching/http_executable.rb', line 30

def post(document, variables)
  Net::HTTP.post(
    URI(@url),
    JSON.generate({ "query" => document, "variables" => variables }),
    @headers,
  )
end

#post_multipart(form_data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/stitching/http_executable.rb', line 38

def post_multipart(form_data)
  uri = URI(@url)
  req = Net::HTTP::Post.new(uri)
  @headers.each_pair do |key, value|
    req[key] = value
  end

  req.set_form(form_data.to_a, "multipart/form-data")
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(req)
  end
end