Class: Graphql::PersistedDocuments::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/persisted_documents/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



4
5
6
# File 'lib/graphql/persisted_documents/rack.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql/persisted_documents/rack.rb', line 8

def call(env)
  if env['PATH_INFO'] == PersistedDocuments.configuration.path
    request = ::Rack::Request.new(env)
    document = request.params['document']

    unless document
      return error_response(422, ['Missing required argument document'])
    end

    persister = Persister.new(document)
    document_id = persister.persist!

    [
      '200',
      {'Content-Type' => 'application/json'},
      [ { document_id: document_id }.to_json ]
    ]
  else
    @app.call(env)
  end
rescue Persister::ParseError
  error_response(422, ['Unable to parse document'])
rescue Persister::InvalidDocument
  error_response(422, persister.errors)
rescue Persister::MissingPersistValidatedDocument
  error_response(400, ['persist_validated_document must be defined in config'])
end