diff --git a/app/graphql/types/account_type.rb b/app/graphql/types/account_type.rb index d89ba497c29..377d31297b5 100644 --- a/app/graphql/types/account_type.rb +++ b/app/graphql/types/account_type.rb @@ -31,5 +31,16 @@ module Types # batch load it in a reasonable way. object.resolved_outcome_proficiency&.outcome_proficiency_ratings end + + field :courses_connection, CourseType.connection_type, null: true + def courses_connection + return unless object.grants_right?(current_user, :read_course_list) + object.associated_courses + end + + field :sub_accounts_connection, AccountType.connection_type, null: true + def sub_accounts_connection + object.sub_accounts.order(:id) + end end end diff --git a/spec/graphql/types/account_type_spec.rb b/spec/graphql/types/account_type_spec.rb index 8b56654b573..bde523de9f0 100644 --- a/spec/graphql/types/account_type_spec.rb +++ b/spec/graphql/types/account_type_spec.rb @@ -23,6 +23,8 @@ describe Types::AccountType do before(:once) do teacher_in_course(active_all: true) student_in_course(active_all: false) + account_admin_user + @sub_account = account_model parent_account: @course.root_account end let(:account) { @course.root_account } @@ -43,4 +45,16 @@ describe Types::AccountType do account_type.resolve('proficiencyRatingsConnection { nodes { _id } }').sort ).to eq OutcomeProficiencyRating.all.map { |r| r.id.to_s }.sort end + + it 'works for courses' do + expect(account_type.resolve('coursesConnection { nodes { _id } }', current_user: @admin)).to eq [@course.id.to_s] + end + + it 'requires read_course_list permission' do + expect(account_type.resolve('coursesConnection { nodes { _id } }', current_user: @teacher)).to be_nil + end + + it 'works for subaccounts' do + expect(account_type.resolve('subAccountsConnection { nodes { _id } }')).to eq [@sub_account.id.to_s] + end end