Moved api_types into TypeCreator

This commit is contained in:
Alberto Miedes Garcés
2016-11-30 13:43:10 +01:00
parent 76dd11631a
commit d1e1ee9d41
2 changed files with 24 additions and 17 deletions

View File

@@ -5,10 +5,10 @@ API_TYPE_DEFINITIONS = {
Comment => %I[ id body user_id user commentable_id ] Comment => %I[ id body user_id user commentable_id ]
} }
api_types = {} type_creator = GraphQL::TypeCreator.new
API_TYPE_DEFINITIONS.each do |model, fields| API_TYPE_DEFINITIONS.each do |model, fields|
api_types[model] = GraphQL::TypeCreator.create(model, fields, api_types) type_creator.create(model, fields)
end end
ConsulSchema = GraphQL::Schema.define do ConsulSchema = GraphQL::Schema.define do
@@ -28,11 +28,11 @@ QueryRoot = GraphQL::ObjectType.define do
name "Query" name "Query"
description "The query root for this schema" description "The query root for this schema"
API_TYPE_DEFINITIONS.each_key do |model| type_creator.created_types.each do |model, created_type|
# create an entry field to retrive a single object # create an entry field to retrive a single object
field model.name.underscore.to_sym do field model.name.underscore.to_sym do
type api_types[model] type created_type
description "Find one #{model.model_name.human} by ID" description "Find one #{model.model_name.human} by ID"
argument :id, !types.ID argument :id, !types.ID
resolve -> (object, arguments, context) { resolve -> (object, arguments, context) {
@@ -41,7 +41,7 @@ QueryRoot = GraphQL::ObjectType.define do
end end
# create an entry filed to retrive a paginated collection # create an entry filed to retrive a paginated collection
connection model.name.underscore.pluralize.to_sym, api_types[model].connection_type do connection model.name.underscore.pluralize.to_sym, created_type.connection_type do
description "Find all #{model.model_name.human.pluralize}" description "Find all #{model.model_name.human.pluralize}"
resolve -> (object, arguments, context) { resolve -> (object, arguments, context) {
model.all model.all

View File

@@ -1,6 +1,5 @@
module GraphQL module GraphQL
class TypeCreator class TypeCreator
# Return a GraphQL type for a 'database_type' # Return a GraphQL type for a 'database_type'
TYPES_CONVERSION = Hash.new(GraphQL::STRING_TYPE).merge( TYPES_CONVERSION = Hash.new(GraphQL::STRING_TYPE).merge(
integer: GraphQL::INT_TYPE, integer: GraphQL::INT_TYPE,
@@ -9,9 +8,16 @@ module GraphQL
double: GraphQL::FLOAT_TYPE double: GraphQL::FLOAT_TYPE
) )
def self.create(model, field_names, api_types) attr_accessor :created_types
new_graphql_type = GraphQL::ObjectType.define do def initialize
@created_types = {}
end
def create(model, field_names)
type_creator = self
created_type = GraphQL::ObjectType.define do
name(model.name) name(model.name)
description("#{model.model_name.human}") description("#{model.model_name.human}")
@@ -24,21 +30,22 @@ module GraphQL
association = model.reflect_on_all_associations.find { |a| a.name == field_name } association = model.reflect_on_all_associations.find { |a| a.name == field_name }
case association.macro case association.macro
when :has_one when :has_one
field(association.name, -> { api_types[association.klass] }) field(association.name, -> { type_creator.created_types[association.klass] })
when :belongs_to when :belongs_to
field(association.name, -> { api_types[association.klass] }) field(association.name, -> { type_creator.created_types[association.klass] })
when :has_many when :has_many
connection(association.name, -> { api_types[association.klass].connection_type { connection association.name, -> do
description "#{association.klass.model_name.human.pluralize}" type_creator.created_types[association.klass].connection_type do
resolve -> (object, arguments, context) { description "#{association.klass.model_name.human.pluralize}"
association.klass.all resolve -> (object, arguments, context) { association.klass.all }
} end
}}) end
end end
end end
end end
end end
return new_graphql_type # GraphQL::ObjectType created_types[model] = created_type
return created_type # GraphQL::ObjectType
end end
end end
end end