Using the ctx · 60 / 69
Handle GraphQL response envelopes
YOUR TASK
Make this happen
Check the host flag and response type, reject nonempty errors, then return data. On the platform, operations still run with the caller’s authorization. A passing mock test does not prove that a real account has permission or that a business query matches the live schema.
Read the cases in
lesson.test.lua, then choose Check solution to see what needs to change.Update
subject.luauntil every test passes. Keep the supplied tests unchanged.
Read the starter and tests
subject.lua
exports.read = function(document: string)
local ok, result = ctx.graphql.query(document, {})
return result
end
lesson.test.lua
local t = require("test")
local subject = require("cococo.subject")
local document = "query AcademyContext { __typename }"
t.it("unwraps successful data and forwards the document", function()
t.mock
.host("ctx.graphql.query")
:returns(true, { data = { __typename = "Query" } })
local s = subject.load()
t.expect(s.read(document)):deep_eq({ __typename = "Query" })
t.expect(t.mock.spy("ctx.graphql.query")):called_with(document, {})
end)
t.it("handles host failure", function()
t.mock.host("ctx.graphql.query"):returns(false, "unavailable")
local s = subject.load()
t.expect(s.read(document)):is_nil()
end)
t.it("rejects a response containing GraphQL errors", function()
t.mock
.host("ctx.graphql.query")
:returns(true, { errors = { { message = "denied" } } })
local s = subject.load()
t.expect(s.read(document)):is_nil()
end)