Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #26296 - graphql: add Puppetclass queries #6607

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
36 changes: 28 additions & 8 deletions app/graphql/types/base_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ class BaseObject < GraphQL::Types::Relay::BaseObject
connection_type_class Connections::BaseConnection

class << self
def field(*args, **kwargs, &block)
null = kwargs.key?(:null) ? kwargs[:null] : nullable?(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, resolver: nil)
def has_many(name, type, resolver: nil)
if resolver
field name, type.connection_type, null: null, resolver: resolver
field name, type.connection_type, resolver: resolver
else
field name, type.connection_type, null: null,
field name, type.connection_type,
resolve: proc { |object| CollectionLoader.for(object.class, name).load(object) }
end
end

def belongs_to(name, type, null: true, resolver: nil)
def belongs_to(name, type, resolver: nil)
if resolver
field name, type, null: null, resolver: resolver
field name, type, resolver: resolver
else
field name, type, null: null,
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 @@ -34,6 +39,21 @@ def belongs_to(name, type, null: true, resolver: nil)
def model_class
"::#{self.to_s.demodulize}".safe_constantize
end

private

def nullable?(attribute)
return false if model_class.columns_hash[attribute.to_s]&.null == false

return false if model_class.validators_on(attribute).find do |v|
v.is_a?(ActiveModel::Validations::PresenceValidator) && v.options.none? { |o| [:if, :unless].include?(o) }
end

reflection = model_class.reflect_on_association(attribute)
return false if reflection && reflection.macro == :belongs_to && nullable?(reflection.foreign_key)

true
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, resolver: Resolvers::Domain::Subnets
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