graphql-client 0.12.2 → 0.12.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00141a0b18656c8a3134bf9b441598ce97eeb1ae
4
- data.tar.gz: 749d8bc7f60581177c4c7ea012b6d515180a87e3
3
+ metadata.gz: 532760d69d3380633bfa7e7555697aa843b72fa7
4
+ data.tar.gz: 947e81aeea7eb9058c01b844425e4035a955a003
5
5
  SHA512:
6
- metadata.gz: ea43f83a968fa438728cb3316f220a4bc58d206b56af5e2dbaee8709b9717ac6e744594e25e69db1bd1250409cdd8021d503e42f287b5dae97fdf70d51c6e37b
7
- data.tar.gz: 3dbab0f1dfd017a1a97aaff1410cbc9971211cf0d507dd6a9b31f19ede515de638e387fc792d6e2f8a4c613624ee867789b3daec4bff5137b021e515b1f44f5c
6
+ metadata.gz: 34f989f928690406e82004b449264785395ffc00c809e70784b53284889b8c4409f1bbc3226b1ae7ac62df69b1ca8d753871f44b2545746e2b775c0a0bc263e8
7
+ data.tar.gz: 7f354451883ab22eefea03dea49cfe1bc848b8a6419ad9f626881a7dece1ec6922b3154a89561d5aa8ada72e9d919ecb24c529b4d13669c28ec63a7ab47d166e
data/README.md CHANGED
@@ -15,7 +15,12 @@ require "graphql/client/http"
15
15
  # Star Wars API example wrapper
16
16
  module SWAPI
17
17
  # Configure GraphQL endpoint using the basic HTTP network adapter.
18
- HTTP = GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/")
18
+ HTTP = GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/") do
19
+ def headers(context)
20
+ # Optionally set any HTTP headers
21
+ { "User-Agent": "My Client" }
22
+ end
23
+ end
19
24
 
20
25
  # Fetch latest schema on init, this will make a network request
21
26
  Schema = GraphQL::Client.load_schema(HTTP)
@@ -46,6 +51,17 @@ HeroNameQuery = SWAPI::Client.parse <<-'GRAPHQL'
46
51
  }
47
52
  GRAPHQL
48
53
  ```
54
+ Queries can reference variables that are passed in at query execution time.
55
+
56
+ ```ruby
57
+ HeroFromEpisodeQuery = SWAPI::Client.parse <<-'GRAPHQL'
58
+ query($episode: Episode) {
59
+ hero(episode: $episode) {
60
+ name
61
+ }
62
+ }
63
+ GRAPHQL
64
+ ```
49
65
 
50
66
  Fragments are declared similarly.
51
67
 
@@ -105,7 +121,11 @@ result = SWAPI::Client.query(Hero::Query)
105
121
  # The wrapped result allows to you access data with Ruby methods
106
122
  result.data.luke.home_planet
107
123
  ```
124
+ `GraphQL::Client#query` also accepts variables and context parameters that can be leveraged by the underlying network executor.
108
125
 
126
+ ``` ruby
127
+ result = SWAPI::Client.query(Hero::HeroFromEpisodeQuery, variables: {episode: "JEDI"}, context: {user_id: current_user_id})
128
+ ```
109
129
  ### Rails ERB integration
110
130
 
111
131
  If you're using Ruby on Rails ERB templates, theres a ERB extension that allows static queries to be defined in the template itself.
@@ -57,7 +57,13 @@ module GraphQL
57
57
  load_schema(JSON.parse(schema))
58
58
  end
59
59
  else
60
- load_schema(dump_schema(schema)) if schema.respond_to?(:execute)
60
+ if schema.respond_to?(:execute)
61
+ load_schema(dump_schema(schema))
62
+ elsif schema.respond_to?(:to_h)
63
+ load_schema(schema.to_h)
64
+ else
65
+ nil
66
+ end
61
67
  end
62
68
  end
63
69
 
@@ -73,7 +79,7 @@ module GraphQL
73
79
  operation_name: "IntrospectionQuery",
74
80
  variables: {},
75
81
  context: {}
76
- )
82
+ ).to_h
77
83
 
78
84
  if io
79
85
  io = File.open(io, "w") if io.is_a?(String)
@@ -197,17 +203,11 @@ module GraphQL
197
203
  definitions[node.name] = definition
198
204
  end
199
205
 
200
- rename_node = ->(node, _parent) do
201
- definition = definitions[node.name]
202
- if definition
203
- node.extend(LazyName)
204
- node.name = -> { definition.definition_name }
205
- end
206
- end
206
+ name_hook = RenameNodeHook.new(definitions)
207
207
  visitor = Language::Visitor.new(doc)
208
- visitor[Language::Nodes::FragmentDefinition].leave << rename_node
209
- visitor[Language::Nodes::OperationDefinition].leave << rename_node
210
- visitor[Language::Nodes::FragmentSpread].leave << rename_node
208
+ visitor[Language::Nodes::FragmentDefinition].leave << name_hook.method(:rename_node)
209
+ visitor[Language::Nodes::OperationDefinition].leave << name_hook.method(:rename_node)
210
+ visitor[Language::Nodes::FragmentSpread].leave << name_hook.method(:rename_node)
211
211
  visitor.visit
212
212
 
213
213
  doc.deep_freeze
@@ -225,6 +225,21 @@ module GraphQL
225
225
  end
226
226
  end
227
227
 
228
+ class RenameNodeHook
229
+ def initialize(definitions)
230
+ @definitions = definitions
231
+ end
232
+
233
+ def rename_node(node, _parent)
234
+ definition = @definitions[node.name]
235
+ if definition
236
+ node.extend(LazyName)
237
+ node.name = -> { definition.definition_name }
238
+ end
239
+ end
240
+ end
241
+
242
+
228
243
  # Public: Create operation definition from a fragment definition.
229
244
  #
230
245
  # Automatically determines operation variable set.
@@ -28,9 +28,9 @@ module GraphQL
28
28
  def initialize(client:, document:, irep_node:, source_location:)
29
29
  @client = client
30
30
  @document = document
31
- @definition_irep_node = irep_node
31
+ @definition_node = irep_node.ast_node
32
32
  @source_location = source_location
33
- @schema_class = client.types.define_class(self, definition_irep_node, definition_irep_node.return_type)
33
+ @schema_class = client.types.define_class(self, irep_node, irep_node.return_type)
34
34
  end
35
35
 
36
36
  # Internal: Get associated owner GraphQL::Client instance.
@@ -41,18 +41,11 @@ module GraphQL
41
41
  # GraphQL::Client::Schema::PossibleTypes.
42
42
  attr_reader :schema_class
43
43
 
44
- # Internal: Get underlying IRep Node for the definition.
45
- #
46
- # Returns GraphQL::InternalRepresentation::Node object.
47
- attr_reader :definition_irep_node
48
-
49
44
  # Internal: Get underlying operation or fragment defintion AST node for
50
45
  # definition.
51
46
  #
52
47
  # Returns OperationDefinition or FragmentDefinition object.
53
- def definition_node
54
- definition_irep_node.ast_node
55
- end
48
+ attr_reader :definition_node
56
49
 
57
50
  # Public: Global name of definition in client document.
58
51
  #
@@ -10,16 +10,10 @@ module GraphQL
10
10
  #
11
11
  # Returns self Node.
12
12
  def deep_freeze
13
- self.class.child_attributes.each do |attr_name|
14
- public_send(attr_name).freeze.each(&:deep_freeze)
15
- end
16
-
17
- self.class.scalar_attributes.each do |attr_name|
18
- object = public_send(attr_name)
19
- object.freeze if object
20
- end
21
-
13
+ children.each(&:deep_freeze)
14
+ scalars.each { |s| s && s.freeze }
22
15
  freeze
16
+ self
23
17
  end
24
18
  end
25
19
  end
@@ -9,6 +9,11 @@ module RuboCop
9
9
  module GraphQL
10
10
  # Public: Rubocop for catching overfetched fields in ERB templates.
11
11
  class Overfetch < Cop
12
+ if defined?(RangeHelp)
13
+ # rubocop 0.53 moved the #source_range method into this module
14
+ include RangeHelp
15
+ end
16
+
12
17
  def_node_search :send_methods, "({send csend block_pass} ...)"
13
18
 
14
19
  def investigate(processed_source)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-24 00:00:00.000000000 Z
11
+ date: 2018-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  version: '0'
212
212
  requirements: []
213
213
  rubyforge_project:
214
- rubygems_version: 2.6.12
214
+ rubygems_version: 2.6.11
215
215
  signing_key:
216
216
  specification_version: 4
217
217
  summary: GraphQL Client