Set author as nullable in snippet GraphQL Type

parent a663b754
......@@ -27,9 +27,12 @@ module Types
authorize: :read_project,
resolve: -> (snippet, args, context) { Gitlab::Graphql::Loaders::BatchModelLoader.new(Project, snippet.project_id).find }
# Author can be nil in some scenarios. For example,
# when the admin setting restricted visibility
# level is set to public
field :author, Types::UserType,
description: 'The owner of the snippet',
null: false,
null: true,
resolve: -> (snippet, args, context) { Gitlab::Graphql::Loaders::BatchModelLoader.new(User, snippet.author_id).find }
field :file_name, GraphQL::STRING_TYPE,
......
---
title: Set author as nullable in snippet GraphQL Type
merge_request: 34135
author:
type: fixed
......@@ -11155,7 +11155,7 @@ type Snippet implements Noteable {
"""
The owner of the snippet
"""
author: User!
author: User
"""
Snippet blob
......
......@@ -32945,13 +32945,9 @@
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "User",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
......@@ -1630,7 +1630,7 @@ Represents a snippet entry
| Name | Type | Description |
| --- | ---- | ---------- |
| `author` | User! | The owner of the snippet |
| `author` | User | The owner of the snippet |
| `blob` | SnippetBlob! | Snippet blob |
| `createdAt` | Time! | Timestamp this snippet was created |
| `description` | String | Description of the snippet |
......
......@@ -16,6 +16,44 @@ describe GitlabSchema.types['Snippet'] do
expect(described_class).to have_graphql_fields(*expected_fields)
end
context 'when restricted visibility level is set to public' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
let(:current_user) { user }
let(:query) do
%(
{
snippets {
nodes {
author {
id
}
}
}
}
)
end
let(:response) { subject.dig('data', 'snippets', 'nodes')[0] }
subject { GitlabSchema.execute(query, context: { current_user: current_user }).as_json }
before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
end
it 'returns snippet author' do
expect(response['author']).to be_present
end
context 'when user is not logged in' do
let(:current_user) { nil }
it 'returns snippet author as nil' do
expect(response['author']).to be_nil
end
end
end
describe 'authorizations' do
specify { expect(described_class).to require_graphql_authorizations(:read_snippet) }
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