Commit 265b5914 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '346939-mutation-store-user-sort-order' into 'master'

Add mutation for updating user preferences

See merge request gitlab-org/gitlab!79127
parents eee502a2 1706b68f
# frozen_string_literal: true
module Mutations
module UserPreferences
class Update < BaseMutation
graphql_name 'UserPreferencesUpdate'
argument :issues_sort, Types::IssueSortEnum,
required: false,
description: 'Sort order for issue lists.'
field :user_preferences,
Types::UserPreferencesType,
null: true,
description: 'User preferences after mutation.'
def resolve(**attributes)
user_preferences = current_user.user_preference
user_preferences.update(attributes)
{
user_preferences: user_preferences.valid? ? user_preferences : nil,
errors: errors_on_object(user_preferences)
}
end
end
end
end
...@@ -121,6 +121,7 @@ module Types ...@@ -121,6 +121,7 @@ module Types
mount_mutation Mutations::Namespace::PackageSettings::Update mount_mutation Mutations::Namespace::PackageSettings::Update
mount_mutation Mutations::Groups::Update mount_mutation Mutations::Groups::Update
mount_mutation Mutations::UserCallouts::Create mount_mutation Mutations::UserCallouts::Create
mount_mutation Mutations::UserPreferences::Update
mount_mutation Mutations::Packages::Destroy mount_mutation Mutations::Packages::Destroy
mount_mutation Mutations::Packages::DestroyFile mount_mutation Mutations::Packages::DestroyFile
mount_mutation Mutations::Echo mount_mutation Mutations::Echo
......
# frozen_string_literal: true
module Types
# rubocop: disable Graphql/AuthorizeTypes
# Only used to render the current user's own preferences
class UserPreferencesType < BaseObject
graphql_name 'UserPreferences'
field :issues_sort, Types::IssueSortEnum,
description: 'Sort order for issue lists.',
null: true
def issues_sort
object.issues_sort.to_sym
end
end
end
...@@ -4886,6 +4886,25 @@ Input type: `UserCalloutCreateInput` ...@@ -4886,6 +4886,25 @@ Input type: `UserCalloutCreateInput`
| <a id="mutationusercalloutcreateerrors"></a>`errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. | | <a id="mutationusercalloutcreateerrors"></a>`errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| <a id="mutationusercalloutcreateusercallout"></a>`userCallout` | [`UserCallout!`](#usercallout) | User callout dismissed. | | <a id="mutationusercalloutcreateusercallout"></a>`userCallout` | [`UserCallout!`](#usercallout) | User callout dismissed. |
### `Mutation.userPreferencesUpdate`
Input type: `UserPreferencesUpdateInput`
#### Arguments
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="mutationuserpreferencesupdateclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationuserpreferencesupdateissuessort"></a>`issuesSort` | [`IssueSort`](#issuesort) | Sort order for issue lists. |
#### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="mutationuserpreferencesupdateclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationuserpreferencesupdateerrors"></a>`errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| <a id="mutationuserpreferencesupdateuserpreferences"></a>`userPreferences` | [`UserPreferences`](#userpreferences) | User preferences after mutation. |
### `Mutation.vulnerabilityConfirm` ### `Mutation.vulnerabilityConfirm`
Input type: `VulnerabilityConfirmInput` Input type: `VulnerabilityConfirmInput`
...@@ -15740,6 +15759,14 @@ fields relate to interactions between the two entities. ...@@ -15740,6 +15759,14 @@ fields relate to interactions between the two entities.
| ---- | ---- | ----------- | | ---- | ---- | ----------- |
| <a id="userpermissionscreatesnippet"></a>`createSnippet` | [`Boolean!`](#boolean) | Indicates the user can perform `create_snippet` on this resource. | | <a id="userpermissionscreatesnippet"></a>`createSnippet` | [`Boolean!`](#boolean) | Indicates the user can perform `create_snippet` on this resource. |
### `UserPreferences`
#### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="userpreferencesissuessort"></a>`issuesSort` | [`IssueSort`](#issuesort) | Sort order for issue lists. |
### `UserStatus` ### `UserStatus`
#### Fields #### Fields
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::UserPreferencesType do
specify { expect(described_class.graphql_name).to eq('UserPreferences') }
it 'exposes the expected fields' do
expected_fields = %i[
issues_sort
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Mutations::UserPreferences::Update do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let(:sort_value) { 'TITLE_ASC' }
let(:input) do
{
'issuesSort' => sort_value
}
end
let(:mutation) { graphql_mutation(:userPreferencesUpdate, input) }
let(:mutation_response) { graphql_mutation_response(:userPreferencesUpdate) }
context 'when user has no existing preference' do
it 'creates the user preference record' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response['userPreferences']['issuesSort']).to eq(sort_value)
expect(current_user.user_preference.persisted?).to eq(true)
expect(current_user.user_preference.issues_sort).to eq(Types::IssueSortEnum.values[sort_value].value.to_s)
end
end
context 'when user has existing preference' do
before do
current_user.create_user_preference!(issues_sort: Types::IssueSortEnum.values['TITLE_DESC'].value)
end
it 'updates the existing value' do
post_graphql_mutation(mutation, current_user: current_user)
current_user.user_preference.reload
expect(response).to have_gitlab_http_status(:success)
expect(mutation_response['userPreferences']['issuesSort']).to eq(sort_value)
expect(current_user.user_preference.issues_sort).to eq(Types::IssueSortEnum.values[sort_value].value.to_s)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment