graphlyte 0.2.1 → 0.2.4

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: a541bce9d787c84cbb85652dfaebced77765a79ef1256f016a723d574e389313
4
- data.tar.gz: 2b1e62293e73d388bb539fcec90f7f6f1269d843db8cd0410176ff4d90426a82
3
+ metadata.gz: 5e01c43639c294e8ae4121522a6b0a5e498e69fd9e4f91e9f3e0a3a04753f826
4
+ data.tar.gz: 985c22c79e96547fdb5da497b52b5c2e8d03621f38253847e57afb932e18c752
5
5
  SHA512:
6
- metadata.gz: 2dc601050babce04c958e6bb6f3bfcf84d55962f8164ffbb1452e69105795961e570c247f8af303baee982173c77eebd049027381a86573780408516a059e1ee
7
- data.tar.gz: 05d5f0acce7bb921cd721125eb63728c347f136eaf6a552385d53eb537578a341707d454be924e82a9b35d13acf9af77874d5c9731ad0215159401f6407f3873
6
+ metadata.gz: 54d9dc438641a2f9f800eff0ade575f893472bbf96faba57e9d69171efbae8a8bd8caf50bdaeb4d7e71aadc37161c61e55c2385a1da98f04d0b687b20b6d654e
7
+ data.tar.gz: 671f0580825f19ce0f010926a40a4cdece1ddb408dbda8ec10120c80ad0a2274680fae80085679a1e5f688fc5049e9ad1efae52b21c595ddfa166f2c759fc509
@@ -26,15 +26,15 @@ module Graphlyte
26
26
  variables
27
27
  end
28
28
 
29
- def to_h(inner = false)
29
+ def to_h(raw = false)
30
30
  return {} unless values && !values.empty?
31
31
  values.inject({}) do |memo, (k, v)|
32
32
  if v.is_a?(Array)
33
- memo[k.to_s.to_camel_case] = v.map(&:to_s)
33
+ memo[k.to_s.to_camel_case] = v.map { |value| value.to_s(raw) }
34
34
  elsif v.is_a?(Set)
35
35
  memo[k.to_s.to_camel_case] = v.to_h
36
36
  else
37
- memo[k.to_s.to_camel_case] = v.to_s
37
+ memo[k.to_s.to_camel_case] = v.to_s(raw)
38
38
  end
39
39
  memo
40
40
  end
@@ -42,7 +42,15 @@ module Graphlyte
42
42
 
43
43
  def to_s(inner = false)
44
44
  return "" unless values && !values.empty?
45
- arr = values.map do |k,v|
45
+ arr = stringify_arguments
46
+ return arr.join(", ") if inner
47
+ "(#{arr.join(", ")})"
48
+ end
49
+
50
+ private
51
+
52
+ def stringify_arguments
53
+ values.map do |k,v|
46
54
  if v.is_a?(Array)
47
55
  "#{k.to_s.to_camel_case}: [#{v.map(&:to_s).join(", ")}]"
48
56
  elsif v.is_a?(Set)
@@ -51,12 +59,8 @@ module Graphlyte
51
59
  "#{k.to_s.to_camel_case}: #{v.to_s}"
52
60
  end
53
61
  end
54
- return arr.join(", ") if inner
55
- "(#{arr.join(", ")})"
56
62
  end
57
63
 
58
- private
59
-
60
64
  def expand_arguments(data)
61
65
  data.inject({}) do |memo, (k, v)|
62
66
  if v.is_a?(Array)
@@ -4,17 +4,11 @@ module Graphlyte
4
4
  class Value
5
5
  using Refinements::StringRefinement
6
6
 
7
- attr_reader :value, :default
7
+ attr_reader :value
8
8
 
9
- def initialize(value, default = nil)
9
+ def initialize(value)
10
10
  raise ArgumentError, "Hash not allowed in this context" if value.is_a? Hash
11
- if value.is_a?(Value)
12
- @value = value.value
13
- @default = value.default
14
- else
15
- @value = value
16
- @default = default
17
- end
11
+ @value = value
18
12
  end
19
13
 
20
14
  def symbol?
@@ -25,10 +19,10 @@ module Graphlyte
25
19
  value.is_a? Schema::Types::Base
26
20
  end
27
21
 
28
- def to_s
22
+ def to_s(raw = false)
29
23
  return "$#{value.to_s.to_camel_case}" if value.is_a? Symbol
30
24
  return value if value.is_a? Numeric
31
- return "\"#{value}\"" if value.is_a? String
25
+ return "\"#{value}\"" if value.is_a?(String) && !raw
32
26
  return "null" if value.nil?
33
27
  return "$#{value.placeholder.to_camel_case}" if value.is_a? Schema::Types::Base
34
28
  value.to_s
@@ -19,8 +19,9 @@ module Graphlyte
19
19
  str = ":#{value.value.placeholder} of #{value.value.name}"
20
20
  end
21
21
 
22
- if value.default
23
- str += " with default #{value.default.to_s}"
22
+ if value.value.default
23
+ str += " with default "
24
+ value.value.default.merge!(str)
24
25
  end
25
26
  str
26
27
  end.join("\n")
@@ -33,11 +34,16 @@ module Graphlyte
33
34
  str = "#{type} #{query_name}"
34
35
  unless types.empty?
35
36
  type_new = types.map do |type_arr|
36
- "$#{type_arr[0].to_camel_case}: #{type_arr[1]}"
37
+ type_str = "$#{type_arr[0].to_camel_case}: #{type_arr[1]}"
38
+ unless type_arr[2].nil?
39
+ type_str << " = "
40
+ type_arr[2].merge!(type_str)
41
+ end
42
+ type_str
37
43
  end
38
44
  str += "(#{type_new.join(", ")})"
39
45
  end
40
- { query: "#{str} #{to_s(1)}", variables: Arguments::Set.new(hargs).to_h }.to_json
46
+ { query: "#{str} #{to_s(1)}", variables: Arguments::Set.new(hargs).to_h(true) }.to_json
41
47
  end
42
48
 
43
49
  def to_s(indent=0)
@@ -59,12 +65,12 @@ module Graphlyte
59
65
  memo << "[#{merge_variable_types(var.value, hargs).first}]"
60
66
  end
61
67
  else
62
- memo << [var.value.placeholder, var.value.name]
68
+ memo << [var.value.placeholder, var.value.name, var.value.default]
63
69
  end
64
70
  memo
65
71
  end
66
72
  end
67
-
73
+
68
74
  def format_fragments
69
75
  str = "\n"
70
76
  flatten(builder.>>).each do |_, fragment|
@@ -63,11 +63,7 @@ module Graphlyte
63
63
  defaults = parse_default
64
64
  key = token[0][1]
65
65
  hash = {}
66
- if [Array, Hash].include?(value.class)
67
- hash[key] = value
68
- else
69
- hash[key] = Graphlyte::Arguments::Value.new(value, defaults)
70
- end
66
+ hash[key] = value
71
67
  hash
72
68
  elsif (token = expect(:SPECIAL_ARG_KEY)) && (value = parse_value)
73
69
  defaults = parse_default
@@ -76,7 +72,8 @@ module Graphlyte
76
72
  if [Array, Hash].include?(value.class)
77
73
  arg[token[0][1]] = value
78
74
  else
79
- arg[token[0][1]] = Graphlyte::Arguments::Value.new(value, defaults)
75
+ new_val = Schema::Types::Base.new(value, token[0][1], defaults)
76
+ arg[token[0][1]] = new_val
80
77
  end
81
78
  @special_args.merge!(arg)
82
79
  arg
@@ -89,8 +86,7 @@ module Graphlyte
89
86
  elsif token = expect(:SPECIAL_ARG_REF)
90
87
  ref = token[0][1]
91
88
  raise "Can't find ref $#{ref}" unless @special_args[ref]
92
- value = @special_args[ref]
93
- Arguments::Value.new(Graphlyte::TYPES.send(value.value, ref.to_sym), value.default)
89
+ @special_args[ref]
94
90
  elsif token = expect(:SPECIAL_ARG_VAL)
95
91
  token[0][1]
96
92
  elsif token = expect(:ARG_HASH_START)
@@ -108,10 +104,15 @@ module Graphlyte
108
104
 
109
105
  def parse_arg_hash
110
106
  if (key = expect(:ARG_KEY)) && (value = parse_value)
111
- need(:ARG_HASH_END)
112
107
  hash = {}
113
108
  hash[key[0][1]] = value
114
109
  hash
110
+ if new_hash = parse_arg_hash
111
+ hash.merge!(new_hash)
112
+ else
113
+ need(:ARG_HASH_END)
114
+ hash
115
+ end
115
116
  end
116
117
  end
117
118
 
@@ -205,6 +206,7 @@ module Graphlyte
205
206
 
206
207
  def parse_fragment
207
208
  if token = expect(:START_FRAGMENT)
209
+ parse_args
208
210
  builder = Builder.new parse_fields
209
211
  fragment = Fragment.new(token[0][1], token[0][2], builder: builder)
210
212
  @fragments_dictionary[token[0][1]] = fragment
@@ -300,12 +302,12 @@ module Graphlyte
300
302
  @tokens << [:END_FRAGMENT]
301
303
  pop_state
302
304
  pop_context
303
- elsif scanner.check /^\s*\{\s*$/
305
+ elsif scanner.check /^\s*\{\s*/
304
306
  if get_context == :field
305
307
  push_state :field
306
308
  push_context :field
307
309
  else
308
- scanner.scan /^\s*\{\s*$/
310
+ scanner.scan /^\s*\{\s*/
309
311
  push_context :field
310
312
  end
311
313
  else
@@ -327,16 +329,16 @@ module Graphlyte
327
329
  handle_field
328
330
  end
329
331
  when :query
330
- if scanner.scan /\}/
332
+ if scanner.scan /\s*\}\s*/
331
333
  @tokens << [:END_QUERY]
332
334
  pop_state
333
335
  pop_context
334
- elsif scanner.check /^\s*\{\s*$/
336
+ elsif scanner.check /^\s*\{\s*/
335
337
  if get_context == :field
336
338
  push_state :field
337
339
  push_context :field
338
340
  else
339
- scanner.scan /^\s*\{\s*$/
341
+ scanner.scan /^\s*\{\s*/
340
342
  push_context :field
341
343
  end
342
344
  else
@@ -397,7 +399,8 @@ module Graphlyte
397
399
  scanner.scan /\.{3}(\w+)/
398
400
  @tokens << [:FRAGMENT_REF, scanner[1]]
399
401
  pop_context
400
- pop_state if scanner.check /\s*\}\s*\}/
402
+ # we need to pop state if we are nested in a field, and not in the query context
403
+ pop_state if get_context == :field
401
404
  elsif scanner.scan /\.{3}(\w+)/
402
405
  @tokens << [:FRAGMENT_REF, scanner[1]]
403
406
  elsif scanner.scan /\s*(\w+):\s*/
@@ -406,7 +409,8 @@ module Graphlyte
406
409
  scanner.scan /\s*(\w+)\s*/
407
410
  @tokens << [:FIELD_NAME, scanner[1]]
408
411
  pop_context
409
- pop_state if scanner.check /\s*\}\s*\}/
412
+ # we need to pop state if we are nested in a field, and not in the query context
413
+ pop_state if get_context == :field
410
414
  elsif scanner.scan /\s*(\w+)\s*/
411
415
  @tokens << [:FIELD_NAME, scanner[1]]
412
416
  elsif scanner.scan /^\s*\(/
@@ -1,12 +1,52 @@
1
1
  module Graphlyte
2
2
  module Schema
3
3
  module Types
4
+ class Defaults
5
+ attr_reader :value
6
+ def initialize(value)
7
+ @value = value
8
+ end
9
+
10
+ def merge!(str)
11
+ parse_value(@value, str)
12
+ end
13
+
14
+ def parse_value(value, str)
15
+ if value.is_a?(Hash)
16
+ str << "{ "
17
+ value.each_with_index do |(k, v), idx|
18
+ str << "#{k}: "
19
+ parse_value(v, str)
20
+ str << ", " if idx < (value.size - 1)
21
+ end
22
+ str << " }"
23
+ elsif value.is_a?(Array)
24
+ str << "["
25
+ value.each_with_index do |item, idx|
26
+ parse_value(item, str)
27
+ str << ", " if idx < (value.size - 1)
28
+ end
29
+ str << "]"
30
+ elsif value.is_a?(Symbol)
31
+ str << value.to_s
32
+ else
33
+ str << "#{Arguments::Value.new(value).to_s}"
34
+ end
35
+ end
36
+ end
37
+
4
38
  class Base
5
39
  attr_reader :name, :placeholder
6
40
 
7
- def initialize(name, placeholder)
41
+ def initialize(name, placeholder, defaults=nil)
8
42
  @name = name
9
43
  @placeholder = placeholder
44
+ @defaults = defaults
45
+ end
46
+
47
+ def default
48
+ return nil if @defaults.class == NilClass
49
+ Defaults.new(@defaults)
10
50
  end
11
51
  end
12
52
  end
@@ -2,8 +2,8 @@ require_relative "schema/types/base"
2
2
 
3
3
  module Graphlyte
4
4
  class Types
5
- def method_missing(method, placeholder)
6
- Schema::Types::Base.new(method, placeholder)
5
+ def method_missing(method, placeholder, default = nil)
6
+ Schema::Types::Base.new(method, placeholder, default)
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphlyte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Gregory
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-21 00:00:00.000000000 Z
11
+ date: 2022-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec