From 0b302c2afc06490c46db913ff8c84ad9394cb0e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Sat, 7 Jan 2017 23:04:30 +0100 Subject: [PATCH] Move code from GraphQL initializer into GraphQL::TypeCreator --- config/initializers/graphql.rb | 38 ++++-------------------------- lib/graph_ql/type_creator.rb | 42 +++++++++++++++++++++++++++++++--- spec/lib/type_creator_spec.rb | 20 ++++++++-------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index b385f1483..478cf10e3 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -1,6 +1,6 @@ api_config = YAML.load_file('./config/api.yml') -API_TYPE_DEFINITIONS = {} +api_type_definitions = {} # Parse API configuration file @@ -19,16 +19,13 @@ api_config.each do |api_type_model, api_type_info| end end - API_TYPE_DEFINITIONS[model] = { options: options, fields: fields } + api_type_definitions[model] = { options: options, fields: fields } end # Create all GraphQL types - -type_creator = GraphQL::TypeCreator.new - -API_TYPE_DEFINITIONS.each do |model, info| - type_creator.create(model, info[:fields]) -end +type_creator = GraphQL::TypeCreator.new(api_type_definitions) +type_creator.create_api_types +QueryRoot = type_creator.create_query_root ConsulSchema = GraphQL::Schema.define do query QueryRoot @@ -42,28 +39,3 @@ ConsulSchema = GraphQL::Schema.define do ConsulSchema.types[type_name] } end - -QueryRoot = GraphQL::ObjectType.define do - name "Query" - description "The query root for this schema" - - type_creator.created_types.each do |model, created_type| - - # create an entry field to retrive a single object - if API_TYPE_DEFINITIONS[model][:fields][:id] - field model.name.underscore.to_sym do - type created_type - description "Find one #{model.model_name.human} by ID" - argument :id, !types.ID - resolve GraphQL::RootElementResolver.new(model) - end - end - - # create an entry filed to retrive a paginated collection - connection model.name.underscore.pluralize.to_sym, created_type.connection_type do - description "Find all #{model.model_name.human.pluralize}" - resolve GraphQL::RootCollectionResolver.new(model) - end - - end -end diff --git a/lib/graph_ql/type_creator.rb b/lib/graph_ql/type_creator.rb index b77360c72..15803285e 100644 --- a/lib/graph_ql/type_creator.rb +++ b/lib/graph_ql/type_creator.rb @@ -8,13 +8,20 @@ module GraphQL string: GraphQL::STRING_TYPE } - attr_accessor :created_types + attr_accessor :created_types, :api_type_definitions - def initialize + def initialize(api_type_definitions) + @api_type_definitions = api_type_definitions @created_types = {} end - def create(model, fields) + def create_api_types + api_type_definitions.each do |model, info| + self.create_type(model, info[:fields]) + end + end + + def create_type(model, fields) type_creator = self created_type = GraphQL::ObjectType.define do @@ -44,6 +51,35 @@ module GraphQL return created_type # GraphQL::ObjectType end + def create_query_root + type_creator = self + + GraphQL::ObjectType.define do + name 'QueryRoot' + description 'The query root for this schema' + + type_creator.created_types.each do |model, created_type| + + # create an entry field to retrive a single object + if type_creator.api_type_definitions[model][:fields][:id] + field model.name.underscore.to_sym do + type created_type + description "Find one #{model.model_name.human} by ID" + argument :id, !types.ID + resolve GraphQL::RootElementResolver.new(model) + end + end + + # create an entry filed to retrive a paginated collection + connection model.name.underscore.pluralize.to_sym, created_type.connection_type do + description "Find all #{model.model_name.human.pluralize}" + resolve GraphQL::RootCollectionResolver.new(model) + end + + end + end + end + def self.type_kind(type) if SCALAR_TYPES[type] :scalar diff --git a/spec/lib/type_creator_spec.rb b/spec/lib/type_creator_spec.rb index 1e25b91fd..9948d77d0 100644 --- a/spec/lib/type_creator_spec.rb +++ b/spec/lib/type_creator_spec.rb @@ -1,11 +1,11 @@ require 'rails_helper' describe GraphQL::TypeCreator do - let(:type_creator) { GraphQL::TypeCreator.new } + let(:type_creator) { GraphQL::TypeCreator.new({}) } - describe "::create" do + describe "::create_type" do it "creates fields for Int attributes" do - debate_type = type_creator.create(Debate, { id: :integer }) + debate_type = type_creator.create_type(Debate, { id: :integer }) created_field = debate_type.fields['id'] expect(created_field).to be_a(GraphQL::Field) @@ -14,7 +14,7 @@ describe GraphQL::TypeCreator do end it "creates fields for String attributes" do - debate_type = type_creator.create(Debate, { title: :string }) + debate_type = type_creator.create_type(Debate, { title: :string }) created_field = debate_type.fields['title'] expect(created_field).to be_a(GraphQL::Field) @@ -23,8 +23,8 @@ describe GraphQL::TypeCreator do end it "creates connections for :belongs_to associations" do - user_type = type_creator.create(User, { id: :integer }) - debate_type = type_creator.create(Debate, { author: User }) + user_type = type_creator.create_type(User, { id: :integer }) + debate_type = type_creator.create_type(Debate, { author: User }) connection = debate_type.fields['author'] @@ -34,8 +34,8 @@ describe GraphQL::TypeCreator do end it "creates connections for :has_one associations" do - user_type = type_creator.create(User, { organization: Organization }) - organization_type = type_creator.create(Organization, { id: :integer }) + user_type = type_creator.create_type(User, { organization: Organization }) + organization_type = type_creator.create_type(Organization, { id: :integer }) connection = user_type.fields['organization'] @@ -45,8 +45,8 @@ describe GraphQL::TypeCreator do end it "creates connections for :has_many associations" do - comment_type = type_creator.create(Comment, { id: :integer }) - debate_type = type_creator.create(Debate, { comments: [Comment] }) + comment_type = type_creator.create_type(Comment, { id: :integer }) + debate_type = type_creator.create_type(Debate, { comments: [Comment] }) connection = debate_type.fields['comments']