Split api types creation and query type creation between two different classes

This commit is contained in:
Alberto Miedes Garcés
2017-01-08 13:39:50 +01:00
parent b9d2bc2801
commit 398bc8c211
7 changed files with 211 additions and 194 deletions

View File

@@ -0,0 +1,58 @@
require 'rails_helper'
describe GraphQL::ApiTypesCreator do
let(:api_types_creator) { GraphQL::ApiTypesCreator.new( {} ) }
describe "::create_type" do
it "creates fields for Int attributes" do
debate_type = api_types_creator.create_type(Debate, { id: :integer })
created_field = debate_type.fields['id']
expect(created_field).to be_a(GraphQL::Field)
expect(created_field.type).to be_a(GraphQL::ScalarType)
expect(created_field.type.name).to eq('Int')
end
it "creates fields for String attributes" do
debate_type = api_types_creator.create_type(Debate, { title: :string })
created_field = debate_type.fields['title']
expect(created_field).to be_a(GraphQL::Field)
expect(created_field.type).to be_a(GraphQL::ScalarType)
expect(created_field.type.name).to eq('String')
end
it "creates connections for :belongs_to associations" do
user_type = api_types_creator.create_type(User, { id: :integer })
debate_type = api_types_creator.create_type(Debate, { author: User })
connection = debate_type.fields['author']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(user_type)
expect(connection.name).to eq('author')
end
it "creates connections for :has_one associations" do
user_type = api_types_creator.create_type(User, { organization: Organization })
organization_type = api_types_creator.create_type(Organization, { id: :integer })
connection = user_type.fields['organization']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(organization_type)
expect(connection.name).to eq('organization')
end
it "creates connections for :has_many associations" do
comment_type = api_types_creator.create_type(Comment, { id: :integer })
debate_type = api_types_creator.create_type(Debate, { comments: [Comment] })
connection = debate_type.fields['comments']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(comment_type.connection_type)
expect(connection.name).to eq('comments')
end
end
end

View File

@@ -0,0 +1,39 @@
require 'rails_helper'
describe GraphQL::QueryTypeCreator do
let(:api_type_definitions) do
{
ProposalNotification => { fields: { title: 'string' } },
Proposal => { fields: { id: 'integer', title: 'string' } }
}
end
let(:api_types_creator) { GraphQL::ApiTypesCreator.new(api_type_definitions) }
let(:created_api_types) { api_types_creator.create }
let(:query_type_creator) { GraphQL::QueryTypeCreator.new(created_api_types) }
describe "::create" do
let(:query_type) { query_type_creator.create }
it 'creates a QueryType with fields to retrieve single objects whose model fields included an ID' do
field = query_type.fields['proposal']
proposal_type = query_type_creator.created_api_types[Proposal]
expect(field).to be_a(GraphQL::Field)
expect(field.type).to eq(proposal_type)
expect(field.name).to eq('proposal')
end
it 'creates a QueryType without 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 "creates a QueryType with connections to retrieve collections of objects" do
connection = query_type.fields['proposals']
proposal_type = query_type_creator.created_api_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
end

View File

@@ -1,91 +0,0 @@
require 'rails_helper'
describe GraphQL::TypeCreator do
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
debate_type = type_creator.create_type(Debate, { id: :integer })
created_field = debate_type.fields['id']
expect(created_field).to be_a(GraphQL::Field)
expect(created_field.type).to be_a(GraphQL::ScalarType)
expect(created_field.type.name).to eq('Int')
end
it "creates fields for String attributes" do
debate_type = type_creator.create_type(Debate, { title: :string })
created_field = debate_type.fields['title']
expect(created_field).to be_a(GraphQL::Field)
expect(created_field.type).to be_a(GraphQL::ScalarType)
expect(created_field.type.name).to eq('String')
end
it "creates connections for :belongs_to associations" do
user_type = type_creator.create_type(User, { id: :integer })
debate_type = type_creator.create_type(Debate, { author: User })
connection = debate_type.fields['author']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(user_type)
expect(connection.name).to eq('author')
end
it "creates connections for :has_one associations" do
user_type = type_creator.create_type(User, { organization: Organization })
organization_type = type_creator.create_type(Organization, { id: :integer })
connection = user_type.fields['organization']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(organization_type)
expect(connection.name).to eq('organization')
end
it "creates connections for :has_many associations" do
comment_type = type_creator.create_type(Comment, { id: :integer })
debate_type = type_creator.create_type(Debate, { comments: [Comment] })
connection = debate_type.fields['comments']
expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(comment_type.connection_type)
expect(connection.name).to eq('comments')
end
end
end