Simplify the way QueryTypeCreator is used
This commit is contained in:
@@ -21,7 +21,7 @@ class GraphqlController < ApplicationController
|
|||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user