Module: Legion::API::Routes::GraphQL

Defined in:
lib/legion/api/graphql.rb

Class Method Summary collapse

Class Method Details

.graphiql_htmlObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/api/graphql.rb', line 53

def self.graphiql_html
  <<~HTML
    <!DOCTYPE html>
    <html>
      <head>
        <title>LegionIO GraphiQL</title>
        <link href="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.css" rel="stylesheet" />
      </head>
      <body style="margin:0">
        <div id="graphiql" style="height:100vh"></div>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react@18/umd/react.development.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.development.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/graphiql@3/graphiql.min.js"></script>
        <script>
          const root = ReactDOM.createRoot(document.getElementById('graphiql'));
          root.render(React.createElement(GraphiQL, {
            fetcher: GraphiQL.createFetcher({ url: '/api/graphql' })
          }));
        </script>
      </body>
    </html>
  HTML
end

.registered(app) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/api/graphql.rb', line 11

def self.registered(app)
  app.post '/api/graphql' do
    content_type :json
    Legion::Logging.debug "API: POST /api/graphql params=#{params.keys}" if defined?(Legion::Logging)

    body_str = request.body.read
    payload  = body_str.empty? ? {} : Legion::JSON.load(body_str)
    payload  = payload.transform_keys(&:to_sym) if payload.is_a?(Hash)

    query          = payload[:query]
    variables      = payload[:variables] || {}
    operation_name = payload[:operationName]

    if query.nil? || query.strip.empty?
      Legion::Logging.warn 'API POST /api/graphql returned 400: query is required' if defined?(Legion::Logging)
      status 400
      next Legion::JSON.dump({
                               errors: [{ message: 'query is required' }]
                             })
    end

    result = Legion::API::GraphQL::Schema.execute(
      query,
      variables:      variables,
      operation_name: operation_name,
      context:        { request: request }
    )

    status 200
    Legion::JSON.dump(result.to_h)
  rescue StandardError => e
    Legion::Logging.error "API POST /api/graphql: #{e.class}#{e.message}" if defined?(Legion::Logging)
    status 500
    Legion::JSON.dump({ errors: [{ message: e.message }] })
  end

  app.get '/api/graphql' do
    content_type 'text/html'
    Legion::API::Routes::GraphQL.graphiql_html
  end
end