graphql-client 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql/client.rb +21 -8
- data/lib/graphql/client/query_result.rb +16 -3
- data/lib/graphql/language/mutator.rb +0 -19
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a60948e4ebebe634045e72a530fccc7e7447b40
|
4
|
+
data.tar.gz: a1b16f7712be91dbe4bff628b4113b5d5d7ef39e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c69238f56d5a2a659e5400b669c4c6cf358ed8b967f8cb88e90c87c708d1cc3361fe4c1c6dcf34bdb94caff6fc87c24d964f5fecab38fc69987458964be63cec
|
7
|
+
data.tar.gz: a2422fbb6cd197535d4c4ba51e26235b2906e6b00f78db4b08bf551f2c0c680a5d0231987c8bffcb2b5451ad29c284c851581511c1760fe8c9015387bc1acc8a
|
data/lib/graphql/client.rb
CHANGED
@@ -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
|
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
|
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
|