Simplify the way QueryTypeCreator is used

This commit is contained in:
Alberto Miedes Garcés
2017-01-27 12:54:33 +01:00
parent 073ce38a8d
commit 620c83fb69
4 changed files with 10 additions and 23 deletions

View File

@@ -20,8 +20,8 @@ class GraphqlController < ApplicationController
private private
def consul_schema def consul_schema
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
query_type = GraphQL::QueryTypeCreator.new(api_types).create query_type = GraphQL::QueryTypeCreator.create(api_types)
GraphQL::Schema.define do GraphQL::Schema.define do
query query_type query query_type

View File

@@ -3,20 +3,12 @@ require 'graphql'
module GraphQL module GraphQL
class QueryTypeCreator class QueryTypeCreator
attr_accessor :created_api_types def self.create(api_types)
def initialize(created_api_types)
@created_api_types = created_api_types
end
def create
query_type_creator = self
GraphQL::ObjectType.define do GraphQL::ObjectType.define do
name 'QueryType' name 'QueryType'
description 'The root query for the schema' description 'The root query for the schema'
query_type_creator.created_api_types.each do |model, created_type| api_types.each do |model, created_type|
if created_type.fields['id'] if created_type.fields['id']
field model.graphql_field_name do field model.graphql_field_name do
type created_type type created_type

View File

@@ -7,19 +7,16 @@ describe GraphQL::QueryTypeCreator do
Proposal => { fields: { id: :integer, title: :string } } Proposal => { fields: { id: :integer, title: :string } }
} }
end end
let(:api_types_creator) { GraphQL::ApiTypesCreator.new(api_type_definitions) } let(:api_types) { GraphQL::ApiTypesCreator.new(api_type_definitions).create }
let(:created_api_types) { api_types_creator.create }
let(:query_type_creator) { GraphQL::QueryTypeCreator.new(created_api_types) }
describe "::create" do describe "::create" do
let(:query_type) { query_type_creator.create } let(:query_type) { GraphQL::QueryTypeCreator.create(api_types) }
it 'creates a QueryType with fields to retrieve single objects whose model fields included an ID' do it 'creates a QueryType with fields to retrieve single objects whose model fields included an ID' do
field = query_type.fields['proposal'] field = query_type.fields['proposal']
proposal_type = query_type_creator.created_api_types[Proposal]
expect(field).to be_a(GraphQL::Field) expect(field).to be_a(GraphQL::Field)
expect(field.type).to eq(proposal_type) expect(field.type).to eq(api_types[Proposal])
expect(field.name).to eq('proposal') expect(field.name).to eq('proposal')
end end
@@ -29,10 +26,9 @@ describe GraphQL::QueryTypeCreator do
it "creates a QueryType with connections to retrieve collections of objects" do it "creates a QueryType with connections to retrieve collections of objects" do
connection = query_type.fields['proposals'] connection = query_type.fields['proposals']
proposal_type = query_type_creator.created_api_types[Proposal]
expect(connection).to be_a(GraphQL::Field) expect(connection).to be_a(GraphQL::Field)
expect(connection.type).to eq(proposal_type.connection_type) expect(connection.type).to eq(api_types[Proposal].connection_type)
expect(connection.name).to eq('proposals') expect(connection.name).to eq('proposals')
end end
end end

View File

@@ -1,8 +1,7 @@
require 'rails_helper' require 'rails_helper'
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
query_type = GraphQL::QueryTypeCreator.new(api_types).create query_type = GraphQL::QueryTypeCreator.create(api_types)
ConsulSchema = GraphQL::Schema.define do ConsulSchema = GraphQL::Schema.define do
query query_type query query_type
max_depth 12 max_depth 12