Class: GraphQL::Client::Query

Inherits:
Node
  • Object
show all
Defined in:
lib/graphql/client/query.rb

Instance Attribute Summary

Attributes inherited from Node

#fragments, #node, #type

Class Method Summary collapse

Methods inherited from Node

#initialize, #new, scan_interpolated_fragments

Constructor Details

This class inherits a constructor from GraphQL::Client::Node

Class Method Details

.parse(str, schema: GraphQL::Client.schema) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graphql/client/query.rb', line 11

def self.parse(str, schema: GraphQL::Client.schema)
  str = str.strip
  str, fragments = scan_interpolated_fragments(str)

  if str.start_with?("query")
    doc = GraphQL.parse(str)
    doc = doc.inject_selection(GraphQL::Language::Nodes::Field.new(name: "__typename"))
    doc = doc.replace_fragment_spread(fragments)
    node = doc.definitions.first
  else
    raise ArgumentError, "expected string to be a query:\n#{str}"
  end

  query = new(node.deep_freeze, fragments.values).freeze
  query.node.validate!(schema: schema) if schema
  query
end