我正在为我的PersonType对象实施GQL授权检查。在授权?下面的方法是将Person UUID与AWS Cognito用户UUID进行比较,如果它们与未通过GraphQL :: ExecutionError触发的授权传递相匹配。
module Types
class PersonType < Types::BaseObject
graphql_name "PersonType"
field :uuid, ID, null: false
field :first_name, String, null: false
def self.authorized?(person, context)
super && context[:cognito_user] == person.uuid
end
end
end
除Rspec测试外,所有功能均按预期工作。当我尝试测试PersonByUuid查询时,我返回了“ UNAUTHORISED”错误。我相信这是因为我没有在标头中传递令牌。因此,我尝试这样做,但是仍然出现相同的“未经授权”错误。
这就是我的测试方式:
RSpec.describe PersonByUuid, type: :request do
describe ".resolve" do
it "returns person by uuid" do
person = create(:person)
headers = { "Authorization": "Bearer <token>" }
post "/graphql", params: { query: query(person[:uuid]) }, headers: headers
json = JSON.parse(response.body)
data = json["data"]["personByUuid"]
expect(data).to match hash_including(
"uuid" => be_present,
"firstName" => "Luke",
"lastName" => "Skywalker",
)
end
end
def query(uuid)
<<~GQL
query {
personByUuid(
uuid: "#{uuid}"
) {
uuid
firstName
}
}
GQL
end
end
当我检查响应时,我可以看到在请求中设置了令牌。我希望这能正常工作并通过测试,但仍然会出现“未经授权”错误。也许有人可以建议我做错了什么?