module Decidim::QueryExtensions

This module's job is to extend the API with custom fields related to decidim-core.

Public Class Methods

included(type) click to toggle source

Public: Extends a type with `decidim-core`'s fields.

type - A GraphQL::BaseType to extend.

Returns nothing.

# File lib/decidim/query_extensions.rb, line 12
def self.included(type)
  type.field :participatory_processes,
             [Decidim::ParticipatoryProcesses::ParticipatoryProcessType],
             null: true,
             description: "Lists all participatory_processes" do
    argument :filter, Decidim::ParticipatoryProcesses::ParticipatoryProcessInputFilter, "This argument let's you filter the results", required: false
    argument :order, Decidim::ParticipatoryProcesses::ParticipatoryProcessInputSort, "This argument let's you order the results", required: false
  end

  type.field :participatory_process,
             Decidim::ParticipatoryProcesses::ParticipatoryProcessType,
             null: true,
             description: "Finds a participatory_process" do
    argument :id, GraphQL::Types::ID, "The ID of the participatory space", required: false
    argument :slug, String, "The slug of the participatory process", required: false
  end

  type.field :component, Decidim::Core::ComponentInterface, null: true do
    description "Lists the components this space contains."
    argument :id, GraphQL::Types::ID, required: true, description: "The ID of the component to be found"
  end

  type.field :session, Core::SessionType, description: "Return's information about the logged in user", null: true

  type.field :decidim, Core::DecidimType, "Decidim's framework properties.", null: true

  type.field :organization, Core::OrganizationType, "The current organization", null: true

  type.field :hashtags, [Core::HashtagType], null: true, description: "The hashtags for current organization" do
    argument :name, GraphQL::Types::String, "The name of the hashtag", required: false
  end

  type.field :metrics, type: [Decidim::Core::MetricType], null: true do
    argument :names, [GraphQL::Types::String], "The names of the metrics you want to retrieve", camelize: false, required: false
    argument :space_type, GraphQL::Types::String, "The type of ParticipatorySpace you want to filter with", camelize: false, required: false
    argument :space_id, GraphQL::Types::Int, "The ID of ParticipatorySpace you want to filter with", camelize: false, required: false
  end

  type.field :user,
             type: Core::AuthorInterface, null: true,
             description: "A participant (user or group) in the current organization" do
    argument :id, GraphQL::Types::ID, "The ID of the participant", required: false
    argument :nickname, GraphQL::Types::String, "The @nickname of the participant", required: false
  end

  type.field :users,
             type: [Core::AuthorInterface], null: true,
             description: "The participants (users or groups) for the current organization" do
    argument :order, Decidim::Core::UserEntityInputSort, "Provides several methods to order the results", required: false
    argument :filter, Decidim::Core::UserEntityInputFilter, "Provides several methods to filter the results", required: false
  end
end

Public Instance Methods

component(id: {}) click to toggle source
# File lib/decidim/query_extensions.rb, line 75
def component(id: {})
  component = Decidim::Component.published.find_by(id: id)
  component&.organization == context[:current_organization] ? component : nil
end
decidim() click to toggle source
# File lib/decidim/query_extensions.rb, line 84
def decidim
  Decidim
end
hashtags(name: nil) click to toggle source
# File lib/decidim/query_extensions.rb, line 92
def hashtags(name: nil)
  Decidim::HashtagsResolver.new(context[:current_organization], name).hashtags
end
metrics(names: [], space_type: nil, space_id: nil) click to toggle source
# File lib/decidim/query_extensions.rb, line 96
def metrics(names: [], space_type: nil, space_id: nil)
  manifests = if names.blank?
                Decidim.metrics_registry.all
              else
                Decidim.metrics_registry.all.select do |manifest|
                  names.include?(manifest.metric_name.to_s)
                end
              end
  filters = {}
  if space_type.present? && space_id.present?
    filters[:participatory_space_type] = space_type
    filters[:participatory_space_id] = space_id
  end

  manifests.map do |manifest|
    Decidim::Core::MetricResolver.new(manifest.metric_name, context[:current_organization], filters)
  end
end
organization() click to toggle source
# File lib/decidim/query_extensions.rb, line 88
def organization
  context[:current_organization]
end
participatory_process(id: nil, slug: nil) click to toggle source
# File lib/decidim/query_extensions.rb, line 70
def participatory_process(id: nil, slug: nil)
  manifest = Decidim.participatory_space_manifests.select { |m| m.name == :participatory_processes }.first
  Decidim::Core::ParticipatorySpaceFinderBase.new(manifest: manifest).call(object, { id: id, slug: slug }, context)
end
participatory_processes(filter: {}, order: {}) click to toggle source
# File lib/decidim/query_extensions.rb, line 65
def participatory_processes(filter: {}, order: {})
  manifest = Decidim.participatory_space_manifests.select { |m| m.name == :participatory_processes }.first
  Decidim::Core::ParticipatorySpaceListBase.new(manifest: manifest).call(object, { filter: filter, order: order }, context)
end
session() click to toggle source
# File lib/decidim/query_extensions.rb, line 80
def session
  context[:current_user]
end
user(id: nil, nickname: nil) click to toggle source
# File lib/decidim/query_extensions.rb, line 115
def user(id: nil, nickname: nil)
  Core::UserEntityFinder.new.call(object, { id: id, nickname: nickname }, context)
end
users(filter: {}, order: {}) click to toggle source
# File lib/decidim/query_extensions.rb, line 119
def users(filter: {}, order: {})
  Core::UserEntityList.new.call(object, { filter: filter, order: order }, context)
end