Add collection_field helper to BaseObject

Add a helper method in BaseObject to define fields with `connection_type`,
reducing code duplication and giving more context about the type of fields.
This commit is contained in:
taitus
2024-10-17 11:37:19 +02:00
parent a426537b4c
commit 2938ced41c
8 changed files with 20 additions and 19 deletions

View File

@@ -30,5 +30,9 @@ module Types
argument :id, ID, required: true, default_value: false
end
end
def self.collection_field(field_name, type, ...)
field(field_name, type.connection_type, ...)
end
end
end

View File

@@ -7,8 +7,8 @@ module Types
field :title, String, null: true
field :description, String, null: true
field :location, String, null: true
field :comments, Types::CommentType.connection_type, null: true
collection_field :comments, Types::CommentType, null: true
field :comments_count, Integer, null: true
field :milestones, Types::MilestoneType.connection_type, null: true
collection_field :milestones, Types::MilestoneType, null: true
end
end

View File

@@ -3,7 +3,7 @@ module Types
field :id, ID, null: false
field :name, String, null: true
field :phase, String, null: true
field :investments, Types::BudgetInvestmentType.connection_type, "Returns all investments", null: false
collection_field :investments, Types::BudgetInvestmentType, "Returns all investments", null: false
object_by_id_field :investment, Types::BudgetInvestmentType, "Returns investment for ID", null: false
def investments

View File

@@ -11,6 +11,6 @@ module Types
field :id, ID, null: false
field :public_author, Types::UserType, null: true
field :public_created_at, String, null: true
field :votes_for, Types::VoteType.connection_type, null: true
collection_field :votes_for, Types::VoteType, null: true
end
end

View File

@@ -3,7 +3,7 @@ module Types
field :cached_votes_down, Integer, null: true
field :cached_votes_total, Integer, null: true
field :cached_votes_up, Integer, null: true
field :comments, Types::CommentType.connection_type, null: true
collection_field :comments, Types::CommentType, null: true
field :comments_count, Integer, null: true
field :confidence_score, Integer, null: true
field :description, String, null: true
@@ -11,9 +11,9 @@ module Types
field :id, ID, null: false
field :public_author, Types::UserType, null: true
field :public_created_at, String, null: true
field :tags, Types::TagType.connection_type, null: true
collection_field :tags, Types::TagType, null: true
field :title, String, null: true
field :votes_for, Types::VoteType.connection_type, null: true
collection_field :votes_for, Types::VoteType, null: true
def tags
object.tags.public_for_api

View File

@@ -1,7 +1,7 @@
module Types
class ProposalType < Types::BaseObject
field :cached_votes_up, Integer, null: true
field :comments, Types::CommentType.connection_type, null: true
collection_field :comments, Types::CommentType, null: true
field :comments_count, Integer, null: true
field :confidence_score, Integer, null: true
field :description, String, null: true
@@ -9,18 +9,18 @@ module Types
field :geozone_id, Integer, null: true
field :hot_score, Integer, null: true
field :id, ID, null: false
field :proposal_notifications, Types::ProposalNotificationType.connection_type, null: true
collection_field :proposal_notifications, Types::ProposalNotificationType, null: true
field :public_author, Types::UserType, null: true
field :public_created_at, String, null: true
field :retired_at, GraphQL::Types::ISO8601DateTime, null: true
field :retired_explanation, String, null: true
field :retired_reason, String, null: true
field :summary, String, null: true
field :tags, Types::TagType.connection_type, null: true
collection_field :tags, Types::TagType, null: true
field :title, String, null: true
field :video_url, String, null: true
field :votes_for, Types::VoteType.connection_type, null: true
field :milestones, Types::MilestoneType.connection_type, null: true
collection_field :votes_for, Types::VoteType, null: true
collection_field :milestones, Types::MilestoneType, null: true
def tags
object.tags.public_for_api

View File

@@ -1,10 +1,7 @@
module Types
class QueryType < Types::BaseObject
def self.collection_and_object_by_id_fields(name, type)
field name.to_s.pluralize.to_sym,
type.connection_type,
"Returns all #{name.to_s.pluralize}",
null: false
collection_field name.to_s.pluralize.to_sym, type, "Returns all #{name.to_s.pluralize}", null: false
object_by_id_field name, type, "Returns #{name} for ID", null: false
end

View File

@@ -1,9 +1,9 @@
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
field :public_comments, Types::CommentType.connection_type, null: true
field :public_debates, Types::DebateType.connection_type, null: true
field :public_proposals, Types::ProposalType.connection_type, null: true
collection_field :public_comments, Types::CommentType, null: true
collection_field :public_debates, Types::DebateType, null: true
collection_field :public_proposals, Types::ProposalType, null: true
field :username, String, null: true
end
end