Wrote specs for QueryType creation

This commit is contained in:
Alberto Miedes Garcés
2017-01-08 12:45:48 +01:00
parent 7027164867
commit b9d2bc2801
3 changed files with 72 additions and 37 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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