Class: BulkImports::Clients::Graphql

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_imports/clients/graphql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: Gitlab::Saas.com_url, token: nil) ⇒ Graphql

Returns a new instance of Graphql.


30
31
32
33
34
35
# File 'lib/bulk_imports/clients/graphql.rb', line 30

def initialize(url: Gitlab::Saas.com_url, token: nil)
  @url = Gitlab::Utils.append_path(url, '/api/graphql')
  @token = token
  @client = Graphlient::Client.new(@url, options(http: HTTP))
  @compatible_instance_version = false
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.


26
27
28
# File 'lib/bulk_imports/clients/graphql.rb', line 26

def client
  @client
end

Instance Method Details

#execute(*args) ⇒ Object


37
38
39
40
41
# File 'lib/bulk_imports/clients/graphql.rb', line 37

def execute(*args)
  validate_instance_version!

  client.execute(*args)
end

#options(extra = {}) ⇒ Object


43
44
45
46
47
48
49
50
51
52
# File 'lib/bulk_imports/clients/graphql.rb', line 43

def options(extra = {})
  return extra unless @token

  {
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@token}"
    }
  }.merge(extra)
end

#validate_instance_version!Object


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bulk_imports/clients/graphql.rb', line 54

def validate_instance_version!
  return if @compatible_instance_version

  response = client.execute('{ metadata { version } }')
  version = Gitlab::VersionInfo.parse(response.data..version)

  if version.major < BulkImport::MIN_MAJOR_VERSION
    raise ::BulkImports::Error.unsupported_gitlab_version
  else
    @compatible_instance_version = true
  end
end