Module: Yogurt

Extended by:
T::Sig
Defined in:
lib/yogurt/query.rb,
lib/yogurt.rb,
lib/yogurt/http.rb,
lib/yogurt/memoize.rb,
lib/yogurt/version.rb,
lib/yogurt/converters.rb,
lib/yogurt/inspectable.rb,
lib/yogurt/error_result.rb,
lib/yogurt/query_result.rb,
lib/yogurt/code_generator.rb,
lib/yogurt/query_executor.rb,
lib/yogurt/query_container.rb,
lib/yogurt/scalar_converter.rb,
lib/yogurt/validation_error.rb,
lib/yogurt/query_declaration.rb,
lib/yogurt/code_generator/utils.rb,
lib/yogurt/unexpected_object_type.rb,
lib/yogurt/code_generator/enum_class.rb,
lib/yogurt/code_generator/leaf_class.rb,
lib/yogurt/code_generator/root_class.rb,
lib/yogurt/code_generator/input_class.rb,
lib/yogurt/code_generator/typed_input.rb,
lib/yogurt/code_generator/type_wrapper.rb,
lib/yogurt/code_generator/typed_output.rb,
lib/yogurt/code_generator/defined_class.rb,
lib/yogurt/code_generator/defined_method.rb,
lib/yogurt/code_generator/generated_file.rb,
lib/yogurt/code_generator/field_access_path.rb,
lib/yogurt/code_generator/field_access_method.rb,
lib/yogurt/code_generator/variable_definition.rb,
lib/yogurt/code_generator/defined_class_sorter.rb,
lib/yogurt/code_generator/operation_declaration.rb,
lib/yogurt/query_container/interfaces_and_unions_have_typename.rb

Overview

typed: strict frozen_string_literal: true

Defined Under Namespace

Modules: Converters, ErrorResult, Inspectable, Memoize, QueryContainer, QueryExecutor, QueryResult, ScalarConverter Classes: CodeGenerator, Http, Query, QueryDeclaration, UnexpectedObjectType, ValidationError

Constant Summary collapse

GRAPHQL_SCHEMA =
T.type_alias {T.class_of(GraphQL::Schema)}
SCALAR_TYPE =
T.type_alias {T.any(::String, T::Boolean, Numeric)}
OBJECT_TYPE =
T.type_alias {T::Hash[String, T.untyped]}
SCALAR_CONVERTER =
T.type_alias {T.all(Module, ScalarConverter)}
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.add_schema(schema, execute, default: true) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
# File 'lib/yogurt.rb', line 46

def self.add_schema(schema, execute, default: true)
  raise ArgumentError, "GraphQL schema must be assigned to a constant" if schema.name.nil?

  registered_schemas[schema] = execute
  @default_schema = T.let(schema, T.nilable(T.class_of(GraphQL::Schema))) if default
end

.default_schemaObject



35
36
37
# File 'lib/yogurt.rb', line 35

def self.default_schema
  @default_schema
end

.execute(schema:, query:, operation_name:, variables:, options:) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/yogurt.rb', line 24

def self.execute(schema:, query:, operation_name:, variables:, options:)
  execute = Yogurt.registered_schemas.fetch(schema)
  execute.execute(
    query,
    operation_name: operation_name,
    variables: variables,
    options: options,
  )
end

.register_scalar(schema, graphql_type_name, deserializer) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
# File 'lib/yogurt.rb', line 80

def self.register_scalar(schema, graphql_type_name, deserializer)
  raise ArgumentError, "Schema does not contain the type #{graphql_type_name}" if !schema.types.key?(graphql_type_name)

  raise ArgumentError, "ScalarConverters must be assigned to constants" if deserializer.name.nil?

  scalar_converters(schema)[graphql_type_name] = deserializer
end

.registered_schemasObject



40
41
42
43
# File 'lib/yogurt.rb', line 40

def self.registered_schemas
  @registered_schemas = T.let(@registered_schemas, T.nilable(T::Hash[GRAPHQL_SCHEMA, QueryExecutor]))
  @registered_schemas ||= {}
end

.scalar_converters(schema) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yogurt.rb', line 56

def self.scalar_converters(schema)
  raise ArgumentError, "GraphQL Schema has not been registered." if !registered_schemas.key?(schema)

  @scalar_converters = T.let(
    @scalar_converters,
    T.nilable(
      T::Hash[
        GRAPHQL_SCHEMA,
        T::Hash[String, SCALAR_CONVERTER]
      ],
    ),
  )

  @scalar_converters ||= Hash.new {|hash, key| hash[key] = {}}
  @scalar_converters[schema]
end