Skip to content

Commit

Permalink
Fixes #26296 - graphql: add Puppetclass queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kamils-iRonin committed Mar 26, 2019
1 parent 3fd1e47 commit edb9cdb
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/graphql/types/architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Architecture < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :name, String
end
end
39 changes: 33 additions & 6 deletions app/graphql/types/base_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ class BaseObject < GraphQL::Types::Relay::BaseObject
connection_type_class Connections::BaseConnection

class << self
def field(*args, **kwargs, &block)
null = kwargs.key?(:null) ? kwargs[:null] : !is_required(model_class, args.first)
super(*args, **kwargs.except(:null), null: null, &block)
end

def timestamps
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
field :created_at, GraphQL::Types::ISO8601DateTime
field :updated_at, GraphQL::Types::ISO8601DateTime
end

def has_many(name, type, null: true)
field name, type.connection_type, null: null,
def has_many(name, type)
field name, type.connection_type,
resolve: proc { |object| CollectionLoader.for(object.class, name).load(object) }
end

def belongs_to(name, type, null: true)
field name, type, null: null,
def belongs_to(name, type)
field name, type,
resolve: (proc do |object|
foreign_key = object.class.reflect_on_association(name).foreign_key
RecordLoader.for(type.model_class).load(object.send(foreign_key))
Expand All @@ -26,6 +31,28 @@ def belongs_to(name, type, null: true)
def model_class
"::#{self.to_s.demodulize}".safe_constantize
end

private

# https://github.com/goco-inc/graphql-activerecord/blob/7e9faafe221ad2385176c59185928a8c6db93dd2/lib/graphql/models/reflection.rb#L18-L35
# Determines if the attribute (or association) is required by examining presence validators
# and the nullability of the column in the database
def is_required(model_class, attr_or_assoc)
# Check for non-nullability on the column itself
return true if model_class.columns_hash[attr_or_assoc.to_s]&.null == false

# Check for a presence validator on the association
return true if model_class.validators_on(attr_or_assoc)
.select { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
.reject { |v| v.options.include?(:if) || v.options.include?(:unless) }
.any?

# If it's a belongs_to association, check for nullability on the foreign key
reflection = model_class.reflect_on_association(attr_or_assoc)
return true if reflection && reflection.macro == :belongs_to && is_required(model_class, reflection.foreign_key)

false
end
end
end
end
4 changes: 2 additions & 2 deletions app/graphql/types/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Domain < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :fullname, String, null: true
field :name, String
field :fullname, String

has_many :subnets, Types::Subnet
end
Expand Down
5 changes: 4 additions & 1 deletion app/graphql/types/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Environment < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :name, String

has_many :locations, Types::Location
has_many :organizations, Types::Organization
end
end
6 changes: 3 additions & 3 deletions app/graphql/types/fact_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class FactName < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :short_name, String, null: true
field :type, String, null: true
field :name, String
field :short_name, String
field :type, String

has_many :fact_values, Types::FactValue
end
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/fact_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class FactValue < BaseObject

global_id_field :id
timestamps
field :value, String, null: true
field :value, String

belongs_to :fact_name, Types::FactName
belongs_to :host, Types::Host
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Host < BaseObject

global_id_field :id
timestamps
field :name, String, null: false
field :name, String

belongs_to :environment, Types::Environment
belongs_to :model, Types::Model
Expand Down
7 changes: 5 additions & 2 deletions app/graphql/types/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ class Location < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :title, String, null: true
field :name, String
field :title, String

has_many :environments, Types::Environment
has_many :puppetclasses, Types::Puppetclass
end
end
8 changes: 4 additions & 4 deletions app/graphql/types/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Model < BaseObject

global_id_field :id
timestamps
field :name, String, null: false
field :info, String, null: true
field :vendorClass, String, null: true
field :hardwareModel, String, null: true
field :name, String
field :info, String
field :vendorClass, String
field :hardwareModel, String

has_many :hosts, Types::Host
end
Expand Down
8 changes: 4 additions & 4 deletions app/graphql/types/operatingsystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Operatingsystem < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :title, String, null: true
field :type, String, null: true
field :fullname, String, null: true
field :name, String
field :title, String
field :type, String
field :fullname, String
end
end
7 changes: 5 additions & 2 deletions app/graphql/types/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ class Organization < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :title, String, null: true
field :name, String
field :title, String

has_many :environments, Types::Environment
has_many :puppetclasses, Types::Puppetclass
end
end
13 changes: 13 additions & 0 deletions app/graphql/types/puppetclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Types
class Puppetclass < BaseObject
description 'A Puppetclass'

global_id_field :id
timestamps
field :name, String

has_many :environments, Types::Environment
has_many :locations, Types::Location
has_many :organizations, Types::Organization
end
end
3 changes: 3 additions & 0 deletions app/graphql/types/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ def collection_field(name, type)

record_field :environment, Types::Environment
collection_field :environments, Types::Environment

record_field :puppetclass, Types::Puppetclass
collection_field :puppetclasses, Types::Puppetclass
end
end
4 changes: 2 additions & 2 deletions app/graphql/types/smart_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SmartProxy < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :url, String, null: true
field :name, String
field :url, String
end
end
32 changes: 16 additions & 16 deletions app/graphql/types/subnet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class Subnet < BaseObject

global_id_field :id
timestamps
field :name, String, null: true
field :type, String, null: true
field :network, String, null: true
field :mask, String, null: true
field :priority, Int, null: true
field :vlanid, Int, null: true
field :gateway, String, null: true
field :dns_primary, String, null: true
field :dns_secondary, String, null: true
field :from, String, null: true
field :to, String, null: true
field :ipam, String, null: true
field :boot_mode, String, null: true
field :network_address, String, null: true
field :network_type, String, null: true
field :cidr, Int, null: true
field :name, String
field :type, String
field :network, String
field :mask, String
field :priority, Int
field :vlanid, Int
field :gateway, String
field :dns_primary, String
field :dns_secondary, String
field :from, String
field :to, String
field :ipam, String
field :boot_mode, String
field :network_address, String
field :network_type, String
field :cidr, Int

has_many :domains, Types::Domain
end
Expand Down
20 changes: 10 additions & 10 deletions app/graphql/types/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ class User < BaseObject

global_id_field :id
timestamps
field :login, String, null: false
field :admin, Boolean, null: false
field :mail, String, null: true
field :firstname, String, null: true
field :lastname, String, null: true
field :fullname, String, null: true
field :locale, Types::LocaleEnum, null: true
field :timezone, Types::TimezoneEnum, null: true
field :description, String, null: true
field :last_login_on, GraphQL::Types::ISO8601DateTime, null: true
field :login, String
field :admin, Boolean
field :mail, String
field :firstname, String
field :lastname, String
field :fullname, String
field :locale, Types::LocaleEnum
field :timezone, Types::TimezoneEnum
field :description, String
field :last_login_on, GraphQL::Types::ISO8601DateTime

belongs_to :default_location, Types::Location
belongs_to :default_organization, Types::Organization
Expand Down
4 changes: 2 additions & 2 deletions app/graphql/types/usergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Usergroup < BaseObject

global_id_field :id
timestamps
field :name, String, null: false
field :admin, Boolean, null: false
field :name, String
field :admin, Boolean
end
end
38 changes: 37 additions & 1 deletion test/graphql/queries/environment_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class Queries::EnvironmentQueryTest < ActiveSupport::TestCase
createdAt
updatedAt
name
locations {
totalCount
edges {
node {
id
}
}
}
organizations {
totalCount
edges {
node {
id
}
}
}
}
}
GRAPHQL
Expand All @@ -27,7 +43,27 @@ class Queries::EnvironmentQueryTest < ActiveSupport::TestCase
'id' => environment_global_id,
'createdAt' => environment.created_at.utc.iso8601,
'updatedAt' => environment.updated_at.utc.iso8601,
'name' => environment.name
'name' => environment.name,
'locations' => {
'totalCount' => environment.locations.count,
'edges' => environment.locations.sort_by(&:id).map do |location|
{
'node' => {
'id' => Foreman::GlobalId.for(location)
}
}
end
},
'organizations' => {
'totalCount' => environment.organizations.count,
'edges' => environment.organizations.sort_by(&:id).map do |organization|
{
'node' => {
'id' => Foreman::GlobalId.for(organization)
}
}
end
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/graphql/queries/fact_name_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Queries::FactNameQueryTest < ActiveSupport::TestCase
'edges' => fact_name.fact_values.map do |fv|
{
'node' => {
'id' => Foreman::GlobalId.for(fact_value)
'id' => Foreman::GlobalId.for(fv)
}
}
end
Expand Down

0 comments on commit edb9cdb

Please sign in to comment.