Moved api_types into TypeCreator
This commit is contained in:
@@ -5,10 +5,10 @@ API_TYPE_DEFINITIONS = {
|
||||
Comment => %I[ id body user_id user commentable_id ]
|
||||
}
|
||||
|
||||
api_types = {}
|
||||
type_creator = GraphQL::TypeCreator.new
|
||||
|
||||
API_TYPE_DEFINITIONS.each do |model, fields|
|
||||
api_types[model] = GraphQL::TypeCreator.create(model, fields, api_types)
|
||||
type_creator.create(model, fields)
|
||||
end
|
||||
|
||||
ConsulSchema = GraphQL::Schema.define do
|
||||
@@ -28,11 +28,11 @@ QueryRoot = GraphQL::ObjectType.define do
|
||||
name "Query"
|
||||
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
|
||||
field model.name.underscore.to_sym do
|
||||
type api_types[model]
|
||||
type created_type
|
||||
description "Find one #{model.model_name.human} by ID"
|
||||
argument :id, !types.ID
|
||||
resolve -> (object, arguments, context) {
|
||||
@@ -41,7 +41,7 @@ QueryRoot = GraphQL::ObjectType.define do
|
||||
end
|
||||
|
||||
# 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}"
|
||||
resolve -> (object, arguments, context) {
|
||||
model.all
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module GraphQL
|
||||
class TypeCreator
|
||||
|
||||
# Return a GraphQL type for a 'database_type'
|
||||
TYPES_CONVERSION = Hash.new(GraphQL::STRING_TYPE).merge(
|
||||
integer: GraphQL::INT_TYPE,
|
||||
@@ -9,9 +8,16 @@ module GraphQL
|
||||
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)
|
||||
description("#{model.model_name.human}")
|
||||
@@ -24,21 +30,22 @@ module GraphQL
|
||||
association = model.reflect_on_all_associations.find { |a| a.name == field_name }
|
||||
case association.macro
|
||||
when :has_one
|
||||
field(association.name, -> { api_types[association.klass] })
|
||||
field(association.name, -> { type_creator.created_types[association.klass] })
|
||||
when :belongs_to
|
||||
field(association.name, -> { api_types[association.klass] })
|
||||
field(association.name, -> { type_creator.created_types[association.klass] })
|
||||
when :has_many
|
||||
connection(association.name, -> { api_types[association.klass].connection_type {
|
||||
description "#{association.klass.model_name.human.pluralize}"
|
||||
resolve -> (object, arguments, context) {
|
||||
association.klass.all
|
||||
}
|
||||
}})
|
||||
connection association.name, -> do
|
||||
type_creator.created_types[association.klass].connection_type do
|
||||
description "#{association.klass.model_name.human.pluralize}"
|
||||
resolve -> (object, arguments, context) { association.klass.all }
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user