diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2019-01-31 03:00:30 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2019-02-14 15:52:17 +0800 |
commit | 91e9e50a1153021257abaa528498d7a82cc3350f (patch) | |
tree | 654dd11658da179f0bd4bd0eea5e4d63718c99b1 /app | |
parent | 1322146bbf5c76403db10969f1af6540717b1cdf (diff) | |
download | gitlab-ce-91e9e50a1153021257abaa528498d7a82cc3350f.tar.gz |
Add field mergeRequests for project in GraphQL
And fix the tests so that it won't run into circular paths.
Diffstat (limited to 'app')
-rw-r--r-- | app/graphql/mutations/merge_requests/base.rb | 3 | ||||
-rw-r--r-- | app/graphql/resolvers/base_resolver.rb | 7 | ||||
-rw-r--r-- | app/graphql/resolvers/merge_requests_resolver.rb (renamed from app/graphql/resolvers/merge_request_resolver.rb) | 19 | ||||
-rw-r--r-- | app/graphql/types/project_type.rb | 9 |
4 files changed, 32 insertions, 6 deletions
diff --git a/app/graphql/mutations/merge_requests/base.rb b/app/graphql/mutations/merge_requests/base.rb index 54f01c99d78..7d0cb777ad1 100644 --- a/app/graphql/mutations/merge_requests/base.rb +++ b/app/graphql/mutations/merge_requests/base.rb @@ -25,7 +25,8 @@ module Mutations def find_object(project_path:, iid:) project = resolve_project(full_path: project_path) - resolver = Resolvers::MergeRequestResolver.new(object: project, context: context) + resolver = Resolvers::MergeRequestsResolver + .single.new(object: project, context: context) resolver.resolve(iid: iid) end diff --git a/app/graphql/resolvers/base_resolver.rb b/app/graphql/resolvers/base_resolver.rb index 459933af9d3..063def75d38 100644 --- a/app/graphql/resolvers/base_resolver.rb +++ b/app/graphql/resolvers/base_resolver.rb @@ -2,5 +2,12 @@ module Resolvers class BaseResolver < GraphQL::Schema::Resolver + def self.single + @single ||= Class.new(self) do + def resolve(**args) + super.first + end + end + end end end diff --git a/app/graphql/resolvers/merge_request_resolver.rb b/app/graphql/resolvers/merge_requests_resolver.rb index d047ce9e3a1..15055db633a 100644 --- a/app/graphql/resolvers/merge_request_resolver.rb +++ b/app/graphql/resolvers/merge_requests_resolver.rb @@ -1,19 +1,30 @@ # frozen_string_literal: true module Resolvers - class MergeRequestResolver < BaseResolver + class MergeRequestsResolver < BaseResolver argument :iid, GraphQL::ID_TYPE, - required: true, + required: false, description: 'The IID of the merge request, e.g., "1"' + argument :iids, [GraphQL::ID_TYPE], + required: false, + description: 'The list of IIDs of issues, e.g., [1, 2]' + type Types::MergeRequestType, null: true alias_method :project, :object - # rubocop: disable CodeReuse/ActiveRecord - def resolve(iid:) + def resolve(**args) return unless project.present? + args[:iids] ||= [args[:iid]] + + args[:iids].map(&method(:batch_load)) + .select(&:itself) # .compact doesn't work on BatchLoader + end + + # rubocop: disable CodeReuse/ActiveRecord + def batch_load(iid) BatchLoader.for(iid.to_s).batch(key: project) do |iids, loader, args| args[:key].merge_requests.where(iid: iids).each do |mr| loader.call(mr.iid.to_s, mr) diff --git a/app/graphql/types/project_type.rb b/app/graphql/types/project_type.rb index 050706f97be..daced8af1c2 100644 --- a/app/graphql/types/project_type.rb +++ b/app/graphql/types/project_type.rb @@ -66,10 +66,17 @@ module Types field :only_allow_merge_if_all_discussions_are_resolved, GraphQL::BOOLEAN_TYPE, null: true field :printing_merge_request_link_enabled, GraphQL::BOOLEAN_TYPE, null: true + field :merge_requests, + Types::MergeRequestType.connection_type, + null: true, + resolver: Resolvers::MergeRequestsResolver do + # authorize :read_merge_request + end + field :merge_request, Types::MergeRequestType, null: true, - resolver: Resolvers::MergeRequestResolver do + resolver: Resolvers::MergeRequestsResolver.single do authorize :read_merge_request end |