gqli 0.4.0 → 0.5.0

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
  SHA256:
3
- metadata.gz: 972ad0f4a3e57ffe7e87a9492805b5d206ff0825ac335dbbf4a24b4981aed375
4
- data.tar.gz: d60e39618526584c5291bfe1fa050e1639cc458e01d1038bb62e88693764c833
3
+ metadata.gz: 546f032ddd8089b8c2709b097dfcf42eedbfaa244fa487aed9d35240530c5a22
4
+ data.tar.gz: acbdf1e8755552a48a08085764dae8b53877a95e13e9cef1b875d58c3a5a4912
5
5
  SHA512:
6
- metadata.gz: 40b7e0a94c6c543993131280bb00dc56e9d7600c9dc698970f66ccb3ff67613820c35199d60f74d7c2ec75417f52bd73ce528b9263193b70ed7e5a6498bc835f
7
- data.tar.gz: 00c23e2e51019bcf85a608507062d0fb46662b008674e09c546aa6151bedbb45d3c4098fd0e7fcc247d39794d96315003544429a9dd097dda172bec6ee2f36d1
6
+ metadata.gz: 9220a899f2fa1acbcdef297b7dc86a3fd6480017e7dc004fe5f3ead83c10c4f1087eb9b37b3d74e9a7b89592191026ba22baa4f9740eb56c6293ec844f367788
7
+ data.tar.gz: 31ab9c1aeaa34d692bc5f0fd85066d58cb6f6f4625fa8b6b3800d73a1dee0b17fe03acdecee9551d2cc4bf19c4d9627d88ca9954aa791a3733ee3aacdaf090db
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## v0.5.0
6
+ ### Added
7
+ * Add Enum support.
8
+
5
9
  ## v0.4.0
6
10
  ### Added
7
11
  * Add directive support.
data/README.md CHANGED
@@ -319,6 +319,34 @@ query = GQLi::DSL.query {
319
319
 
320
320
  The difference is that by using the native implementation, in case of the condition not being met, the node will be completely not included in the outgoing query.
321
321
 
322
+ ### Enums
323
+
324
+ [Enums](https://graphql.org/learn/schema/#enumeration-types) are a list of predefined values that are defined on the type system level.
325
+ Since Ruby doesn't have built-in Enums that can be translated directly into GraphQL Enums, we created the `__enum` helper that you can use within your queries to transform
326
+ your values into an Enum value.
327
+
328
+ ```ruby
329
+ query = GQLi::DSL.query {
330
+ catCollection(order: __enum('lives_ASC')) {
331
+ items {
332
+ name
333
+ }
334
+ }
335
+ }
336
+ ```
337
+
338
+ This will render to:
339
+
340
+ ```graphql
341
+ query {
342
+ catCollection(order: lives_ASC) {
343
+ items {
344
+ name
345
+ }
346
+ }
347
+ }
348
+ ```
349
+
322
350
  ## Yet to be implemented
323
351
 
324
352
  * Mutation queries
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative './enum_value'
4
+
3
5
  module GQLi
4
6
  # Base class for GraphQL type wrappers
5
7
  class Base
@@ -28,6 +30,11 @@ module GQLi
28
30
  @__nodes << Node.new(name, params, __depth + 1, &block)
29
31
  end
30
32
 
33
+ # Creates an EnumType value
34
+ def __enum(value)
35
+ EnumValue.new(value)
36
+ end
37
+
31
38
  protected
32
39
 
33
40
  def __clone_nodes(node_container)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative './query'
4
4
  require_relative './fragment'
5
+ require_relative './enum_value'
5
6
 
6
7
  module GQLi
7
8
  # GraphQL-like DSL methods
@@ -20,6 +21,13 @@ module GQLi
20
21
  Fragment.new(name, on, &block)
21
22
  end
22
23
 
24
+ # Creates a EnumValue object
25
+ #
26
+ # Can be used at a class level
27
+ def self.enum(value)
28
+ EnumValue.new(value)
29
+ end
30
+
23
31
  # Creates a Query object
24
32
  #
25
33
  # Can be used at an instance level
@@ -33,5 +41,12 @@ module GQLi
33
41
  def fragment(name, on, &block)
34
42
  Fragment.new(name, on, &block)
35
43
  end
44
+
45
+ # Creates a EnumValue object
46
+ #
47
+ # Can be used at an instance level
48
+ def enum(value)
49
+ EnumValue.new(value)
50
+ end
36
51
  end
37
52
  end
@@ -11,7 +11,7 @@ module GQLi
11
11
 
12
12
  # Serializes the enum value to string
13
13
  def to_s
14
- value
14
+ value.to_s
15
15
  end
16
16
  end
17
17
  end
@@ -115,28 +115,32 @@ module GQLi
115
115
  end
116
116
 
117
117
  def value_type_error(is_type, should_be, for_arg)
118
- fail "Value is '#{is_type}', but should be '#{should_be}' for '#{for_arg}'"
118
+ should_be = should_be.kind == 'ENUM' ? 'Enum' : should_be.name
119
+ additional_message = '. Wrap the value with `__enum`.' if should_be == 'Enum'
120
+
121
+ fail "Value is '#{is_type}', but should be '#{should_be}' for '#{for_arg}'#{additional_message}"
119
122
  end
120
123
 
121
124
  def validate_value_for_type(arg_type, value, for_arg)
122
125
  case value
123
- when ::String
124
- unless arg_type.name == 'String' || arg_type.kind == 'ENUM' || arg_type.name == 'ID'
125
- value_type_error('String, Enum or ID', arg_type.name, for_arg)
126
- end
127
- if arg_type.kind == 'ENUM' && !arg_type.enumValues.map(&:name).include?(value)
126
+ when EnumValue
127
+ if arg_type.kind == 'ENUM' && !arg_type.enumValues.map(&:name).include?(value.to_s)
128
128
  fail "Invalid value for Enum '#{arg_type.name}' for '#{for_arg}'"
129
129
  end
130
+ when ::String
131
+ unless arg_type.name == 'String' || arg_type.name == 'ID'
132
+ value_type_error('String or ID', arg_type, for_arg)
133
+ end
130
134
  when ::Integer
131
- value_type_error('Integer', arg_type.name, for_arg) unless arg_type.name == 'Int'
135
+ value_type_error('Integer', arg_type, for_arg) unless arg_type.name == 'Int'
132
136
  when ::Float
133
- value_type_error('Float', arg_type.name, for_arg) unless arg_type.name == 'Float'
137
+ value_type_error('Float', arg_type, for_arg) unless arg_type.name == 'Float'
134
138
  when ::Hash
135
139
  validate_hash_value(arg_type, value, for_arg)
136
140
  when true, false
137
- value_type_error('Boolean', arg_type.name, for_arg) unless arg_type.name == 'Boolean'
141
+ value_type_error('Boolean', arg_type, for_arg) unless arg_type.name == 'Boolean'
138
142
  else
139
- value_type_error(value.class.name, arg_type.name, for_arg)
143
+ value_type_error(value.class.name, arg_type, for_arg)
140
144
  end
141
145
  end
142
146
 
@@ -3,5 +3,5 @@
3
3
  # GraphQL Client and DSL library
4
4
  module GQLi
5
5
  # Gem version
6
- VERSION = '0.4.0'
6
+ VERSION = '0.5.0'
7
7
  end
@@ -26,6 +26,24 @@ describe GQLi::Client do
26
26
 
27
27
  let(:dsl) { GQLi::DSL }
28
28
 
29
+ describe 'query can call the client to execute' do
30
+ subject { client }
31
+
32
+ it '#__execute' do
33
+ vcr('catCollection') {
34
+ expect(
35
+ dsl.query {
36
+ catCollection {
37
+ items {
38
+ name
39
+ }
40
+ }
41
+ }.__execute(subject).data
42
+ ).not_to be_empty
43
+ }
44
+ end
45
+ end
46
+
29
47
  context 'validations enabled' do
30
48
  subject { client }
31
49
 
@@ -20,6 +20,7 @@ describe GQLi::DSL do
20
20
  someField
21
21
  }
22
22
  GRAPHQL
23
+ expect(query.to_gql).to eq query.to_s
23
24
  end
24
25
 
25
26
  it 'can create a query with a name' do
@@ -50,6 +51,15 @@ describe GQLi::DSL do
50
51
  GRAPHQL
51
52
  end
52
53
  end
54
+
55
+ describe '::enum' do
56
+ it 'can create an enum value' do
57
+ enum_value = subject.enum('foo')
58
+
59
+ expect(enum_value).to be_a GQLi::EnumValue
60
+ expect(enum_value.to_s).to eq 'foo'
61
+ end
62
+ end
53
63
  end
54
64
 
55
65
  describe 'instance level methods' do
@@ -97,6 +107,15 @@ describe GQLi::DSL do
97
107
  GRAPHQL
98
108
  end
99
109
  end
110
+
111
+ describe '#enum does the same as ::enum' do
112
+ it 'can create an enum value' do
113
+ enum_value = subject.enum('foo')
114
+
115
+ expect(enum_value).to be_a GQLi::EnumValue
116
+ expect(enum_value.to_s).to eq 'foo'
117
+ end
118
+ end
100
119
  end
101
120
 
102
121
  describe 'node DSL' do
@@ -259,5 +278,25 @@ describe GQLi::DSL do
259
278
  }
260
279
  GRAPHQL
261
280
  end
281
+
282
+ it '__enum can create EnumType values' do
283
+ query = subject.query {
284
+ catCollection(order: __enum('lives_ASC')) {
285
+ items {
286
+ name
287
+ }
288
+ }
289
+ }
290
+
291
+ expect(query.to_gql).to eq <<~GRAPHQL
292
+ query {
293
+ catCollection(order: lives_ASC) {
294
+ items {
295
+ name
296
+ }
297
+ }
298
+ }
299
+ GRAPHQL
300
+ end
262
301
  end
263
302
  end
@@ -153,7 +153,59 @@ describe GQLi::Introspection do
153
153
  validation = subject.validate(query)
154
154
  expect(validation.valid?).to be_falsey
155
155
  expect(validation.errors).not_to be_empty
156
- expect(validation.errors.map(&:to_s)).to include("Value is 'String, Enum or ID', but should be 'Int' for 'limit'")
156
+ expect(validation.errors.map(&:to_s)).to include("Value is 'String or ID', but should be 'Int' for 'limit'")
157
+ end
158
+
159
+ describe 'enum values' do
160
+ it 'can create a query with an enum as a filter and validations should not fail' do
161
+ query = dsl.query {
162
+ catCollection(order: __enum('lives_ASC')) {
163
+ items {
164
+ name
165
+ }
166
+ }
167
+ }
168
+
169
+ expect(subject.valid?(query)).to be_truthy
170
+
171
+ validation = subject.validate(query)
172
+ expect(validation.valid?).to be_truthy
173
+ expect(validation.errors).to be_empty
174
+ end
175
+
176
+ it 'fails when enum value is not provided properly' do
177
+ query = dsl.query {
178
+ catCollection(order: 'lives_ASC') {
179
+ items {
180
+ name
181
+ }
182
+ }
183
+ }
184
+
185
+ expect(subject.valid?(query)).to be_falsey
186
+
187
+ validation = subject.validate(query)
188
+ expect(validation.valid?).to be_falsey
189
+ expect(validation.errors).not_to be_empty
190
+ expect(validation.errors.map(&:to_s)).to include("Value is 'String or ID', but should be 'Enum' for 'order'. Wrap the value with `__enum`.")
191
+ end
192
+
193
+ it 'fails when enum value is not provided properly and is not a string' do
194
+ query = dsl.query {
195
+ catCollection(order: 1) {
196
+ items {
197
+ name
198
+ }
199
+ }
200
+ }
201
+
202
+ expect(subject.valid?(query)).to be_falsey
203
+
204
+ validation = subject.validate(query)
205
+ expect(validation.valid?).to be_falsey
206
+ expect(validation.errors).not_to be_empty
207
+ expect(validation.errors.map(&:to_s)).to include("Value is 'Integer', but should be 'Enum' for 'order'. Wrap the value with `__enum`.")
208
+ end
157
209
  end
158
210
 
159
211
  describe 'directives' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gqli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Contentful GmbH (David Litvak Bruno)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-19 00:00:00.000000000 Z
11
+ date: 2019-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http