Skip to content

Add GraphQL query for linked work items

Eugenia Grieff requested to merge 416768-query-linked-work-items into master

Related to #416768 (closed)

What does this MR do and why?

Following !126854 (merged) which added a new model to store work item links, this MR adds a new work item widget to be able to query items linked to a work item.

The mutations to link and unlink the items will be added separately with !127380 (merged) and !127639 (merged).

This feature is being developed behind the FF linked_work_items.

Example query
query getRelated {
  workItem(id: "gid://gitlab/WorkItem/1896") {
    widgets {
      ...on WorkItemWidgetLinkedItems {
        linkedItems {
          edges {
            node {
              linkId
              linkType
              linkCreatedAt
              linkUpdatedAt
              workItem {
                title
              }
            }
          }
        }
      }
    }
  }
}

Database migration

A new migration is required to add the widget, for more context see docs and example MRs: !115527 (merged) & !116687 (merged).

This migration does not change structure.sql

Update: I initially used a regular migration but I had to update to use a post-migration because a new work item type was added during this milestone using a post-migration and we need to make sure that the new type is available for the migration in this MR. See !126980 (comment 1500138625) for more context.

DB migration output
❯ bin/rails db:migrate
main: == [advisory_lock_connection] object_id: 225120, pg_backend_pid: 76308
main: == 20230807083334 AddLinkedItemsWorkItemWidget: migrating =====================
main: == 20230807083334 AddLinkedItemsWorkItemWidget: migrated (0.0375s) ============

main: == [advisory_lock_connection] object_id: 225120, pg_backend_pid: 76308
ci: == [advisory_lock_connection] object_id: 225500, pg_backend_pid: 76311
ci: == 20230807083334 AddLinkedItemsWorkItemWidget: migrating =====================
ci: -- The migration is skipped since it modifies the schemas: [:gitlab_main].
ci: -- This database can only apply migrations in one of the following schemas: [:gitlab_ci, :gitlab_internal, :gitlab_shared].
ci: == 20230807083334 AddLinkedItemsWorkItemWidget: migrated (0.0122s) ============

ci: == [advisory_lock_connection] object_id: 225500, pg_backend_pid: 76311

❯ rake db:migrate:down:main VERSION=20230807083334
main: == [advisory_lock_connection] object_id: 224640, pg_backend_pid: 90144
main: == 20230807083334 AddLinkedItemsWorkItemWidget: reverting =====================
main: == 20230807083334 AddLinkedItemsWorkItemWidget: reverted (0.0188s) ============

main: == [advisory_lock_connection] object_id: 224640, pg_backend_pid: 90144

How to set up and validate locally

  1. Using rails console enable the feature flag
Feature.enable(:linked_work_items)
  1. Still in rails console create a work item of type task and take note of its ID
author, project = User.first, Project.first
wi_type = WorkItems::Type.find_by(base_type: 'task')
task = WorkItem.create!(title: "Source Task", project: project, author: author, work_item_type: wi_type)
  1. Create 3 more tasks
related_task = WorkItem.create!(title: "Related Task", project: project, author: author, work_item_type: wi_type)
blocked_task = WorkItem.create!(title: "Blocked Task", project: project, author: author, work_item_type: wi_type)
blocking_task = WorkItem.create!(title: "Blocking Task", project: project, author: author, work_item_type: wi_type)
  1. Link them to task using the 3 different link types
WorkItems::RelatedWorkItemLink.create!(source: task, target: blocked_task, link_type: 'blocks')
WorkItems::RelatedWorkItemLink.create!(source: blocking_task, target: task, link_type: 'blocks')
WorkItems::RelatedWorkItemLink.create!(source: task, target: related_task, link_type: 'relates_to')
  1. Visit https://gdk.test:3000/-/graphql-explorer and use the following query (replace task ID) to fetch the linked items
query
query getRelated {
  workItem(id: "gid://gitlab/WorkItem/NOTE_ID") {
    widgets {
      ...on WorkItemWidgetLinkedItems {
        linkedItems {
          edges {
            node {
              linkId
              linkType
              linkCreatedAt
              linkUpdatedAt
              workItem {
                title
              }
            }
          }
        }
      }
    }
  }
}
  1. Verify that the response includes the correct fields
response

example_query

  1. Disable the FF using Feature.disable(:linked_work_items) and verify that the response is empty for linkedItems

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eugenia Grieff

Merge request reports