graphlyte 0.3.1 → 0.3.2

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: 62efb09535601a1ef60cd1f6d24108a32c954382a8e2f280e8e570bf8d14d0e2
4
- data.tar.gz: 9098e5efe7b6344f1bff6effddd660de3e4d6632ec5d8c7b18516cdc7d522a4e
3
+ metadata.gz: 93f97fef094a43b1c151dee58329d7b198210246b1fec046e18263d65437d3f4
4
+ data.tar.gz: 3d057d189d9a93e601c7b91ce5ad1feb1fd00b0ab17e1c772ebdbff58cbe8349
5
5
  SHA512:
6
- metadata.gz: 2577fa1470f54149841295630b98b865f44b2cc5059c67a34bdea735dfb10b0da8bedfd325567577b7e0d6b900f9cc5004e4cf7d4b7fe8e6ac801ea491487e49
7
- data.tar.gz: b319c1970a99b031e13df0b33c74a5687490e423c4d141016cb01edf1fb0e2a5a17474ed19bea4b4db709715a883fb3148fac8414a1da1d293fca52d355d5c7c
6
+ metadata.gz: 8f106a673e0d70b97d0a4d5634c5c5b03c540fa4e4b857465e2da4252053744f2950e1b78021b99fd0ff3a4fb7bdd896aedbe775a9afc5c2e50b9017692dc626
7
+ data.tar.gz: 5ce5c740e6b25fca56736261db1596e25471f1e7bf5bd91785f81da8d2d8d6641b75487b325063ff115a2a3594c6f3fb891e47613c93e2ea5d53529240251b06
@@ -12,6 +12,22 @@ module Graphlyte
12
12
  @values = expand_arguments(data) unless data.nil?
13
13
  end
14
14
 
15
+ def resolve_lazy_special_args(parser_special_args)
16
+ @values&.each do |key, value|
17
+ if value.is_a?(Set)
18
+ value.resolve_lazy_special_args(parser_special_args) if value.is_a?(Set)
19
+ elsif value.is_a?(Array)
20
+ value.each do |it|
21
+ it.refresh(parser_special_args) if it.is_a?(Value)
22
+ it.resolve_lazy_special_args(parser_special_args) if it.is_a?(Set)
23
+ end
24
+ else
25
+ value.refresh(parser_special_args)
26
+ end
27
+ [key, value]
28
+ end.to_h
29
+ end
30
+
15
31
  def extract_variables(values=@values, variables=[])
16
32
  values&.each do |key, value|
17
33
  if value.is_a?(Set)
@@ -25,6 +25,10 @@ module Graphlyte
25
25
  value.is_a? Schema::Types::Base
26
26
  end
27
27
 
28
+ def refresh(args)
29
+ @value = value.call(args) if value.is_a? Proc
30
+ end
31
+
28
32
  def to_s(raw = false)
29
33
  return "$#{value.to_s.to_camel_case}" if value.is_a? Symbol
30
34
  return value if value.is_a? Numeric
@@ -0,0 +1,17 @@
1
+ module Graphlyte
2
+ module Arguments
3
+ class ValueLiteral
4
+ attr_reader :value
5
+
6
+ def initialize(string)
7
+ raise 'Value must be a string' unless string.class == String
8
+
9
+ @value = string
10
+ end
11
+
12
+ def to_s
13
+ @value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -4,6 +4,7 @@ require_relative "../query"
4
4
  require_relative "../fragment"
5
5
  require_relative "../schema_query"
6
6
  require_relative "../types"
7
+ require_relative "../arguments/value_literal"
7
8
 
8
9
  module Graphlyte
9
10
  module Schema
@@ -122,10 +123,20 @@ module Graphlyte
122
123
  def parse_value
123
124
  if token = expect(:ARG_NUM_VALUE) || expect(:ARG_STRING_VALUE) || expect(:ARG_BOOL_VALUE) || expect(:ARG_FLOAT_VALUE)
124
125
  token[0][1]
126
+ elsif token = expect(:ARG_LITERAL_VALUE)
127
+ Graphlyte::Arguments::ValueLiteral.new(token[0][1])
125
128
  elsif token = expect(:SPECIAL_ARG_REF)
126
129
  ref = token[0][1]
127
- raise "Can't find ref $#{ref}" unless @special_args[ref]
128
- @special_args[ref]
130
+ # can't prove if this exists yet, so lets add it to the list
131
+ unless @special_args&.dig(ref)
132
+ @refs_to_validate ||= []
133
+ @refs_to_validate << ref
134
+ -> (args) do
135
+ args[ref]
136
+ end
137
+ else
138
+ @special_args[ref]
139
+ end
129
140
  elsif token = expect(:SPECIAL_ARG_VAL)
130
141
  token[0][1]
131
142
  elsif token = expect(:ARG_HASH)
@@ -244,7 +255,7 @@ module Graphlyte
244
255
  end
245
256
 
246
257
  class FragmentParser
247
- attr_reader :tokens, :position, :fragments_dictionary
258
+ attr_reader :tokens, :position, :fragments_dictionary, :special_args
248
259
 
249
260
  include ParserHelpers
250
261
 
@@ -293,14 +304,34 @@ module Graphlyte
293
304
  @position = 0
294
305
  end
295
306
 
307
+ def refresh_lazy_refs(fields)
308
+ fields.each do |field|
309
+ if field.is_a? Fieldset
310
+ refresh_lazy_refs(field.fields)
311
+ else
312
+ field.inputs.resolve_lazy_special_args(@special_args)
313
+ refresh_lazy_refs(field.fieldset.fields)
314
+ end
315
+ end
316
+ end
317
+
296
318
  def parse
297
319
  if token = expect(:EXPRESSION)
298
- parse_expression(token[0][1], token[0][2])
320
+ value = parse_expression(token[0][1], token[0][2])
321
+
322
+ # validate fragment refs
323
+ @refs_to_validate&.each do |ref|
324
+ raise "Argument reference #{ref} doesn't exist" unless @special_args[ref]
325
+ end
326
+
327
+ refresh_lazy_refs(value.builder.>>)
328
+
329
+ value
299
330
  elsif expect(:FRAGMENT)
300
331
  skip_fragments
301
332
  parse
302
333
  else
303
- raise "INVALID"
334
+ raise "Expression or Fragment not found"
304
335
  end
305
336
  end
306
337
 
@@ -310,7 +341,6 @@ module Graphlyte
310
341
 
311
342
  def parse_expression(type, name)
312
343
  parse_args
313
- fields = []
314
344
  builder = parse_fieldset_into_builder
315
345
  need(:END_FIELDSET)
316
346
  query = Query.new(name, type.to_sym, builder: builder)
@@ -369,11 +399,7 @@ module Graphlyte
369
399
  push_state :arguments
370
400
  end
371
401
  elsif scanner.check /\s*\}/
372
- if get_context == :fragments
373
- end_fragment
374
- elsif get_context == :expression
375
- end_expression
376
- end
402
+ end_fieldset
377
403
  else
378
404
  advance
379
405
  end
@@ -431,6 +457,7 @@ module Graphlyte
431
457
  def end_arguments
432
458
  scanner.scan /\s*\)/
433
459
  @tokens << [:END_ARGS]
460
+ pop_state if state == :argument_defaults
434
461
  pop_state
435
462
  end_fieldset while check_for_last && state == :fieldset
436
463
  end
@@ -493,9 +520,10 @@ module Graphlyte
493
520
 
494
521
  def pop_argument_state
495
522
  if check_for_last(/\s*\)/)
523
+ @tokens << [:END_DEFAULT_VALUE] if state == :argument_defaults
496
524
  end_arguments
497
525
  else
498
- pop_state unless %i[argument_defaults hash_arguments array_arguments special_args arguments].include?(state)
526
+ pop_state unless %i[arguments argument_defaults hash_arguments array_arguments special_args].include?(state)
499
527
  end
500
528
  end
501
529
 
@@ -555,17 +583,19 @@ module Graphlyte
555
583
  pop_argument_state
556
584
  elsif scanner.scan /,/
557
585
  # no-op
586
+ elsif scanner.scan /([A-Za-z_"]+)/
587
+ @tokens << [:ARG_LITERAL_VALUE, scanner[1]]
588
+
558
589
  elsif check_for_last(/\s*\)/)
559
590
  @tokens << [:END_DEFAULT_VALUE] if state == :argument_defaults
560
591
  end_arguments
561
- pop_state
562
592
  else
563
593
  advance
564
594
  end
565
595
  end
566
596
 
567
597
  def tokenize_fields
568
- if scanner.check %r[{]
598
+ if scanner.check %r[\s*{]
569
599
  tokenize_fieldset
570
600
  # ... on Model - or - ... @directive
571
601
  elsif scanner.scan %r{\.{3}\s}
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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Gregory
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-27 00:00:00.000000000 Z
11
+ date: 2022-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -47,6 +47,7 @@ files:
47
47
  - lib/graphlyte.rb
48
48
  - lib/graphlyte/arguments/set.rb
49
49
  - lib/graphlyte/arguments/value.rb
50
+ - lib/graphlyte/arguments/value_literal.rb
50
51
  - lib/graphlyte/builder.rb
51
52
  - lib/graphlyte/directive.rb
52
53
  - lib/graphlyte/field.rb