Skip to content
Snippets Groups Projects
Commit 94daf8cf authored by Marius Bobin's avatar Marius Bobin :two: Committed by Luke Duncalfe
Browse files

Update CiJobConnection.count to limited count

#360672

Changelog: changed
parent 8e4251ff
No related branches found
No related tags found
1 merge request!89642Add limited count connection type to GraphQL
......@@ -7,7 +7,7 @@ module Ci
class JobType < BaseObject
graphql_name 'CiJob'
connection_type_class(Types::CountableConnectionType)
connection_type_class(Types::LimitedCountableConnectionType)
expose_permissions Types::PermissionTypes::Ci::Job
......
# frozen_string_literal: true
module Types
# rubocop: disable Graphql/AuthorizeTypes
class LimitedCountableConnectionType < GraphQL::Types::Relay::BaseConnection
COUNT_LIMIT = 1000
COUNT_DESCRIPTION = "Limited count of collection. Returns limit + 1 for counts greater than the limit."
field :count, GraphQL::Types::Int, null: false, description: COUNT_DESCRIPTION do
argument :limit, GraphQL::Types::Int,
required: false, default_value: COUNT_LIMIT,
validates: { numericality: { greater_than: 0, less_than_or_equal_to: COUNT_LIMIT } },
description: "Limit value to be applied to the count query. Default is 1000."
end
def count(limit:)
relation = object.items
if relation.respond_to?(:page)
relation.page.total_count_with_limit(:all, limit: limit)
else
[relation.size, limit.next].min
end
end
end
end
......@@ -6033,11 +6033,24 @@ The connection type for [`CiJob`](#cijob).
 
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="cijobconnectioncount"></a>`count` | [`Int!`](#int) | Total count of collection. |
| <a id="cijobconnectionedges"></a>`edges` | [`[CiJobEdge]`](#cijobedge) | A list of edges. |
| <a id="cijobconnectionnodes"></a>`nodes` | [`[CiJob]`](#cijob) | A list of nodes. |
| <a id="cijobconnectionpageinfo"></a>`pageInfo` | [`PageInfo!`](#pageinfo) | Information to aid in pagination. |
 
##### Fields with arguments
###### `CiJobConnection.count`
Limited count of collection. Returns limit + 1 for counts greater than the limit.
Returns [`Int!`](#int).
####### Arguments
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="cijobconnectioncountlimit"></a>`limit` | [`Int`](#int) | Limit value to be applied to the count query. Default is 1000. |
#### `CiJobEdge`
 
The edge type for [`CiJob`](#cijob).
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::LimitedCountableConnectionType do
it 'has the expected fields' do
expected_fields = %i[count page_info]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
......@@ -258,4 +258,81 @@ def all(*fields)
end
end
end
describe '.jobs.count' do
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
let_it_be(:successful_job) { create(:ci_build, :success, pipeline: pipeline) }
let_it_be(:pending_job) { create(:ci_build, :pending, pipeline: pipeline) }
let_it_be(:failed_job) { create(:ci_build, :failed, pipeline: pipeline) }
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
jobs {
count
}
}
}
}
)
end
before do
post_graphql(query, current_user: user)
end
it 'returns the number of jobs' do
expect(graphql_data_at(:project, :pipeline, :jobs, :count)).to eq(3)
end
context 'with limit value' do
let(:limit) { 1 }
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
jobs {
count(limit: #{limit})
}
}
}
}
)
end
it 'returns a limited number of jobs' do
expect(graphql_data_at(:project, :pipeline, :jobs, :count)).to eq(2)
end
context 'with invalid value' do
let(:limit) { 1500 }
it 'returns a validation error' do
expect(graphql_errors).to include(a_hash_including('message' => 'limit must be less than or equal to 1000'))
end
end
end
context 'with jobs filter' do
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
jobs(statuses: FAILED) {
count
}
}
}
)
end
it 'returns the number of failed jobs' do
expect(graphql_data_at(:project, :jobs, :count)).to eq(1)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment