graphql: add account type

Test plan: make graphql queries for an account

closes GQL-85
flag = none

Change-Id: I12817cf9484ef96095a020d28797691dfa08a7c7
Reviewed-on: https://gerrit.instructure.com/208370
Tested-by: Jenkins
Reviewed-by: Landon Gilbert-Bland <lbland@instructure.com>
QA-Review: Cameron Matheson <cameron@instructure.com>
Product-Review: Cameron Matheson <cameron@instructure.com>
This commit is contained in:
Cameron Matheson 2019-08-30 15:22:52 -06:00
parent 829105c1e6
commit a648078d19
8 changed files with 96 additions and 0 deletions

View File

@ -43,6 +43,7 @@ class CanvasSchema < GraphQL::Schema
def self.resolve_type(type, obj, _ctx)
case obj
when Account then Types::AccountType
when Course then Types::CourseType
when Assignment then Types::AssignmentType
when AssignmentGroup then Types::AssignmentGroupType

View File

@ -21,6 +21,8 @@ module GraphQLNodeLoader
check_read_permission = make_permission_check(ctx, :read)
case type
when "Account"
Loaders::IDLoader.for(Account).load(id).then(check_read_permission)
when "Course"
Loaders::IDLoader.for(Course).load(id).then(check_read_permission)
when "Assignment"

View File

@ -0,0 +1,28 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Types
class AccountType < ApplicationObjectType
implements GraphQL::Types::Relay::Node
global_id_field :id
field :_id, ID, method: :id, null: false
field :name, String, null: true
end
end

View File

@ -19,6 +19,7 @@
class Types::LegacyNodeType < Types::BaseEnum
graphql_name "NodeType"
value "Account"
value "Assignment"
value "AssignmentGroup"
value "Course"

View File

@ -31,6 +31,14 @@ module Types
GraphQLNodeLoader.load(type, _id, context)
end
field :account, Types::AccountType, null: true do
argument :id, ID, "a graphql or legacy id", required: true,
prepare: GraphQLHelpers.relay_or_legacy_id_prepare_func("Account")
end
def account
GraphQLNodeLoader.load("Account", id, context)
end
field :course, Types::CourseType, null: true do
argument :id, ID, "a graphql or legacy id", required: true,
prepare: GraphQLHelpers.relay_or_legacy_id_prepare_func("Course")

View File

@ -5,6 +5,9 @@
"kind": "INTERFACE",
"name": "Node",
"possibleTypes": [
{
"name": "Account"
},
{
"name": "Assignment"
},

View File

@ -1,3 +1,9 @@
type Account implements Node {
_id: ID!
id: ID!
name: String
}
"""
A list of students that an `AssignmentOverride` applies to
"""
@ -1928,6 +1934,7 @@ interface Node {
}
enum NodeType {
Account
Assignment
AssignmentGroup
Course
@ -2188,6 +2195,13 @@ enum ProgressState {
}
type Query {
account(
"""
a graphql or legacy id
"""
id: ID!
): Account
"""
All courses viewable by the current user
"""

View File

@ -0,0 +1,39 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require_relative '../../spec_helper'
require_relative "../graphql_spec_helper"
describe Types::AccountType do
before(:once) do
teacher_in_course(active_all: true)
student_in_course(active_all: false)
end
let(:account) { @course.root_account }
let(:account_type) { GraphQLTypeTester.new(account, current_user: @teacher) }
it "works" do
expect(account_type.resolve(:name)).to eq account.name
expect(account_type.resolve(:_id)).to eq account.id.to_s
end
it "requires read permission" do
expect(account_type.resolve(:name, current_user: @student)).to be_nil
end
end