Class: GraphQL::Client
- Inherits:
-
Object
- Object
- GraphQL::Client
- Defined in:
- lib/graphql/client.rb,
lib/graphql/client/query_result.rb
Defined Under Namespace
Classes: Definition, Error, FragmentDefinition, OperationDefinition, QueryResult, ValidationError
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #document ⇒ Object
-
#initialize(schema:) ⇒ Client
constructor
A new instance of Client.
- #parse(str) ⇒ Object
- #validate! ⇒ Object
Constructor Details
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
14 15 16 |
# File 'lib/graphql/client.rb', line 14 def schema @schema end |
Instance Method Details
#document ⇒ Object
136 137 138 |
# File 'lib/graphql/client.rb', line 136 def document @document end |
#parse(str) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/graphql/client.rb', line 90 def parse(str) str = str.gsub(/\.\.\.([a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)+)/) { |m| const_name = $1 case fragment = ActiveSupport::Inflector.safe_constantize(const_name) when FragmentDefinition "...#{fragment.definition_name}" when nil raise NameError, "uninitialized constant #{const_name}\n#{str}" else raise TypeError, "expected #{const_name} to be a #{FragmentDefinition}, but was a #{fragment.class}" end } doc = GraphQL::Relay::Parser.parse(str) mutator = GraphQL::Language::Mutator.new(doc) definitions, renames = {}, {} doc.definitions.each do |node| local_name = node.name definition = case node when Language::Nodes::OperationDefinition OperationDefinition.new(document: self.document, node: node) when Language::Nodes::FragmentDefinition FragmentDefinition.new(node: node) end definitions[local_name] = definition renames[local_name] = -> { definition.definition_name } end mutator.rename_definitions(renames) doc.deep_freeze self.document.definitions.concat(doc.definitions) if definitions[nil] definitions[nil] else Module.new do definitions.each do |name, definition| const_set(name, definition) end end end end |
#validate! ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/graphql/client.rb', line 140 def validate! validator = StaticValidation::Validator.new(schema: @schema) query = Query.new(@schema, document: document) validator.validate(query).fetch(:errors).each do |error| raise ValidationError, error["message"] end nil end |