summaryrefslogtreecommitdiff
path: root/app/graphql/types/notes
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2019-06-07 19:13:26 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2019-06-14 12:36:27 +0200
commitb6ff5f1e141162e701c33647aae5015e5d42cc11 (patch)
treefb6ec57e96dc811d07c75d6b32f9471263c85166 /app/graphql/types/notes
parent8934ddbb47d24dac937351588bc28551bd7654e7 (diff)
downloadgitlab-ce-b6ff5f1e141162e701c33647aae5015e5d42cc11.tar.gz
Expose comments on Noteables in GraphQL
This exposes `Note`s on Issues & MergeRequests using a `Types::Notes::NoteableType` in GraphQL. Exposing notes on a new type can be done by implementing the `NoteableType` interface on the type. The presented object should be a `Noteable`.
Diffstat (limited to 'app/graphql/types/notes')
-rw-r--r--app/graphql/types/notes/diff_position_type.rb46
-rw-r--r--app/graphql/types/notes/discussion_type.rb15
-rw-r--r--app/graphql/types/notes/note_type.rb46
-rw-r--r--app/graphql/types/notes/noteable_type.rb25
-rw-r--r--app/graphql/types/notes/position_type_enum.rb13
5 files changed, 145 insertions, 0 deletions
diff --git a/app/graphql/types/notes/diff_position_type.rb b/app/graphql/types/notes/diff_position_type.rb
new file mode 100644
index 00000000000..104ccb79bbb
--- /dev/null
+++ b/app/graphql/types/notes/diff_position_type.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module Types
+ module Notes
+ class DiffPositionType < BaseObject
+ graphql_name 'DiffPosition'
+
+ field :head_sha, GraphQL::STRING_TYPE, null: false,
+ description: "The sha of the head at the time the comment was made"
+ field :base_sha, GraphQL::STRING_TYPE, null: true,
+ description: "The merge base of the branch the comment was made on"
+ field :start_sha, GraphQL::STRING_TYPE, null: false,
+ description: "The sha of the branch being compared against"
+
+ field :file_path, GraphQL::STRING_TYPE, null: false,
+ description: "The path of the file that was changed"
+ field :old_path, GraphQL::STRING_TYPE, null: true,
+ description: "The path of the file on the start sha."
+ field :new_path, GraphQL::STRING_TYPE, null: true,
+ description: "The path of the file on the head sha."
+ field :position_type, Types::Notes::PositionTypeEnum, null: false
+
+ # Fields for text positions
+ field :old_line, GraphQL::INT_TYPE, null: true,
+ description: "The line on start sha that was changed",
+ resolve: -> (position, _args, _ctx) { position.old_line if position.on_text? }
+ field :new_line, GraphQL::INT_TYPE, null: true,
+ description: "The line on head sha that was changed",
+ resolve: -> (position, _args, _ctx) { position.new_line if position.on_text? }
+
+ # Fields for image positions
+ field :x, GraphQL::INT_TYPE, null: true,
+ description: "The X postion on which the comment was made",
+ resolve: -> (position, _args, _ctx) { position.x if position.on_image? }
+ field :y, GraphQL::INT_TYPE, null: true,
+ description: "The Y position on which the comment was made",
+ resolve: -> (position, _args, _ctx) { position.y if position.on_image? }
+ field :width, GraphQL::INT_TYPE, null: true,
+ description: "The total width of the image",
+ resolve: -> (position, _args, _ctx) { position.width if position.on_image? }
+ field :height, GraphQL::INT_TYPE, null: true,
+ description: "The total height of the image",
+ resolve: -> (position, _args, _ctx) { position.height if position.on_image? }
+ end
+ end
+end
diff --git a/app/graphql/types/notes/discussion_type.rb b/app/graphql/types/notes/discussion_type.rb
new file mode 100644
index 00000000000..c4691942f2d
--- /dev/null
+++ b/app/graphql/types/notes/discussion_type.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Types
+ module Notes
+ class DiscussionType < BaseObject
+ graphql_name 'Discussion'
+
+ authorize :read_note
+
+ field :id, GraphQL::ID_TYPE, null: false
+ field :created_at, Types::TimeType, null: false
+ field :notes, Types::Notes::NoteType.connection_type, null: false, description: "All notes in the discussion"
+ end
+ end
+end
diff --git a/app/graphql/types/notes/note_type.rb b/app/graphql/types/notes/note_type.rb
new file mode 100644
index 00000000000..85c55d16ac2
--- /dev/null
+++ b/app/graphql/types/notes/note_type.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+module Types
+ module Notes
+ class NoteType < BaseObject
+ graphql_name 'Note'
+
+ authorize :read_note
+
+ expose_permissions Types::PermissionTypes::Note
+
+ field :id, GraphQL::ID_TYPE, null: false
+
+ field :project, Types::ProjectType,
+ null: true,
+ description: "The project this note is associated to",
+ resolve: -> (note, args, context) { Gitlab::Graphql::Loaders::BatchModelLoader.new(Project, note.project_id).find }
+
+ field :author, Types::UserType,
+ null: false,
+ description: "The user who wrote this note",
+ resolve: -> (note, args, context) { Gitlab::Graphql::Loaders::BatchModelLoader.new(User, note.author_id).find }
+
+ field :resolved_by, Types::UserType,
+ null: true,
+ description: "The user that resolved the discussion",
+ resolve: -> (note, _args, _context) { Gitlab::Graphql::Loaders::BatchModelLoader.new(User, note.resolved_by_id).find }
+
+ field :system, GraphQL::BOOLEAN_TYPE,
+ null: false,
+ description: "Whether or not this note was created by the system or by a user"
+
+ field :body, GraphQL::STRING_TYPE,
+ null: false,
+ method: :note,
+ description: "The content note itself"
+
+ field :created_at, Types::TimeType, null: false
+ field :updated_at, Types::TimeType, null: false
+ field :discussion, Types::Notes::DiscussionType, null: true, description: "The discussion this note is a part of"
+ field :resolvable, GraphQL::BOOLEAN_TYPE, null: false, method: :resolvable?
+ field :resolved_at, Types::TimeType, null: true, description: "The time the discussion was resolved"
+ field :position, Types::Notes::DiffPositionType, null: true, description: "The position of this note on a diff"
+ end
+ end
+end
diff --git a/app/graphql/types/notes/noteable_type.rb b/app/graphql/types/notes/noteable_type.rb
new file mode 100644
index 00000000000..9f126d67b0d
--- /dev/null
+++ b/app/graphql/types/notes/noteable_type.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Types
+ module Notes
+ module NoteableType
+ include Types::BaseInterface
+
+ field :notes, Types::Notes::NoteType.connection_type, null: false, description: "All notes on this noteable"
+ field :discussions, Types::Notes::DiscussionType.connection_type, null: false, description: "All discussions on this noteable"
+
+ definition_methods do
+ def resolve_type(object, context)
+ case object
+ when Issue
+ Types::IssueType
+ when MergeRequest
+ Types::MergeRequestType
+ else
+ raise "Unknown GraphQL type for #{object}"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/app/graphql/types/notes/position_type_enum.rb b/app/graphql/types/notes/position_type_enum.rb
new file mode 100644
index 00000000000..abdb2cfc804
--- /dev/null
+++ b/app/graphql/types/notes/position_type_enum.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module Types
+ module Notes
+ class PositionTypeEnum < BaseEnum
+ graphql_name 'DiffPositionType'
+ description 'Type of file the position refers to'
+
+ value 'text'
+ value 'image'
+ end
+ end
+end