DRY GraphQL::TypeCreator
This commit is contained in:
@@ -27,19 +27,11 @@ module GraphQL
|
||||
if model.column_names.include?(field_name.to_s)
|
||||
field(field_name.to_s, TYPES_CONVERSION[model.columns_hash[field_name.to_s].type])
|
||||
else
|
||||
association = model.reflect_on_all_associations.find { |a| a.name == field_name }
|
||||
case association.macro
|
||||
when :has_one
|
||||
field(association.name, -> { type_creator.created_types[association.klass] })
|
||||
when :belongs_to
|
||||
field(association.name, -> { type_creator.created_types[association.klass] })
|
||||
when :has_many
|
||||
connection(
|
||||
association.name,
|
||||
Proc.new { type_creator.created_types[association.klass].connection_type }
|
||||
) do
|
||||
resolve -> (object, arguments, context) { association.klass.all }
|
||||
end
|
||||
association = type_creator.class.association?(model, field_name)
|
||||
if type_creator.class.needs_pagination?(association)
|
||||
connection association.name, -> { type_creator.created_types[association.klass].connection_type }
|
||||
else
|
||||
field association.name, -> { type_creator.created_types[association.klass] }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -47,5 +39,13 @@ module GraphQL
|
||||
created_types[model] = created_type
|
||||
return created_type # GraphQL::ObjectType
|
||||
end
|
||||
|
||||
def self.association?(model, field_name)
|
||||
model.reflect_on_all_associations.find { |a| a.name == field_name }
|
||||
end
|
||||
|
||||
def self.needs_pagination?(association)
|
||||
association.macro == :has_many
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user