Wrote specs for QueryType creation
This commit is contained in:
@@ -23,10 +23,10 @@ api_config.each do |api_type_model, api_type_info|
|
|||||||
end
|
end
|
||||||
|
|
||||||
type_creator = GraphQL::TypeCreator.new(api_type_definitions)
|
type_creator = GraphQL::TypeCreator.new(api_type_definitions)
|
||||||
QueryRoot = type_creator.query_root
|
QueryType = type_creator.query_type
|
||||||
|
|
||||||
ConsulSchema = GraphQL::Schema.define do
|
ConsulSchema = GraphQL::Schema.define do
|
||||||
query QueryRoot
|
query QueryType
|
||||||
max_depth 10
|
max_depth 10
|
||||||
|
|
||||||
resolve_type -> (object, ctx) do
|
resolve_type -> (object, ctx) do
|
||||||
|
|||||||
@@ -10,21 +10,26 @@ module GraphQL
|
|||||||
string: GraphQL::STRING_TYPE
|
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)
|
def initialize(api_type_definitions)
|
||||||
@api_type_definitions = api_type_definitions
|
@api_type_definitions = api_type_definitions
|
||||||
@created_types = {}
|
@created_types = {}
|
||||||
create_api_types
|
create_api_types
|
||||||
@query_root = create_query_root
|
create_query_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_api_types
|
def self.type_kind(type)
|
||||||
api_type_definitions.each do |model, info|
|
if SCALAR_TYPES[type]
|
||||||
self.create_type(model, info[:fields])
|
:scalar
|
||||||
|
elsif type.class == Class
|
||||||
|
:simple_association
|
||||||
|
elsif type.class == Array
|
||||||
|
:paginated_association
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: this method shouldn't be public just for testing purposes, ¿smell?
|
||||||
def create_type(model, fields)
|
def create_type(model, fields)
|
||||||
type_creator = self
|
type_creator = self
|
||||||
|
|
||||||
@@ -55,44 +60,41 @@ module GraphQL
|
|||||||
return created_type # GraphQL::ObjectType
|
return created_type # GraphQL::ObjectType
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_query_root
|
private
|
||||||
type_creator = self
|
|
||||||
|
|
||||||
GraphQL::ObjectType.define do
|
def create_api_types
|
||||||
name 'QueryRoot'
|
api_type_definitions.each do |model, info|
|
||||||
description 'The query root for this schema'
|
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
|
@query_type = GraphQL::ObjectType.define do
|
||||||
if type_creator.api_type_definitions[model][:fields][:id]
|
name 'QueryType'
|
||||||
field model.name.underscore.to_sym do
|
description 'The root query for this schema'
|
||||||
type created_type
|
|
||||||
description "Find one #{model.model_name.human} by ID"
|
type_creator.created_types.each do |model, created_type|
|
||||||
argument :id, !types.ID
|
# create field to retrive a single object
|
||||||
resolve GraphQL::RootElementResolver.new(model)
|
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
|
||||||
end
|
|
||||||
|
|
||||||
# create an entry filed to retrive a paginated collection
|
# create connection to retrive a collection
|
||||||
connection model.name.underscore.pluralize.to_sym, created_type.connection_type do
|
connection model.name.underscore.pluralize.to_sym, created_type.connection_type do
|
||||||
description "Find all #{model.model_name.human.pluralize}"
|
description "Find all #{model.model_name.human.pluralize}"
|
||||||
resolve GraphQL::RootCollectionResolver.new(model)
|
resolve GraphQL::RootCollectionResolver.new(model)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,40 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe GraphQL::TypeCreator do
|
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
|
describe "::create_type" do
|
||||||
it "creates fields for Int attributes" do
|
it "creates fields for Int attributes" do
|
||||||
|
|||||||
Reference in New Issue
Block a user