Simplify the way QueryTypeCreator is used
This commit is contained in:
@@ -20,8 +20,8 @@ class GraphqlController < ApplicationController
|
||||
private
|
||||
|
||||
def consul_schema
|
||||
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
|
||||
query_type = GraphQL::QueryTypeCreator.new(api_types).create
|
||||
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
|
||||
query_type = GraphQL::QueryTypeCreator.create(api_types)
|
||||
|
||||
GraphQL::Schema.define do
|
||||
query query_type
|
||||
|
||||
@@ -3,20 +3,12 @@ require 'graphql'
|
||||
module GraphQL
|
||||
class QueryTypeCreator
|
||||
|
||||
attr_accessor :created_api_types
|
||||
|
||||
def initialize(created_api_types)
|
||||
@created_api_types = created_api_types
|
||||
end
|
||||
|
||||
def create
|
||||
query_type_creator = self
|
||||
|
||||
def self.create(api_types)
|
||||
GraphQL::ObjectType.define do
|
||||
name 'QueryType'
|
||||
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']
|
||||
field model.graphql_field_name do
|
||||
type created_type
|
||||
|
||||
@@ -7,19 +7,16 @@ describe GraphQL::QueryTypeCreator do
|
||||
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) }
|
||||
let(:api_types) { GraphQL::ApiTypesCreator.new(api_type_definitions).create }
|
||||
|
||||
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
|
||||
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.type).to eq(api_types[Proposal])
|
||||
expect(field.name).to eq('proposal')
|
||||
end
|
||||
|
||||
@@ -29,10 +26,9 @@ describe GraphQL::QueryTypeCreator do
|
||||
|
||||
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.type).to eq(api_types[Proposal].connection_type)
|
||||
expect(connection.name).to eq('proposals')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
require 'rails_helper'
|
||||
|
||||
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
|
||||
query_type = GraphQL::QueryTypeCreator.new(api_types).create
|
||||
|
||||
api_types = GraphQL::ApiTypesCreator.new(API_TYPE_DEFINITIONS).create
|
||||
query_type = GraphQL::QueryTypeCreator.create(api_types)
|
||||
ConsulSchema = GraphQL::Schema.define do
|
||||
query query_type
|
||||
max_depth 12
|
||||
|
||||
Reference in New Issue
Block a user