From b9d2bc280140bfe79037020a3353c8aa1de2dfc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Sun, 8 Jan 2017 12:45:48 +0100 Subject: [PATCH] Wrote specs for QueryType creation --- config/initializers/graphql.rb | 4 +- lib/graph_ql/type_creator.rb | 70 +++++++++++++------------- spec/lib/graph_ql/type_creator_spec.rb | 35 ++++++++++++- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index 01d54ab19..971a565e4 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -23,10 +23,10 @@ api_config.each do |api_type_model, api_type_info| end type_creator = GraphQL::TypeCreator.new(api_type_definitions) -QueryRoot = type_creator.query_root +QueryType = type_creator.query_type ConsulSchema = GraphQL::Schema.define do - query QueryRoot + query QueryType max_depth 10 resolve_type -> (object, ctx) do diff --git a/lib/graph_ql/type_creator.rb b/lib/graph_ql/type_creator.rb index 9042d7f3f..b705839c3 100644 --- a/lib/graph_ql/type_creator.rb +++ b/lib/graph_ql/type_creator.rb @@ -10,21 +10,26 @@ module GraphQL string: GraphQL::STRING_TYPE } - attr_accessor :created_types, :api_type_definitions, :query_root + attr_accessor :created_types, :api_type_definitions, :query_type def initialize(api_type_definitions) @api_type_definitions = api_type_definitions @created_types = {} create_api_types - @query_root = create_query_root + create_query_type end - def create_api_types - api_type_definitions.each do |model, info| - self.create_type(model, info[:fields]) + def self.type_kind(type) + if SCALAR_TYPES[type] + :scalar + elsif type.class == Class + :simple_association + elsif type.class == Array + :paginated_association end end + # TODO: this method shouldn't be public just for testing purposes, ¿smell? def create_type(model, fields) type_creator = self @@ -55,44 +60,41 @@ module GraphQL return created_type # GraphQL::ObjectType end - def create_query_root - type_creator = self + private - GraphQL::ObjectType.define do - name 'QueryRoot' - description 'The query root for this schema' + def create_api_types + api_type_definitions.each do |model, info| + self.create_type(model, info[:fields]) + end + end - type_creator.created_types.each do |model, created_type| + def create_query_type + type_creator = self - # 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) + @query_type = GraphQL::ObjectType.define do + name 'QueryType' + description 'The root query for this schema' + + type_creator.created_types.each do |model, created_type| + # create 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 - 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) + # create connection to retrive a 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 - end - - def self.type_kind(type) - if SCALAR_TYPES[type] - :scalar - elsif type.class == Class - :simple_association - elsif type.class == Array - :paginated_association - end - end end end diff --git a/spec/lib/graph_ql/type_creator_spec.rb b/spec/lib/graph_ql/type_creator_spec.rb index 9948d77d0..d5291c961 100644 --- a/spec/lib/graph_ql/type_creator_spec.rb +++ b/spec/lib/graph_ql/type_creator_spec.rb @@ -1,7 +1,40 @@ require 'rails_helper' describe GraphQL::TypeCreator do - let(:type_creator) { GraphQL::TypeCreator.new({}) } + let(:api_type_definitions) { {} } + let(:type_creator) { GraphQL::TypeCreator.new(api_type_definitions) } + + describe "::query_type" do + let(:api_type_definitions) do + { + ProposalNotification => { fields: { title: 'string' } }, + Proposal => { fields: { id: 'integer', title: 'string' } } + } + end + let(:query_type) { type_creator.query_type } + + it 'has fields to retrieve single objects whose model fields included an ID' do + field = query_type.fields['proposal'] + proposal_type = type_creator.created_types[Proposal] + + expect(field).to be_a(GraphQL::Field) + expect(field.type).to eq(proposal_type) + expect(field.name).to eq('proposal') + end + + it 'does not have fields to retrieve single objects whose model fields did not include an ID' do + expect(query_type.fields['proposal_notification']).to be_nil + end + + it "has connections to retrieve collections of objects" do + connection = query_type.fields['proposals'] + proposal_type = type_creator.created_types[Proposal] + + expect(connection).to be_a(GraphQL::Field) + expect(connection.type).to eq(proposal_type.connection_type) + expect(connection.name).to eq('proposals') + end + end describe "::create_type" do it "creates fields for Int attributes" do