Class: Graphlyte::Document

Inherits:
Data
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphlyte/document.rb

Overview

The representation of a GraphQL document.

Documents can have multiple definitions, which can be queries, mutations, subscriptions (operations) or fragments.

During execution, only one operation can be executed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Data

attr_accessor, attr_reader, attributes, #dup, #hash, #inspect

Constructor Details

#initialize(**kwargs) ⇒ Document

Returns a new instance of Document.



26
27
28
29
30
31
# File 'lib/graphlyte/document.rb', line 26

def initialize(**kwargs)
  super
  @definitions ||= []
  @variables ||= {}
  @var_name_counter = @variables.size + 1
end

Instance Attribute Details

#definitionsObject

Returns the value of attribute definitions.



22
23
24
# File 'lib/graphlyte/document.rb', line 22

def definitions
  @definitions
end

#schemaObject

Returns the value of attribute schema.



22
23
24
# File 'lib/graphlyte/document.rb', line 22

def schema
  @schema
end

#variablesObject

Returns the value of attribute variables.



22
23
24
# File 'lib/graphlyte/document.rb', line 22

def variables
  @variables
end

Instance Method Details

#+(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphlyte/document.rb', line 33

def +(other)
  return dup unless other

  other = other.dup
  doc = dup

  defs = doc.definitions + other.definitions
  vars = doc.variables.merge(other.variables) # TODO: detect conflicts?

  self.class.new(definitions: defs, vars: vars)
end

#add_fragments(frags) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/graphlyte/document.rb', line 55

def add_fragments(frags)
  current = fragments

  frags.each do |frag|
    @definitions << frag unless current[frag.name]
  end
end

#declare(var) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphlyte/document.rb', line 63

def declare(var)
  if var.name.nil?
    var.name = "var#{@var_name_counter}"
    @var_name_counter += 1
  end

  parser = Graphlyte::Parser.new(tokens: Graphlyte::Lexer.lex(var.type))
  parsed_type = parser.type_name! if var.type
  current_def = @variables[var.name]

  if current_def && current_def.type != parsed_type
    msg = "Cannot re-declare #{var.name} at different types. #{current_def.type} != #{var.type}"
    raise ArgumentError, msg
  end

  @variables[var.name] ||= Graphlyte::Syntax::VariableDefinition.new(
    variable: var.name,
    type: parsed_type
  )

  Syntax::VariableReference.new(var.name, parsed_type)
end

#define(dfn) ⇒ Object



51
52
53
# File 'lib/graphlyte/document.rb', line 51

def define(dfn)
  @definitions << dfn
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


45
46
47
# File 'lib/graphlyte/document.rb', line 45

def eql?(other)
  other.is_a?(self.class) && other.fragments == fragments && other.operations == operations
end

#executable?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/graphlyte/document.rb', line 94

def executable?
  @definitions.all?(&:executable?)
end

#fragmentsObject



86
87
88
# File 'lib/graphlyte/document.rb', line 86

def fragments
  definitions.select { _1.is_a?(Graphlyte::Syntax::Fragment) }.to_h { [_1.name, _1] }
end

#operationsObject



90
91
92
# File 'lib/graphlyte/document.rb', line 90

def operations
  @definitions.select { _1.is_a?(Graphlyte::Syntax::Operation) }.to_h { [_1.name, _1] }
end

#request_body(operation = nil, **variables) ⇒ Object

Return this document as a JSON request body, suitable for posting to a server.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graphlyte/document.rb', line 111

def request_body(operation = nil, **variables)
  if operation.nil? && operations.size != 1
    raise ArgumentError, 'Operation name is required when the document contains multiple operations'
  end

  variables.transform_keys! { _1.to_s.camelize }

  doc = Editors::WithVariables.new(schema, operation, variables).edit(dup)

  {
    query: doc.to_s,
    variables: variables,
    operation: operation
  }.compact.to_json
end

#to_sObject



98
99
100
101
102
103
# File 'lib/graphlyte/document.rb', line 98

def to_s
  buff = []
  write(buff)

  buff.join
end

#variable_referencesObject



127
128
129
# File 'lib/graphlyte/document.rb', line 127

def variable_references
  Editors::CollectVariableReferences.new.edit(self)[Syntax::Operation]
end

#write(io) ⇒ Object

More efficient for writing to files or streams - avoids building up the full string.



106
107
108
# File 'lib/graphlyte/document.rb', line 106

def write(io)
  Graphlyte::Serializer.new(io).dump_definitions(definitions)
end