Class: Graphlyte::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlyte/dsl.rb

Overview

The DSL methods for query construction are defined here.

The main methods are:

  • ‘var`: creates a fresh unique variable

  • ‘enum`: allows referring to enum values

  • ‘fragment`: creates a fragment that can be re-used in operations

  • ‘query`: creates a `Query` operation

  • ‘mutation`: creates a `Mutation` operation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema = nil) ⇒ DSL

Returns a new instance of DSL.



23
24
25
# File 'lib/graphlyte/dsl.rb', line 23

def initialize(schema = nil)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



21
22
23
# File 'lib/graphlyte/dsl.rb', line 21

def schema
  @schema
end

Instance Method Details

#enum(value) ⇒ Object



31
32
33
# File 'lib/graphlyte/dsl.rb', line 31

def enum(value)
  Syntax::Value.new(value.to_sym, :ENUM)
end

#fragment(fragment_name = nil, on:, doc: Document.new, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphlyte/dsl.rb', line 58

def fragment(fragment_name = nil, on:, doc: Document.new, &block)
  frag = Graphlyte::Syntax::Fragment.new

  frag.type_name = on
  frag.selection = SelectionBuilder.build(doc, &block)

  if fragment_name
    frag.name = fragment_name
  else
    base = "#{on}Fields"
    n = 1
    frag.name = base

    while doc.fragments[frag.name]
      frag.name = "#{base}_#{n}"
      n += 1
    end
  end

  doc.fragments.each_value do |required|
    frag.refers_to required
  end

  doc.define(frag)

  frag
end

#mutation(name = nil, doc = Document.new, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/graphlyte/dsl.rb', line 47

def mutation(name = nil, doc = Document.new, &block)
  op = Syntax::Operation.new(type: :mutation)
  doc.define(op)

  op.name = name
  op.selection = SelectionBuilder.build(doc, &block)

  # TODO: infer operation signatures (requires schema!)
  doc
end

#query(name = nil, doc = Document.new, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphlyte/dsl.rb', line 35

def query(name = nil, doc = Document.new, &block)
  op = Syntax::Operation.new(type: :query)
  doc.define(op)

  op.name = name
  op.selection = SelectionBuilder.build(doc, &block)

  Editors::InferSignature.new(@schema).edit(doc)

  doc
end

#var(type = nil, name = nil) ⇒ Object



27
28
29
# File 'lib/graphlyte/dsl.rb', line 27

def var(type = nil, name = nil)
  SelectionBuilder::Variable.new(type: type, name: name&.to_s&.camelize)
end