Class: Graphql::PersistedDocuments::Persister

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

Constant Summary collapse

InvalidDocumentError =
Class.new(StandardError)
MissingPersistValidatedDocumentError =
Class.new(StandardError)
ParseError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string) ⇒ Persister

Returns a new instance of Persister.



10
11
12
13
# File 'lib/graphql/persisted_documents/persister.rb', line 10

def initialize(query_string)
  @query_string = query_string
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/graphql/persisted_documents/persister.rb', line 8

def errors
  @errors
end

#query_stringObject (readonly)

Returns the value of attribute query_string.



8
9
10
# File 'lib/graphql/persisted_documents/persister.rb', line 8

def query_string
  @query_string
end

Instance Method Details

#persistObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/persisted_documents/persister.rb', line 15

def persist
  document = GraphQL.parse(query_string)
  query = GraphQL::Query.new(config.schema, document: document)
  perform_validation(query)

  raise InvalidDocumentError unless errors.empty?

  user_persister = config.persist_validated_document
  return user_persister.call(document) if user_persister

  raise MissingPersistValidatedDocumentError
rescue GraphQL::ParseError
  raise ParseError
end