Module: Yogurt::QueryContainer

Extended by:
T::Sig
Defined in:
lib/yogurt/query_container.rb,
lib/yogurt/query_container/interfaces_and_unions_have_typename.rb

Defined Under Namespace

Modules: InterfacesAndUnionsHaveTypename

Constant Summary collapse

CONTAINER =
T.type_alias do
  T.all(Module, QueryContainer)
end
VALIDATION_RULES =
T.let(
  [
    *GraphQL::StaticValidation::ALL_RULES,
    InterfacesAndUnionsHaveTypename
  ].freeze,
  T::Array[T.untyped],
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.containersObject



32
33
34
35
# File 'lib/yogurt/query_container.rb', line 32

def self.containers
  @containers = T.let(@containers, T.nilable(T::Array[CONTAINER]))
  @containers ||= []
end

.extended(other) ⇒ Object



26
27
28
29
# File 'lib/yogurt/query_container.rb', line 26

def self.extended(other)
  super
  QueryContainer.containers << T.cast(other, CONTAINER)
end

.included(other) ⇒ Object



21
22
23
# File 'lib/yogurt/query_container.rb', line 21

def self.included(other)
  Kernel.raise(ValidationError, "You need to `extend Yogurt::QueryContainer`, you cannot use `include`.")
end

Instance Method Details

#declare_query(query_text, schema: nil) ⇒ Object



55
56
57
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
85
86
87
# File 'lib/yogurt/query_container.rb', line 55

def declare_query(query_text, schema: nil)
  schema ||= Yogurt.default_schema
  Kernel.raise(ValidationError, "You need to either provide a `schema:` to declare_query, or set Yogurt.default_schema") if schema.nil?

  case (container = self)
  when Module
    # noop
  else
    Kernel.raise(ValidationError, "You need to `extend Yogurt::QueryContainer`, you cannot use `include`.")
  end

  Kernel.raise(ValidationError, "Query containers must be classes or modules that are assigned to constants.") if container.name.nil?

  validator = GraphQL::StaticValidation::Validator.new(schema: schema, rules: VALIDATION_RULES)
  query = GraphQL::Query.new(schema, query_text)
  validation_result = validator.validate(query)

  error = validation_result[:errors][0]
  Kernel.raise(ValidationError, error.message) if error

  if query.operations.key?(nil)
    Kernel.raise(ValidationError, "You must provide a name for each of the operations in your GraphQL query.")
  elsif query.operations.none?
    Kernel.raise(ValidationError, "Your query did not define any operations.")
  end

  declared_queries << QueryDeclaration.new(
    container: container,
    operations: query.operations.keys.freeze,
    query_text: query_text,
    schema: schema,
  )
end

#declared_queriesObject



38
39
40
41
# File 'lib/yogurt/query_container.rb', line 38

def declared_queries
  @declared_queries = T.let(@declared_queries, T.nilable(T::Array[QueryDeclaration]))
  @declared_queries ||= []
end

#fetch_query(operation_name) ⇒ Object



44
45
46
47
# File 'lib/yogurt/query_container.rb', line 44

def fetch_query(operation_name)
  result = declared_queries.detect {|d| d.operations.include?(operation_name)}
  T.must(result)
end