Sha256: 65e3f8a9d0656be631d558ba8442d235204f905c10c011961bed3f316ad1f6ba

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

#!/usr/bin/env ruby

require 'bundler/setup'
Bundler.require

require 'irb'
require 'irb/completion'
require 'optparse'
require 'net/http'
require_relative '../lib/graphql_client'

JSON_MIME_TYPE = 'application/json'.freeze
DEFAULT_HEADERS = { 'Accept' => JSON_MIME_TYPE, 'Content-Type' => JSON_MIME_TYPE }

options = {}

op = OptionParser.new do |opts|
  opts.banner = 'Usage: bin/graphql-client <url> [options]'

  opts.on('-u', '--username [USERNAME]', 'HTTP Basic Auth Username') do |arg|
    options[:username] = arg
  end

  opts.on('-p', '--password [PASSWORD]', 'HTTP Basic Auth Password') do |arg|
    options[:password] = arg
  end

  opts.on('-h', '--headers [HEADERS]', Array, 'Comma separated string of headers (eg: -h"X-TOKEN=token,X-FOO=bar")') do |arg|
    options[:headers] = arg.map { |header| header.split('=') }.to_h
  end

  opts.on('-v', '--verbose', 'Verbose mode: show debugging info') do |arg|
    options[:verbose] = arg
  end
end

op.parse!

if ARGV.empty?
  puts op.help
  exit(-1)
end

uri = URI.parse(ARGV.pop)

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
  headers = DEFAULT_HEADERS.merge(options[:headers].to_h)

  request = Net::HTTP::Post.new(uri, headers).tap do |req|
    if options[:username] || options[:password]
      req.basic_auth(options[:username], options[:password])
    end

    req.body = { query: GraphQL::Client::INTROSPECTION_QUERY }.to_json
  end

  http.request(request)
end

case response
when Net::HTTPOK then
  schema = JSON.parse(response.body)
  config = GraphQL::Client::Config.new(options)
else
  abort("Error fetching the schema: server responded with #{response.code}")
end

client = GraphQL::Client.new(schema, config: config) do
  configure do |c|
    c.url = uri
  end
end

IRB.setup nil
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context

require 'irb/ext/multi-irb'
IRB.irb nil, client

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql_client-0.4.1 bin/graphql-client
graphql_client-0.3.3 bin/graphql-client