graphql-client 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cee45489051065d2c8d47c6f8c68c6b3c98d75ca
4
- data.tar.gz: e462bb5c2fb1997f8679bb0fea315f8abd751539
3
+ metadata.gz: 8a60948e4ebebe634045e72a530fccc7e7447b40
4
+ data.tar.gz: a1b16f7712be91dbe4bff628b4113b5d5d7ef39e
5
5
  SHA512:
6
- metadata.gz: 9f335b9446f04fe5e684218b1f0f377b16412ac4918065ac610b2abff61d00792a461131b1ab615cd6512ba611a8dde6929af9c1b229f4a36c713e7e0983a22d
7
- data.tar.gz: 428487913828790e402b3a3c51daceafe8c338ebabfd234d0570424c8af426bc54bfa6f8fbf306d365fd674174a5708936e8a350cce5fcdf5c6426f5d3f1bd37
6
+ metadata.gz: c69238f56d5a2a659e5400b669c4c6cf358ed8b967f8cb88e90c87c708d1cc3361fe4c1c6dcf34bdb94caff6fc87c24d964f5fecab38fc69987458964be63cec
7
+ data.tar.gz: a2422fbb6cd197535d4c4ba51e26235b2906e6b00f78db4b08bf551f2c0c680a5d0231987c8bffcb2b5451ad29c284c851581511c1760fe8c9015387bc1acc8a
@@ -60,8 +60,28 @@ module GraphQL
60
60
  end
61
61
 
62
62
  class OperationDefinition < Definition
63
+ def initialize(document:, **kargs)
64
+ @document = document
65
+ super(**kargs)
66
+ end
67
+
63
68
  # Public: Alias for definition name.
64
69
  alias_method :operation_name, :definition_name
70
+
71
+ # Public: Owner document of operation definition.
72
+ #
73
+ # Returns GraphQL::Language::Nodes::Document of all registered
74
+ # definitions.
75
+ attr_reader :document
76
+
77
+ # Public: Get document with only the definitions needed to perform this
78
+ # operation.
79
+ #
80
+ # Returns GraphQL::Language::Nodes::Document with one OperationDefinition
81
+ # and any FragmentDefinition dependencies.
82
+ def operation_document
83
+ @operation_document ||= Language::OperationSlice.slice(document, operation_name).deep_freeze
84
+ end
65
85
  end
66
86
 
67
87
  class FragmentDefinition < Definition
@@ -84,15 +104,12 @@ module GraphQL
84
104
 
85
105
  mutator = GraphQL::Language::Mutator.new(doc)
86
106
 
87
- # TODO: Make this __typename injection optional
88
- mutator.prepend_selection(GraphQL::Language::Nodes::Field.new(name: "__typename").deep_freeze)
89
-
90
107
  definitions, renames = {}, {}
91
108
  doc.definitions.each do |node|
92
109
  local_name = node.name
93
110
  definition = case node
94
111
  when Language::Nodes::OperationDefinition
95
- OperationDefinition.new(node: node)
112
+ OperationDefinition.new(document: self.document, node: node)
96
113
  when Language::Nodes::FragmentDefinition
97
114
  FragmentDefinition.new(node: node)
98
115
  end
@@ -120,10 +137,6 @@ module GraphQL
120
137
  @document
121
138
  end
122
139
 
123
- def document_slice(operation_name)
124
- @document_slices[operation_name] ||= Language::OperationSlice.slice(document, operation_name).deep_freeze
125
- end
126
-
127
140
  def validate!
128
141
  validator = StaticValidation::Validator.new(schema: @schema)
129
142
  query = Query.new(@schema, document: document)
@@ -106,7 +106,7 @@ module GraphQL
106
106
  when Hash
107
107
  new(obj)
108
108
  when QueryResult
109
- spreads = Set.new(obj.class.source_node.selections.select { |s| s.is_a?(GraphQL::Language::Nodes::FragmentSpread) }.map(&:name))
109
+ spreads = Set.new(self.spreads(obj.class.source_node).map(&:name))
110
110
 
111
111
  if !spreads.include?(self.source_node.name)
112
112
  message = "couldn't cast #{obj.inspect} to #{self.inspect}\n\n"
@@ -124,6 +124,20 @@ module GraphQL
124
124
  end
125
125
  end
126
126
 
127
+ # Internal
128
+ def self.spreads(node)
129
+ node.selections.flat_map do |selection|
130
+ case selection
131
+ when Language::Nodes::FragmentSpread
132
+ selection
133
+ when Language::Nodes::InlineFragment
134
+ spreads(selection)
135
+ else
136
+ []
137
+ end
138
+ end
139
+ end
140
+
127
141
  def self.new(obj)
128
142
  case obj
129
143
  when Hash
@@ -150,9 +164,8 @@ module GraphQL
150
164
  alias_method :to_h, :data
151
165
 
152
166
  def inspect
153
- ivars = (self.class.fields.keys - [:__typename]).map { |sym| "#{sym}=#{instance_variable_get("@#{sym}").inspect}" }
167
+ ivars = (self.class.fields.keys).map { |sym| "#{sym}=#{instance_variable_get("@#{sym}").inspect}" }
154
168
  buf = "#<#{self.class.name}"
155
- buf << " " << @__typename if @__typename
156
169
  buf << " " << ivars.join(" ") if ivars.any?
157
170
  buf << ">"
158
171
  buf
@@ -30,25 +30,6 @@ module GraphQL
30
30
 
31
31
  nil
32
32
  end
33
-
34
- def prepend_selection(selection)
35
- on_selections = -> (node, parent) {
36
- return if !node.selections.any?
37
- # TODO: Simplify if AbstractNode#eql? is implemented
38
- existing_selections = Set.new(node.selections.map { |s| s.respond_to?(:name) ? s.name : nil }.compact)
39
- selections_to_prepend = [selection].reject { |s| existing_selections.include?(s.name) }
40
- node.selections = selections_to_prepend + node.selections
41
- }
42
-
43
- visitor = Visitor.new(@document)
44
- visitor[Nodes::Field].leave << on_selections
45
- visitor[Nodes::FragmentDefinition].leave << on_selections
46
- visitor[Nodes::InlineFragment].leave << on_selections
47
- visitor[Nodes::OperationDefinition].leave << on_selections
48
- visitor.visit
49
-
50
- nil
51
- end
52
33
  end
53
34
  end
54
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub