Skip to content

Commit

Permalink
Add some run_graphql_field tests for loading by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 9, 2024
1 parent 62a2df0 commit 4e07d3f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions spec/graphql/testing/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def authorized?(obj, args, ctx)
class Query < GraphQL::Schema::Object
field :students, [Student]

field :student, Student do
argument :student_id, ID, loads: Student
end

def student(student:)
student
end

field :lookahead_selections, String, extras: [:lookahead]

def lookahead_selections(lookahead:)
Expand All @@ -95,6 +103,18 @@ def self.unauthorized_object(err)
def self.unauthorized_field(err)
raise err
end

def self.object_from_id(id, ctx)
if id == "s1"
-> do { name: "Student1", type: Student } end
else
raise ArgumentError, "No data for id: #{id.inspect}"
end
end

def self.resolve_type(abs_t, obj, ctx)
obj.fetch(:type)
end
end

include GraphQL::Testing::Helpers
Expand All @@ -106,9 +126,19 @@ def self.unauthorized_field(err)
it "resolves fields" do
assert_equal "Blah", run_graphql_field(AssertionsSchema, "Student.name", { name: "Blah" })
assert_equal "Blah McBlah", run_graphql_field(AssertionsSchema, "Student.name", { name: "Blah" }, arguments: { "fullName" => true })
assert_equal "Blah McBlah", run_graphql_field(AssertionsSchema, "Student.name", { name: "Blah" }, arguments: { full_name: true })
assert_equal({ amount: 1_000_001 }, run_graphql_field(AssertionsSchema, "Student.latestBill", :student, context: admin_context))
end

it "loads arguments with lazy_resolve" do
student = run_graphql_field(AssertionsSchema, "Query.student", nil, arguments: { "studentId" => "s1" })
expected_student = { name: "Student1", type: AssertionsSchema::Student }
assert_equal(expected_student, student)

student2 = run_graphql_field(AssertionsSchema, "Query.student", nil, arguments: { student: "s1" })
assert_equal(expected_student, student2)
end

it "works with resolution context" do
with_resolution_context(AssertionsSchema, object: { name: "Foo" }, type: "Student", context: { admin_for: ["Foo"] }) do |rc|
rc.run_graphql_field("name")
Expand Down

0 comments on commit 4e07d3f

Please sign in to comment.