Module: Stardust::GraphQL::Collector
- Defined in:
- lib/stardust/graphql/collector.rb
Constant Summary collapse
- FIXED_TYPES =
{ id: ::GraphQL::Types::ID, int: Integer, integer: Integer, float: Float, string: String, date: ::GraphQL::Types::ISO8601DateTime, datetime: ::GraphQL::Types::ISO8601DateTime, boolean: ::GraphQL::Types::Boolean, }.freeze
- @@__types__ =
{}.merge(FIXED_TYPES)
- @@__defined_types__ =
[]
- @@__queries__ =
{}
- @@__mutations__ =
{}
Class Method Summary collapse
- .add_mutation(name, mutation:) ⇒ Object
- .add_query(name, query:) ⇒ Object
- .add_type(type, block, object_klass) ⇒ Object
- .clear_definitions! ⇒ Object
- .lookup_type(type) ⇒ Object
- .queries ⇒ Object
- .replace_types! ⇒ Object
- .types ⇒ Object
Class Method Details
.add_mutation(name, mutation:) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/stardust/graphql/collector.rb', line 61 def add_mutation(name, mutation:) if name.in?(@@__queries__.keys) raise "Mutation #{name.to_s} is already defined." end @@__mutations__[name] = mutation end |
.add_query(name, query:) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/stardust/graphql/collector.rb', line 54 def add_query(name, query:) if name.in?(@@__queries__.keys) raise "Query #{name.to_s} is already defined." end @@__queries__[name] = query end |
.add_type(type, block, object_klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/stardust/graphql/collector.rb', line 23 def add_type(type, block, object_klass) if type.in?(@@__types__.keys) raise "Type #{type.to_s} is alread defined." end @@__defined_types__ << type.to_s.camelize klass = Class.new(object_klass, &block) begin klass.graphql_name rescue NotImplementedError klass.graphql_name(type.to_s.camelize) end ::Stardust::GraphQL::Types.const_set("#{type.to_s.camelize}", klass) @@__types__[type] = "Stardust::GraphQL::Types::#{type.to_s.camelize}".constantize end |
.clear_definitions! ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/stardust/graphql/collector.rb', line 42 def self.clear_definitions! @@__defined_types__.each do |type| Stardust::GraphQL::Types.send(:remove_const, type) end @@__defined_types__ = [] @@__types__ = {}.merge(FIXED_TYPES) @@__queries__ = {} @@__mutations__ = {} end |
.lookup_type(type) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/stardust/graphql/collector.rb', line 76 def self.lookup_type(type) if type.is_a?(Array) type = type.first is_a_array = true end raise MissingType, type.to_s unless @@__types__[type] if is_a_array [@@__types__[type]] else @@__types__[type] end end |
.queries ⇒ Object
72 73 74 |
# File 'lib/stardust/graphql/collector.rb', line 72 def self.queries @@__queries__ end |
.replace_types! ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/stardust/graphql/collector.rb', line 91 def self.replace_types! @@__types__.values.each do |klass| begin klass.replace_types! if klass.respond_to?(:replace_types!) rescue MissingType => e warn <<~TEXT Stardust Compilation Error Type #{e.} is not defined. TEXT end end ## Here we define a new graphql type if Stardust::GraphQL.const_defined?("Schema") Stardust::GraphQL.send(:remove_const, "Schema") end klass = Class.new(::GraphQL::Schema) do use ::GraphQL::Batch end Stardust::GraphQL.const_set("Schema", klass) query_class = Class.new(::Stardust::GraphQL::Object) query_class.graphql_name("QueryRoot") @@__queries__.each do |name, query| block = ->(field) { query.get_arguments.each do |name, type, description, kwargs, block| field.argument(name, type, description, **kwargs, &block) end field.instance_variable_set(:@resolver_class, query) field.instance_variable_set(:@resolver_method, :resolve) field.extension(Extensions::Authorize) } query_class.send(:field, name, query.get_type, query.get_description, null: query.get_null, &block ) end begin query_class.replace_types! rescue MissingType => e warn <<~TEXT Stardust Compilation Error Type #{e.} is not defined. TEXT end Stardust::GraphQL::Schema.query(query_class) mutation_class = Class.new(::GraphQL::Schema::Object) mutation_class.graphql_name("MutationRoot") @@__mutations__.each do |name, mutation| begin mutation.replace_types! mutation_class.send(:field, name, mutation: mutation ) rescue MissingType => e warn <<~TEXT Stardust Compilation Error Type #{e.} is not defined. TEXT end end Stardust::GraphQL::Schema.mutation(mutation_class) end |
.types ⇒ Object
68 69 70 |
# File 'lib/stardust/graphql/collector.rb', line 68 def self.types @@__types__ end |