graphlient 0.1.0 → 0.2.0

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: f73350b469913784302c46b7305cef2d8949ad32
4
- data.tar.gz: 91e1a47a61c402174bef0f15a0568e05d26b73cc
3
+ metadata.gz: cd0db2077a31028524926bedc3c7ac54c5f3e6eb
4
+ data.tar.gz: d78c92a97b670680fc056c0996c975ee7a9df80c
5
5
  SHA512:
6
- metadata.gz: aaadb8bf32399b399fae81c4ce62a3d73508317eb4a157f90c9bf1665a77f5d91116dc0e263a6d27a2d5dfad9eeca1ea730b3d81966d68d0519c65b2dd9cb3cc
7
- data.tar.gz: f50b03492b2e1c82b974fd5e59952e00cd38f82e992d5a2be84efdb4afa146a4ec65cf3370b4d77054a9de3094b7fb483453aea6f48655695c73e76bbbd83d59
6
+ metadata.gz: c00966fb20008a0b9524c46b24a249d4551aef3d8543855e923ce6f2c40b71fefa8cb68874fbc660e90d7c8fd196b8c1dc3376a2e641968f83de271629ce016d
7
+ data.tar.gz: 183d86c88b7344be098af445c96504dc866271cd1210b7d4e1b57da446dff1c919be8cebe79592c936792952d42257a1c23a9506442bdf1302e8e63b120c1501
@@ -1,3 +1,8 @@
1
+ ### 0.2.0 (11/09/2017)
2
+
3
+ * [#33](https://github.com/ashkan18/graphlient/pull/33): Added dsl for supporting parametrized queries/mutations - [@ashkan18](https://github.com/ashkan18).
4
+ * [#34](https://github.com/ashkan18/graphlient/issues/34): Fix: don't convert variables to `String` - [@dblock](https://github.com/dblock).
5
+
1
6
  ### 0.1.0 (10/27/2017)
2
7
 
3
8
  * [#31](https://github.com/ashkan18/graphlient/issues/31): Fix: catch execution errors that don't contain field names - [@dblock](https://github.com/dblock).
data/README.md CHANGED
@@ -144,21 +144,31 @@ With a block.
144
144
 
145
145
  ```ruby
146
146
  client.query(ids: [42]) do
147
- query(:$ids => :'[Int]') do
148
- invoices(ids: :$ids) do
147
+ query(ids: [:int]) do
148
+ invoices(ids: :ids) do
149
149
  id
150
150
  fee_in_cents
151
151
  end
152
152
  end
153
153
  end
154
154
  ```
155
+ Graphlient supports following Scalar types for parameterized queries by default:
156
+ - `:id` maps to `ID`
157
+ - `:boolean` maps to `Boolean`
158
+ - `:float` maps to `Float`
159
+ - `:int` maps to `Int`
160
+ - `:string` maps to `String`
161
+
162
+ You can use any of the above types with `!` to make it required or use them in `[]` for array parameters.
163
+
164
+ For any other costume types, graphlient will simply use `to_s` of the symbol provided for the type, so `query(ids: [:InvoiceType!])` will result in `query($ids: [InvoiceType!])`.
155
165
 
156
166
  The following mutation accepts a custom type that requires `fee_in_cents`.
157
167
 
158
168
  ```ruby
159
169
  client.query(input: { fee_in_cents: 12_345 }) do
160
- mutation(:$input => :createInvoiceInput!) do
161
- createInvoice(input: :$input) do
170
+ mutation(input: :createInvoiceInput!) do
171
+ createInvoice(input: :input) do
162
172
  id
163
173
  fee_in_cents
164
174
  end
@@ -174,8 +184,8 @@ You can `parse` and `execute` queries separately with optional variables. This i
174
184
  ```ruby
175
185
  # parse a query, returns a GraphQL::Client::OperationDefinition
176
186
  query = client.parse do
177
- query(:$ids => :'[Int]') do
178
- invoices(ids: :$ids) do
187
+ query(ids: [:int]) do
188
+ invoices(ids: :ids) do
179
189
  id
180
190
  fee_in_cents
181
191
  end
@@ -214,8 +224,8 @@ Define a query.
214
224
  ```ruby
215
225
  module SWAPI
216
226
  InvoiceQuery = Client.parse do
217
- query(:$id => :Int) do
218
- invoice(id: :$id) do
227
+ query(id: :int) do
228
+ invoice(id: :id) do
219
229
  id
220
230
  fee_in_cents
221
231
  end
@@ -11,7 +11,7 @@ module Graphlient
11
11
  req.body = {
12
12
  query: document.to_query_string,
13
13
  operationName: operation_name,
14
- variables: variables.to_json
14
+ variables: variables
15
15
  }.to_json
16
16
  end
17
17
  response.body
@@ -1,15 +1,32 @@
1
1
  module Graphlient
2
2
  class Query
3
+ SCALAR_TYPES = {
4
+ int: 'Int',
5
+ float: 'Float',
6
+ string: 'String',
7
+ boolean: 'Boolean'
8
+ }.freeze
9
+
10
+ ROOT_NODES = %w(query mutation subscription).freeze
11
+
3
12
  attr_accessor :query_str
4
13
 
5
14
  def initialize(&block)
6
15
  @indents = 0
7
16
  @query_str = ''
17
+ @variables = []
8
18
  instance_eval(&block)
9
19
  end
10
20
 
11
21
  def method_missing(m, *args, &block)
12
- append(m, args, &block)
22
+ append_node(m, args, &block)
23
+ end
24
+
25
+ ROOT_NODES.each do |root_node|
26
+ define_method(root_node) do |*args, &block|
27
+ @variables = args.first unless args.empty?
28
+ append_node(root_node, args, arg_processor: ->(k, v) { "$#{k}: #{variable_string(v)}" }, &block)
29
+ end
13
30
  end
14
31
 
15
32
  def respond_to_missing?(m, include_private = false)
@@ -22,11 +39,12 @@ module Graphlient
22
39
 
23
40
  private
24
41
 
25
- def append(query_field, args, &block)
42
+ def append_node(node, args, arg_processor: nil, &block)
26
43
  # add field
27
- @query_str << "\n#{indent}#{query_field}"
44
+ @query_str << "\n#{indent}#{node}"
28
45
  # add filter
29
- @query_str << "(#{get_args_str(args)})" if find_hash_arg(args)
46
+ hash_arguments = hash_arg(args)
47
+ @query_str << "(#{args_str(hash_arguments, arg_processor: arg_processor)})" if hash_arguments
30
48
 
31
49
  if block_given?
32
50
  @indents += 1
@@ -43,28 +61,46 @@ module Graphlient
43
61
  ' ' * @indents
44
62
  end
45
63
 
46
- def get_args_str(args)
47
- hash_args_str(find_hash_arg(args))
64
+ def hash_arg(args)
65
+ args.detect { |arg| arg.is_a? Hash }
66
+ end
67
+
68
+ def args_str(hash_args, arg_processor: nil)
69
+ hash_args.map do |k, v|
70
+ arg_processor ? arg_processor.call(k, v) : argument_string(k, v)
71
+ end.join(', ')
48
72
  end
49
73
 
50
- def find_hash_arg(args)
51
- args.detect { |arg| arg.is_a? Hash }
74
+ def argument_string(k, v)
75
+ "#{k}: #{argument_value_string(v)}"
52
76
  end
53
77
 
54
- def hash_args_str(hash)
55
- hash.map { |k, v| "#{k}: #{get_arg_value_str(v)}" }.join(', ')
78
+ def variable_string(v)
79
+ case v
80
+ when :id, :id!
81
+ v.to_s.upcase
82
+ when ->(value) { SCALAR_TYPES.key?(value.to_s.delete('!').to_sym) }
83
+ # scalar types
84
+ v.to_s.camelize
85
+ when Array
86
+ "[#{variable_string(v.first)}]"
87
+ else
88
+ v.to_s
89
+ end
56
90
  end
57
91
 
58
- def get_arg_value_str(value)
92
+ def argument_value_string(value)
59
93
  case value
60
94
  when String
61
95
  "\"#{value}\""
62
96
  when Numeric
63
97
  value.to_s
64
98
  when Array
65
- "[#{value.map { |v| get_arg_value_str(v) }.join(', ')}]"
99
+ "[#{value.map { |v| argument_value_string(v) }.join(', ')}]"
66
100
  when Hash
67
- "{ #{hash_args_str(value)} }"
101
+ "{ #{value.map { |k, v| "#{k}: #{argument_value_string(v)}" }.join(', ')} }"
102
+ when Symbol
103
+ @variables.keys.include?(value) ? "$#{value}" : value.to_s.camelize(:lower)
68
104
  else
69
105
  value
70
106
  end
@@ -1,3 +1,3 @@
1
1
  module Graphlient
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -30,8 +30,8 @@ describe Graphlient::Client do
30
30
  context 'parameterized query' do
31
31
  let(:query) do
32
32
  client.parse do
33
- query(:$ids => :'[Int]') do
34
- invoices(ids: :$ids) do
33
+ query(some_ids: [:int]) do
34
+ invoices(ids: :some_ids) do
35
35
  id
36
36
  fee_in_cents
37
37
  end
@@ -44,7 +44,7 @@ describe Graphlient::Client do
44
44
  end
45
45
 
46
46
  it '#execute' do
47
- response = client.execute(query, ids: [42])
47
+ response = client.execute(query, some_ids: [42])
48
48
  invoices = response.data.invoices
49
49
  expect(invoices.first.id).to eq 42
50
50
  expect(invoices.first.fee_in_cents).to eq 20_000
@@ -162,8 +162,8 @@ describe Graphlient::Client do
162
162
  it 'fails when missing input' do
163
163
  expect do
164
164
  client.query do
165
- mutation('$input' => :createInvoiceInput!) do
166
- createInvoice(input: :$input) do
165
+ mutation(input: :createInvoiceInput!) do
166
+ createInvoice(input: :input) do
167
167
  id
168
168
  fee_in_cents
169
169
  end
@@ -175,8 +175,8 @@ describe Graphlient::Client do
175
175
 
176
176
  it 'returns a response from a query' do
177
177
  response = client.query(ids: [42]) do
178
- query(:$ids => :'[Int]') do
179
- invoices(ids: :$ids) do
178
+ query(ids: [:int]) do
179
+ invoices(ids: :ids) do
180
180
  id
181
181
  fee_in_cents
182
182
  end
@@ -190,8 +190,8 @@ describe Graphlient::Client do
190
190
 
191
191
  it 'executes the mutation' do
192
192
  response = client.query(input: { fee_in_cents: 12_345 }) do
193
- mutation(:$input => :createInvoiceInput!) do
194
- createInvoice(input: :$input) do
193
+ mutation(input: :createInvoiceInput!) do
194
+ createInvoice(input: :input) do
195
195
  id
196
196
  fee_in_cents
197
197
  end
@@ -206,8 +206,8 @@ describe Graphlient::Client do
206
206
  it 'fails when mutation missing a field' do
207
207
  expect do
208
208
  client.query(input: {}) do
209
- mutation(:$input => :createInvoiceInput!) do
210
- createInvoice(input: :$input) do
209
+ mutation(input: :createInvoiceInput!) do
210
+ createInvoice(input: :input) do
211
211
  id
212
212
  fee_in_cents
213
213
  end
@@ -51,7 +51,7 @@ describe Graphlient::Query do
51
51
  expect(query.to_s).to eq "query{\n invoice(id: 10, threshold: 10.3, item_list: [\"str_item\", 2]){\n line_items(name: \"new name\")\n }\n }"
52
52
  end
53
53
 
54
- it 'returns proper query with query name' do
54
+ it 'returns proper query' do
55
55
  query = Graphlient::Query.new do
56
56
  query do
57
57
  invoice(id: 10) do
@@ -63,6 +63,19 @@ describe Graphlient::Query do
63
63
  end
64
64
  expect(query.to_s).to eq "query{\n invoice(id: 10){\n line_items{\n line_item_type\n }\n }\n }"
65
65
  end
66
+
67
+ it 'returns proper query with query variables' do
68
+ query = Graphlient::Query.new do
69
+ query(invoice_id: :int, names: [:string!]) do
70
+ invoice(id: :invoice_id, name: :names) do
71
+ line_items do
72
+ line_item_type
73
+ end
74
+ end
75
+ end
76
+ end
77
+ expect(query.to_s).to eq "query($invoice_id: Int, $names: [String!]){\n invoice(id: $invoice_id, name: $names){\n line_items{\n line_item_type\n }\n }\n }"
78
+ end
66
79
  end
67
80
 
68
81
  context 'mutation' do
@@ -16,8 +16,8 @@ describe Graphlient::Client do
16
16
  end
17
17
 
18
18
  Query = Client.parse do
19
- query(:$ids => :'[Int]') do
20
- invoices(ids: :$ids) do
19
+ query(some_ids: [:int]) do
20
+ invoices(ids: :some_ids) do
21
21
  id
22
22
  fee_in_cents
23
23
  end
@@ -30,7 +30,7 @@ describe Graphlient::Client do
30
30
  end
31
31
 
32
32
  it '#execute' do
33
- response = Graphlient::Client::Spec::Client.execute(Graphlient::Client::Spec::Query, ids: [42])
33
+ response = Graphlient::Client::Spec::Client.execute(Graphlient::Client::Spec::Query, some_ids: [42])
34
34
  invoices = response.data.invoices
35
35
  expect(invoices.first.id).to eq 42
36
36
  expect(invoices.first.fee_in_cents).to eq 20_000
@@ -12,7 +12,7 @@ post '/graphql' do
12
12
  headers['Content-Type'] = 'application/json'
13
13
  DummySchema.execute(
14
14
  params[:query],
15
- variables: params[:variables] ? JSON.parse(params[:variables]) : {},
15
+ variables: params[:variables] || {},
16
16
  context: {},
17
17
  operation_name: params[:operationName]
18
18
  ).to_json
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphlient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashkan Nasseri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-27 00:00:00.000000000 Z
11
+ date: 2017-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql-client