summaryrefslogtreecommitdiff
path: root/app/graphql/types/design_management/design_type.rb
blob: 44e87905f92530cb9e2de653724b416bd4593ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Types
  module DesignManagement
    class DesignType < BaseObject
      graphql_name 'Design'
      description 'A single design'

      authorize :read_design

      alias_method :design, :object

      implements(Types::Notes::NoteableType)
      implements(Types::DesignManagement::DesignFields)
      implements(Types::CurrentUserTodos)

      field :versions,
            Types::DesignManagement::VersionType.connection_type,
            resolver: Resolvers::DesignManagement::VersionsResolver,
            description: "All versions related to this design ordered newest first.",
            extras: [:parent]

      # Returns a `DesignManagement::Version` for this query based on the
      # `atVersion` argument passed to a parent node if present, or otherwise
      # the most recent `Version` for the issue.
      def cached_stateful_version(parent_node)
        version_gid = Gitlab::Graphql::FindArgumentInParent.find(parent_node, :at_version)

        # Caching is scoped to an `issue_id` to allow us to cache the
        # most recent `Version` for an issue
        Gitlab::SafeRequestStore.fetch([request_cache_base_key, 'stateful_version', object.issue_id, version_gid]) do
          if version_gid
            GitlabSchema.object_from_id(version_gid, expected_type: ::DesignManagement::Version)&.sync
          else
            object.issue.design_versions.most_recent
          end
        end
      end

      def request_cache_base_key
        self.class.name
      end
    end
  end
end