Class: GraphQL::Client::Fragment

Inherits:
Node
  • Object
show all
Defined in:
lib/graphql/client/fragment.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
28
29
# File 'lib/graphql/client/fragment.rb', line 11

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

  if str.start_with?("fragment")
    str = str.sub(/^fragment on /, "fragment __anonymous__ on ")
    doc = GraphQL.parse(str)
    doc = doc.inject_selection(GraphQL::Language::Nodes::Field.new(name: "__typename"))
    doc = doc.replace_fragment_spread(fragments)
    fragment = doc.definitions.first
    node = GraphQL::Language::Nodes::InlineFragment.new(type: fragment.type, directives: fragment.directives, selections: fragment.selections)
  else
    raise ArgumentError, "expected string to be a fragment:\n#{str}"
  end

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