Module: Quail::Resource::TypeBuilder::FieldBuilder
- Defined in:
- lib/quail/resource/type_builder/field_builder.rb
Overview
Defines scalar (column-backed) and computed fields on a GraphQL type.
Constant Summary collapse
- CONTEXT_AWARE_ARITY =
Full block signature: |object, args, context|
3
Class Method Summary collapse
- .define_column_fields(type_class, model, attrs) ⇒ Object
- .define_computed_fields(type_class, attrs) ⇒ Object
- .define_computed_resolver(type_class, name, blk) ⇒ Object
- .define_single_computed_field(type_class, name, config) ⇒ Object
- .safe_call(blk) ⇒ Object
Class Method Details
.define_column_fields(type_class, model, attrs) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/quail/resource/type_builder/field_builder.rb', line 11 def self.define_column_fields(type_class, model, attrs) attrs.each do |name, config| next unless config[:type] == :column col = model.columns_hash[name.to_s] if col type_class.field name, TypeMap.graphql_types(col), null: TypeMap.nullable?(col) else type_class.field name, GraphQL::Types::String, null: true end end end |
.define_computed_fields(type_class, attrs) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/quail/resource/type_builder/field_builder.rb', line 24 def self.define_computed_fields(type_class, attrs) attrs.each do |name, config| next unless config[:type] == :computed define_single_computed_field(type_class, name, config) end end |
.define_computed_resolver(type_class, name, blk) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/quail/resource/type_builder/field_builder.rb', line 39 def self.define_computed_resolver(type_class, name, blk) type_class.define_method(name) do if blk.arity.abs >= CONTEXT_AWARE_ARITY FieldBuilder.safe_call(blk, object, nil, context) else FieldBuilder.safe_call(blk, object) end end end |
.define_single_computed_field(type_class, name, config) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/quail/resource/type_builder/field_builder.rb', line 32 def self.define_single_computed_field(type_class, name, config) gql_type = config[:graphql_type] || GraphQL::Types::String nullable = config[:null].nil? || config[:null] type_class.field name, gql_type, null: nullable define_computed_resolver(type_class, name, config[:block]) end |
.safe_call(blk) ⇒ Object
49 50 51 52 53 |
# File 'lib/quail/resource/type_builder/field_builder.rb', line 49 def self.safe_call(blk, *) blk.call(*) rescue LocalJumpError => e e.exit_value end |