Skip to content

Commit

Permalink
graphql: add some top-level fields
Browse files Browse the repository at this point in the history
These objects were originally accessible via the node/legacyNode, but
these first-class fields will be a bit more convenient to use.

Test plan:
  * retrieve objects from the top-level
    course/assignment/assignmentGroup fields

Change-Id: I6f64a0841a36dabd753e8e91aa4145689d56fc1b
Reviewed-on: https://gerrit.instructure.com/167469
Reviewed-by: Carl Kibler <ckibler@instructure.com>
Tested-by: Jenkins
QA-Review: Cameron Matheson <cameron@instructure.com>
Product-Review: Cameron Matheson <cameron@instructure.com>
  • Loading branch information
cmatheson committed Oct 12, 2018
1 parent f3ae091 commit 6b0114e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
24 changes: 24 additions & 0 deletions app/graphql/types/query_type.rb
Expand Up @@ -31,6 +31,30 @@ def legacy_node(type:, _id:)
GraphQLNodeLoader.load(type, _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")
end
def course(id:)
GraphQLNodeLoader.load("Course", id, context)
end

field :assignment, Types::AssignmentType, null: true do
argument :id, ID, "a graphql or legacy id", required: true,
prepare: GraphQLHelpers.relay_or_legacy_id_prepare_func("Course")
end
def assignment(id:)
GraphQLNodeLoader.load("Assignment", id, context)
end

field :assignment_group, Types::AssignmentGroupType, null: true do
argument :id, ID, "a graphql or legacy id", required: true,
prepare: GraphQLHelpers.relay_or_legacy_id_prepare_func("Course")
end
def assignment_group(id:)
GraphQLNodeLoader.load("AssignmentGroup", id, context)
end

field :all_courses, [CourseType],
"All courses viewable by the current user",
null: true
Expand Down
12 changes: 12 additions & 0 deletions schema.graphql
Expand Up @@ -747,6 +747,18 @@ type PageViewAnalysis {
type Query {
# All courses viewable by the current user
allCourses: [Course!]
assignment(
# a graphql or legacy id
id: ID!
): Assignment
assignmentGroup(
# a graphql or legacy id
id: ID!
): AssignmentGroup
course(
# a graphql or legacy id
id: ID!
): Course

# Fetches an object given its type and legacy ID
legacyNode(_id: ID!, type: NodeType!): Node
Expand Down
13 changes: 13 additions & 0 deletions spec/graphql/types/assignment_group_type_spec.rb
Expand Up @@ -37,6 +37,19 @@
@group_type = GraphQLTypeTester.new(@group, current_user: @student)
end

context "top-level permissions" do
it "needs read permission" do
some_person = user_factory(active_all: true)
expect(@group_type.resolve("_id", current_user: some_person)).to be_nil

expect(
CanvasSchema.execute(<<~GQL, context: {current_user: some_person}).dig("data", "ag")
query { ag: assignmentGroup(id: "#{@group.id}") { id } }
GQL
).to be_nil
end
end

it "returns information about the group" do
expect(@group_type.resolve("_id")).to eq @group.id.to_s
expect(@group_type.resolve("name")).to eq @group.name
Expand Down
17 changes: 14 additions & 3 deletions spec/graphql/types/assignment_type_spec.rb
Expand Up @@ -43,9 +43,20 @@
expect(assignment_type.resolve("muted")).to eq assignment.muted?
end

it "requires read permission" do
assignment.unpublish
expect(assignment_type.resolve("_id")).to be_nil
context "top-level permissions" do
it "requires read permission" do
assignment.unpublish

# node / legacy node
expect(assignment_type.resolve("_id")).to be_nil

# assignment
expect(
CanvasSchema.execute(<<~GQL, context: {current_user: student}).dig("data", "assignment")
query { assignment(id: "#{assignment.id.to_s}") { id } }
GQL
).to be_nil
end
end

it "returns needsGradingCount" do
Expand Down
18 changes: 14 additions & 4 deletions spec/graphql/types/course_type_spec.rb
Expand Up @@ -33,11 +33,21 @@
expect(course_type.resolve("name")).to eq course.name
end

it "needs read permission" do
course_with_student
@course2, @student2 = @course, @student
context "top-level permissions" do
it "needs read permission" do
course_with_student
@course2, @student2 = @course, @student

expect(course_type.resolve("_id", current_user: @student2)).to be_nil
# node / legacy node
expect(course_type.resolve("_id", current_user: @student2)).to be_nil

# course
expect(
CanvasSchema.execute(<<~GQL, context: {current_user: @student2}).dig("data", "course")
query { course(id: "#{course.id.to_s}") { id } }
GQL
).to be_nil
end
end

describe "assignmentsConnection" do
Expand Down

0 comments on commit 6b0114e

Please sign in to comment.