Clean code

This commit is contained in:
Alberto Miedes Garcés
2017-01-08 11:37:56 +01:00
parent ed6ba384dd
commit 7027164867
4 changed files with 9 additions and 10 deletions

View File

@@ -22,20 +22,15 @@ api_config.each do |api_type_model, api_type_info|
api_type_definitions[model] = { options: options, fields: fields }
end
# Create all GraphQL types
type_creator = GraphQL::TypeCreator.new(api_type_definitions)
type_creator.create_api_types
QueryRoot = type_creator.create_query_root
QueryRoot = type_creator.query_root
ConsulSchema = GraphQL::Schema.define do
query QueryRoot
# Reject deeply-nested queries
max_depth 10
resolve_type -> (object, ctx) {
# look up types by class name
type_name = object.class.name
resolve_type -> (object, ctx) do
type_name = object.class.name # look up types by class name
ConsulSchema.types[type_name]
}
end
end

View File

@@ -1,3 +1,5 @@
require 'graphql'
module GraphQL
class TypeCreator
SCALAR_TYPES = {
@@ -8,11 +10,13 @@ module GraphQL
string: GraphQL::STRING_TYPE
}
attr_accessor :created_types, :api_type_definitions
attr_accessor :created_types, :api_type_definitions, :query_root
def initialize(api_type_definitions)
@api_type_definitions = api_type_definitions
@created_types = {}
create_api_types
@query_root = create_query_root
end
def create_api_types