Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/graphql')
-rw-r--r--lib/gitlab/graphql/authorize/authorize_resource.rb4
-rw-r--r--lib/gitlab/graphql/connection_collection_methods.rb13
-rw-r--r--lib/gitlab/graphql/connection_redaction.rb33
-rw-r--r--lib/gitlab/graphql/deferred.rb12
-rw-r--r--lib/gitlab/graphql/docs/helper.rb6
-rw-r--r--lib/gitlab/graphql/docs/templates/default.md.haml4
-rw-r--r--lib/gitlab/graphql/expose_permissions.rb2
-rw-r--r--lib/gitlab/graphql/externally_paginated_array.rb6
-rw-r--r--lib/gitlab/graphql/laziness.rb46
-rw-r--r--lib/gitlab/graphql/lazy.rb2
-rw-r--r--lib/gitlab/graphql/pagination/array_connection.rb15
-rw-r--r--lib/gitlab/graphql/pagination/connections.rb4
-rw-r--r--lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb9
-rw-r--r--lib/gitlab/graphql/pagination/keyset/connection.rb2
-rw-r--r--lib/gitlab/graphql/pagination/keyset/order_info.rb2
-rw-r--r--lib/gitlab/graphql/pagination/offset_active_record_relation_connection.rb2
16 files changed, 148 insertions, 14 deletions
diff --git a/lib/gitlab/graphql/authorize/authorize_resource.rb b/lib/gitlab/graphql/authorize/authorize_resource.rb
index c70127553fd..6ee446011d4 100644
--- a/lib/gitlab/graphql/authorize/authorize_resource.rb
+++ b/lib/gitlab/graphql/authorize/authorize_resource.rb
@@ -62,8 +62,8 @@ module Gitlab
end
end
- def raise_resource_not_available_error!
- raise Gitlab::Graphql::Errors::ResourceNotAvailable, RESOURCE_ACCESS_ERROR
+ def raise_resource_not_available_error!(msg = RESOURCE_ACCESS_ERROR)
+ raise Gitlab::Graphql::Errors::ResourceNotAvailable, msg
end
end
end
diff --git a/lib/gitlab/graphql/connection_collection_methods.rb b/lib/gitlab/graphql/connection_collection_methods.rb
new file mode 100644
index 00000000000..0e2c4a98bb6
--- /dev/null
+++ b/lib/gitlab/graphql/connection_collection_methods.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Graphql
+ module ConnectionCollectionMethods
+ extend ActiveSupport::Concern
+
+ included do
+ delegate :to_a, :size, :include?, :empty?, to: :nodes
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/graphql/connection_redaction.rb b/lib/gitlab/graphql/connection_redaction.rb
new file mode 100644
index 00000000000..5e037bb9f63
--- /dev/null
+++ b/lib/gitlab/graphql/connection_redaction.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Graphql
+ module ConnectionRedaction
+ class RedactionState
+ attr_reader :redactor
+ attr_reader :redacted_nodes
+
+ def redactor=(redactor)
+ @redactor = redactor
+ @redacted_nodes = nil
+ end
+
+ def redacted(&block)
+ @redacted_nodes ||= redactor.present? ? redactor.redact(yield) : yield
+ end
+ end
+
+ delegate :redactor=, to: :redaction_state
+
+ def nodes
+ redaction_state.redacted { super.to_a }
+ end
+
+ private
+
+ def redaction_state
+ @redaction_state ||= RedactionState.new
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/graphql/deferred.rb b/lib/gitlab/graphql/deferred.rb
new file mode 100644
index 00000000000..d0b36aabd5f
--- /dev/null
+++ b/lib/gitlab/graphql/deferred.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+# A marker interface that allows use to lazily resolve a wider range of value
+module Gitlab
+ module Graphql
+ module Deferred
+ def execute
+ raise NotImplementedError, 'Deferred classes must provide an execute method'
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/graphql/docs/helper.rb b/lib/gitlab/graphql/docs/helper.rb
index 503b1064b11..ad9e08e189c 100644
--- a/lib/gitlab/graphql/docs/helper.rb
+++ b/lib/gitlab/graphql/docs/helper.rb
@@ -13,6 +13,12 @@ module Gitlab
def auto_generated_comment
<<-MD.strip_heredoc
+ ---
+ stage: Plan
+ group: Project Management
+ info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
+ ---
+
<!---
This documentation is auto generated by a script.
diff --git a/lib/gitlab/graphql/docs/templates/default.md.haml b/lib/gitlab/graphql/docs/templates/default.md.haml
index 97df4233905..8f5a1788fa5 100644
--- a/lib/gitlab/graphql/docs/templates/default.md.haml
+++ b/lib/gitlab/graphql/docs/templates/default.md.haml
@@ -12,7 +12,7 @@
Each table below documents a GraphQL type. Types match loosely to models, but not all
fields and methods on a model are available via GraphQL.
- CAUTION: **Caution:**
+ WARNING:
Fields that are deprecated are marked with **{warning-solid}**.
Items (fields, enums, etc) that have been removed according to our [deprecation process](../index.md#deprecation-process) can be found
in [Removed Items](../removed_items.md).
@@ -21,7 +21,7 @@
:plain
## Object types
- Object types represent the resources that GitLab's GraphQL API can return.
+ Object types represent the resources that the GitLab GraphQL API can return.
They contain _fields_. Each field has its own type, which will either be one of the
basic GraphQL [scalar types](https://graphql.org/learn/schema/#scalar-types)
(e.g.: `String` or `Boolean`) or other object types.
diff --git a/lib/gitlab/graphql/expose_permissions.rb b/lib/gitlab/graphql/expose_permissions.rb
index 365b7cca24f..ab9ed354673 100644
--- a/lib/gitlab/graphql/expose_permissions.rb
+++ b/lib/gitlab/graphql/expose_permissions.rb
@@ -9,7 +9,7 @@ module Gitlab
field :user_permissions, permission_type,
description: description,
null: false,
- resolve: -> (obj, _, _) { obj }
+ method: :itself
end
end
end
diff --git a/lib/gitlab/graphql/externally_paginated_array.rb b/lib/gitlab/graphql/externally_paginated_array.rb
index 4797fe15cd3..873d7f4efdf 100644
--- a/lib/gitlab/graphql/externally_paginated_array.rb
+++ b/lib/gitlab/graphql/externally_paginated_array.rb
@@ -3,12 +3,12 @@
module Gitlab
module Graphql
class ExternallyPaginatedArray < Array
- attr_reader :previous_cursor, :next_cursor
+ attr_reader :start_cursor, :end_cursor
def initialize(previous_cursor, next_cursor, *args)
super(args)
- @previous_cursor = previous_cursor
- @next_cursor = next_cursor
+ @start_cursor = previous_cursor
+ @end_cursor = next_cursor
end
end
end
diff --git a/lib/gitlab/graphql/laziness.rb b/lib/gitlab/graphql/laziness.rb
new file mode 100644
index 00000000000..749d832919d
--- /dev/null
+++ b/lib/gitlab/graphql/laziness.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Graphql
+ # This module allows your class to easily defer and force values.
+ # Its methods are just sugar for calls to the Gitlab::Graphql::Lazy class.
+ #
+ # example:
+ #
+ # class MyAwesomeClass
+ # include ::Gitlab::Graphql::Laziness
+ #
+ # # takes a list of id and list of factors, and computes
+ # # sum of [SomeObject[i]#value * factor[i]]
+ # def resolve(ids:, factors:)
+ # ids.zip(factors)
+ # .map { |id, factor| promise_an_int(id, factor) }
+ # .map(&method(:force))
+ # .sum
+ # end
+ #
+ # # returns a promise for an Integer
+ # def (id, factor)
+ # thunk = SomeObject.lazy_find(id)
+ # defer { force(thunk).value * factor }
+ # end
+ # end
+ #
+ # In the example above, we use defer to delay forcing the batch-loaded
+ # item until we need it, and then we use `force` to consume the lazy values
+ #
+ # If `SomeObject.lazy_find(id)` batches correctly, calling
+ # `resolve` will only perform one batched load for all objects, rather than
+ # loading them individually before combining the results.
+ #
+ module Laziness
+ def defer(&block)
+ ::Gitlab::Graphql::Lazy.new(&block)
+ end
+
+ def force(lazy)
+ ::Gitlab::Graphql::Lazy.force(lazy)
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/graphql/lazy.rb b/lib/gitlab/graphql/lazy.rb
index 3cc11047387..54013cf4790 100644
--- a/lib/gitlab/graphql/lazy.rb
+++ b/lib/gitlab/graphql/lazy.rb
@@ -24,6 +24,8 @@ module Gitlab
value.force
when ::BatchLoader::GraphQL
value.sync
+ when ::Gitlab::Graphql::Deferred
+ value.execute
when ::GraphQL::Execution::Lazy
value.value # part of the private api, but we can force this as well
when ::Concurrent::Promise
diff --git a/lib/gitlab/graphql/pagination/array_connection.rb b/lib/gitlab/graphql/pagination/array_connection.rb
new file mode 100644
index 00000000000..efc912eaeca
--- /dev/null
+++ b/lib/gitlab/graphql/pagination/array_connection.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+# We use the Keyset / Stable cursor connection by default for ActiveRecord::Relation.
+# However, there are times when that may not be powerful enough (yet), and we
+# want to use standard offset pagination.
+module Gitlab
+ module Graphql
+ module Pagination
+ class ArrayConnection < ::GraphQL::Pagination::ArrayConnection
+ prepend ::Gitlab::Graphql::ConnectionRedaction
+ include ::Gitlab::Graphql::ConnectionCollectionMethods
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/graphql/pagination/connections.rb b/lib/gitlab/graphql/pagination/connections.rb
index 8f37fa3f474..54a84be4274 100644
--- a/lib/gitlab/graphql/pagination/connections.rb
+++ b/lib/gitlab/graphql/pagination/connections.rb
@@ -12,6 +12,10 @@ module Gitlab
schema.connections.add(
Gitlab::Graphql::ExternallyPaginatedArray,
Gitlab::Graphql::Pagination::ExternallyPaginatedArrayConnection)
+
+ schema.connections.add(
+ Array,
+ Gitlab::Graphql::Pagination::ArrayConnection)
end
end
end
diff --git a/lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb b/lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb
index 12e047420bf..ce309df65d9 100644
--- a/lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb
+++ b/lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb
@@ -5,13 +5,10 @@ module Gitlab
module Graphql
module Pagination
class ExternallyPaginatedArrayConnection < GraphQL::Pagination::ArrayConnection
- def start_cursor
- items.previous_cursor
- end
+ include ::Gitlab::Graphql::ConnectionCollectionMethods
+ prepend ::Gitlab::Graphql::ConnectionRedaction
- def end_cursor
- items.next_cursor
- end
+ delegate :start_cursor, :end_cursor, to: :items
def next_page?
end_cursor.present?
diff --git a/lib/gitlab/graphql/pagination/keyset/connection.rb b/lib/gitlab/graphql/pagination/keyset/connection.rb
index 252f6371765..2ad8d2f7ab7 100644
--- a/lib/gitlab/graphql/pagination/keyset/connection.rb
+++ b/lib/gitlab/graphql/pagination/keyset/connection.rb
@@ -31,6 +31,8 @@ module Gitlab
module Keyset
class Connection < GraphQL::Pagination::ActiveRecordRelationConnection
include Gitlab::Utils::StrongMemoize
+ include ::Gitlab::Graphql::ConnectionCollectionMethods
+ prepend ::Gitlab::Graphql::ConnectionRedaction
# rubocop: disable Naming/PredicateName
# https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo.Fields
diff --git a/lib/gitlab/graphql/pagination/keyset/order_info.rb b/lib/gitlab/graphql/pagination/keyset/order_info.rb
index f3ce3a10703..d37264c1343 100644
--- a/lib/gitlab/graphql/pagination/keyset/order_info.rb
+++ b/lib/gitlab/graphql/pagination/keyset/order_info.rb
@@ -127,3 +127,5 @@ module Gitlab
end
end
end
+
+Gitlab::Graphql::Pagination::Keyset::OrderInfo.prepend_if_ee('EE::Gitlab::Graphql::Pagination::Keyset::OrderInfo')
diff --git a/lib/gitlab/graphql/pagination/offset_active_record_relation_connection.rb b/lib/gitlab/graphql/pagination/offset_active_record_relation_connection.rb
index 33f84701562..4a57b7aceca 100644
--- a/lib/gitlab/graphql/pagination/offset_active_record_relation_connection.rb
+++ b/lib/gitlab/graphql/pagination/offset_active_record_relation_connection.rb
@@ -7,6 +7,8 @@ module Gitlab
module Graphql
module Pagination
class OffsetActiveRecordRelationConnection < GraphQL::Pagination::ActiveRecordRelationConnection
+ prepend ::Gitlab::Graphql::ConnectionRedaction
+ include ::Gitlab::Graphql::ConnectionCollectionMethods
end
end
end