Module: ElasticGraph::GraphiQL

Defined in:
elasticgraph-graphiql/lib/elastic_graph/graphiql.rb

Overview

A Rack application that serves both an ElasticGraph GraphQL endpoint and a GraphiQL IDE. This can be used for local development, mounted in a Rails application, or run in any other Rack-compatible context.

Examples:

Simple config.ru to serve an ElasticGraph GraphiQL IDE

require "elastic_graph/graphql"
require "elastic_graph/graphiql"

graphql = ElasticGraph::GraphQL.from_yaml_file("config/settings/development.yaml")
run ElasticGraph::GraphiQL.new(graphql)

Class Method Summary collapse

Class Method Details

.new(graphql, output: $stdout) ⇒ Rack::Builder

Builds a Rack application that serves both an ElasticGraph GraphQL endpoint and a GraphiQL IDE.

Parameters:

  • graphql (ElasticGraph::GraphQL)

    ElasticGraph GraphQL instance

Returns:

  • (Rack::Builder)

    built Rack application



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'elasticgraph-graphiql/lib/elastic_graph/graphiql.rb', line 33

def self.new(graphql, output: $stdout)
  tarball_path = ::File.join(__dir__.to_s, "graphiql/assets.tar.gz")
  static_content_root = ::Dir.mktmpdir("elasticgraph_graphiql")
  output.puts "Extracting GraphiQL assets from #{tarball_path} to #{static_content_root}..."

  tar_command = "tar -xzf #{::Shellwords.escape(tarball_path)} -C #{::Shellwords.escape(static_content_root)}"
  tar_output, status = ::Open3.capture2e(tar_command)

  unless status.success?
    error_message = "Failed to extract GraphiQL assets from #{tarball_path}.\n"
    error_message += "Command: '#{tar_command}'\n"
    error_message += "Exit Status: #{status.exitstatus}\n"
    error_message += "Output: #{tar_output}"

    raise error_message
  end

  output.puts "GraphiQL assets extracted successfully to #{static_content_root}."
  graphql_endpoint = Rack::GraphQLEndpoint.new(graphql)

  ::Rack::Builder.new do
    # @type self: ::Rack::Builder
    use ::Rack::Static, urls: ["/assets", "/favicon.svg", "/monacoeditorwork"], root: static_content_root
    use ::Rack::Static, urls: {"/" => "index.html"}, root: static_content_root

    map "/graphql" do
      run graphql_endpoint
    end
  end
end