Class: Okay::GraphQL::QueryDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/okay/graphql.rb

Overview

Implements the GraphQL DSL.

Instance Method Summary collapse

Constructor Details

#initialize(indent = 0, &query) ⇒ QueryDSL

Returns a new instance of QueryDSL.



34
35
36
37
38
39
# File 'lib/okay/graphql.rb', line 34

def initialize(indent = 0, &query)
  @query = ""
  @indent = indent
  @indent_str = " " * indent
  instance_exec(&query)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object

rubocop:disable Metrics/AbcSize



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/okay/graphql.rb', line 42

def method_missing(name, *args, **kwargs, &block)
  query_part = @indent_str + name.to_s
  if !args.empty? || !kwargs.empty?
    query_part += "("

    query_args = []
    query_args += args unless args.empty?
    query_args += kwargs.map { |k, v|
      [k, v.inspect].join(": ")
    }
    query_part += query_args.join(", ")

    query_part += ")"
  end

  if block
    query_part += " {\n"
    query_part += QueryDSL.new(@indent + 2, &block).to_s
    query_part += @indent_str + "}"
  end

  @query += "#{query_part}\n"
end

Instance Method Details

#to_sObject

rubocop:enable Metrics/AbcSize



67
68
69
# File 'lib/okay/graphql.rb', line 67

def to_s
  @query
end