From 7027164867f2f9831c8e0459eee016fe26ef7102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Sun, 8 Jan 2017 11:37:56 +0100 Subject: [PATCH] Clean code --- config/initializers/graphql.rb | 13 ++++--------- lib/graph_ql/type_creator.rb | 6 +++++- .../lib/{ => graph_ql}/association_resolver_spec.rb | 0 spec/lib/{ => graph_ql}/type_creator_spec.rb | 0 4 files changed, 9 insertions(+), 10 deletions(-) rename spec/lib/{ => graph_ql}/association_resolver_spec.rb (100%) rename spec/lib/{ => graph_ql}/type_creator_spec.rb (100%) diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index 478cf10e3..01d54ab19 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -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 diff --git a/lib/graph_ql/type_creator.rb b/lib/graph_ql/type_creator.rb index 15803285e..9042d7f3f 100644 --- a/lib/graph_ql/type_creator.rb +++ b/lib/graph_ql/type_creator.rb @@ -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 diff --git a/spec/lib/association_resolver_spec.rb b/spec/lib/graph_ql/association_resolver_spec.rb similarity index 100% rename from spec/lib/association_resolver_spec.rb rename to spec/lib/graph_ql/association_resolver_spec.rb diff --git a/spec/lib/type_creator_spec.rb b/spec/lib/graph_ql/type_creator_spec.rb similarity index 100% rename from spec/lib/type_creator_spec.rb rename to spec/lib/graph_ql/type_creator_spec.rb