Commit 0c623080 authored by Mario de la Ossa's avatar Mario de la Ossa

Add GraphQL BoardResolver

Adds a BoardResolver to be able to mark the ID as a required argument
when querying for a single board.
parent 75fe1f08
# frozen_string_literal: true
module Resolvers
class BoardResolver < BaseResolver.single
alias_method :parent, :synchronized_object
type Types::BoardType, null: true
argument :id, GraphQL::ID_TYPE,
required: true,
description: 'The board\'s ID'
def resolve(id: nil)
return unless parent
::Boards::ListService.new(parent, context[:current_user], board_id: extract_board_id(id)).execute(create_default_board: false).first
rescue ActiveRecord::RecordNotFound
nil
end
private
def extract_board_id(gid)
GitlabSchema.parse_gid(gid, expected_type: ::Board).model_id
end
end
end
......@@ -64,7 +64,7 @@ module Types
Types::BoardType,
null: true,
description: 'A single board of the group',
resolver: Resolvers::BoardsResolver.single
resolver: Resolvers::BoardResolver
field :label,
Types::LabelType,
......
......@@ -234,7 +234,7 @@ module Types
Types::BoardType,
null: true,
description: 'A single board of the project',
resolver: Resolvers::BoardsResolver.single
resolver: Resolvers::BoardResolver
field :jira_imports,
Types::JiraImportType.connection_type,
......
---
title: 'GraphQL: No longer allows to omit ID when querying for a single board.'
merge_request: 43627
author:
type: fixed
......@@ -7120,9 +7120,9 @@ type Group {
"""
board(
"""
Find a board by its ID
The board's ID
"""
id: ID
id: ID!
): Board
"""
......@@ -12756,9 +12756,9 @@ type Project {
"""
board(
"""
Find a board by its ID
The board's ID
"""
id: ID
id: ID!
): Board
"""
......
......@@ -19758,11 +19758,15 @@
"args": [
{
"name": "id",
"description": "Find a board by its ID",
"description": "The board's ID",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
}
......@@ -37713,11 +37717,15 @@
"args": [
{
"name": "id",
"description": "Find a board by its ID",
"description": "The board's ID",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
}
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::BoardResolver do
include GraphqlHelpers
let_it_be(:user) { create(:user) }
let(:dummy_gid) { 'gid://gitlab/Board/1' }
shared_examples_for 'group and project boards resolver' do
it 'does not create a default board' do
expect(resolve_board(id: dummy_gid)).to eq nil
end
it 'calls Boards::ListService' do
expect_next_instance_of(Boards::ListService) do |service|
expect(service).to receive(:execute).and_return([])
end
resolve_board(id: dummy_gid)
end
it 'requires an ID' do
expect do
resolve(described_class, obj: board_parent, args: {}, ctx: { current_user: user })
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
context 'when querying for a single board' do
let(:board1) { create(:board, name: 'One', resource_parent: board_parent) }
it 'returns specified board' do
expect(resolve_board(id: global_id_of(board1))).to eq board1
end
it 'returns nil if board not found' do
outside_parent = create(board_parent.class.underscore.to_sym) # rubocop:disable Rails/SaveBang
outside_board = create(:board, name: 'outside board', resource_parent: outside_parent)
expect(resolve_board(id: global_id_of(outside_board))).to eq nil
end
end
end
describe '#resolve' do
context 'when there is no parent' do
let(:board_parent) { nil }
it 'returns nil if parent is nil' do
expect(resolve_board(id: dummy_gid)).to eq(nil)
end
end
context 'when project boards' do
let(:board_parent) { create(:project, :public, creator_id: user.id, namespace: user.namespace ) }
it_behaves_like 'group and project boards resolver'
end
context 'when group boards' do
let(:board_parent) { create(:group) }
it_behaves_like 'group and project boards resolver'
end
end
def resolve_board(id:)
resolve(described_class, obj: board_parent, args: { id: id }, ctx: { current_user: user })
end
end
......@@ -90,7 +90,7 @@ RSpec.shared_examples 'group and project boards query' do
it_behaves_like 'a working graphql query' do
before do
post_graphql(query_single_board, current_user: current_user)
post_graphql(query_single_board("id: \"gid://gitlab/Board/1\""), current_user: current_user)
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