Move api config file parsing method into the ApiTypesCreator class
This commit is contained in:
@@ -1,26 +1,5 @@
|
||||
api_config = YAML.load_file('./config/api.yml')
|
||||
|
||||
api_type_definitions = {}
|
||||
|
||||
# Parse API configuration file
|
||||
|
||||
api_config.each do |api_type_model, api_type_info|
|
||||
model = api_type_model.constantize
|
||||
options = api_type_info['options']
|
||||
fields = {}
|
||||
|
||||
api_type_info['fields'].each do |field_name, field_type|
|
||||
if field_type.is_a?(Array) # paginated association
|
||||
fields[field_name.to_sym] = [field_type.first.constantize]
|
||||
elsif GraphQL::ApiTypesCreator::SCALAR_TYPES[field_type.to_sym]
|
||||
fields[field_name.to_sym] = field_type.to_sym
|
||||
else # simple association
|
||||
fields[field_name.to_sym] = field_type.constantize
|
||||
end
|
||||
end
|
||||
|
||||
api_type_definitions[model] = { options: options, fields: fields }
|
||||
end
|
||||
api_type_definitions = GraphQL::ApiTypesCreator::parse_api_config_file(api_config)
|
||||
|
||||
api_types_creator = GraphQL::ApiTypesCreator.new(api_type_definitions)
|
||||
created_api_types = api_types_creator.create
|
||||
|
||||
@@ -64,5 +64,28 @@ module GraphQL
|
||||
return created_type # GraphQL::ObjectType
|
||||
end
|
||||
|
||||
def self.parse_api_config_file(file)
|
||||
api_type_definitions = {}
|
||||
|
||||
file.each do |api_type_model, api_type_info|
|
||||
model = api_type_model.constantize
|
||||
options = api_type_info['options']
|
||||
fields = {}
|
||||
|
||||
api_type_info['fields'].each do |field_name, field_type|
|
||||
if field_type.is_a?(Array) # paginated association
|
||||
fields[field_name.to_sym] = [field_type.first.constantize]
|
||||
elsif SCALAR_TYPES[field_type.to_sym]
|
||||
fields[field_name.to_sym] = field_type.to_sym
|
||||
else # simple association
|
||||
fields[field_name.to_sym] = field_type.constantize
|
||||
end
|
||||
end
|
||||
|
||||
api_type_definitions[model] = { options: options, fields: fields }
|
||||
end
|
||||
|
||||
api_type_definitions
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user