【发布时间】:2021-12-30 15:58:23
【问题描述】:
我有一个配置了 Cognito 和 API 密钥以进行身份验证的 Amplify Web 应用程序。
我想让未经身份验证的用户调用 Lambda 解析器,它会返回 JSON 响应。我可以让用户调用解析器,然后执行。但我在前端应用程序的响应中收到以下错误:
errorType: "Unauthorized"
message: "Not Authorized to access score on type FormAssessmentResponse"
...
errorType: "Unauthorized"
message: "Not Authorized to access isInCoverageArea on type FormAssessmentResponse"
我的 GraphQL 架构如下:
type FormAssessmentResponse {
score: Int
isInCoverageArea: Boolean
}
...
type Mutation {
formAssessment(input: FormAssessmentInput!): FormAssessmentResponse @function(name: "formResolvers-${env}") @auth(rules: [{allow: public, provider: apiKey}])
}
我的 Lambda 解析器(简化)如下:
const formAssessment = async (event, context, callback) => {
console.log(event.arguments.input)
callback(null, {
score: 54,
isInCoverageArea: true
})
}
const resolvers = {
Mutation: {
formAssessment,
},
}
exports.handler = async (event, context, callback) => {
const typeHandler = resolvers[event.typeName]
if (typeHandler) {
const resolver = typeHandler[event.fieldName]
if (resolver) {
return await resolver(event, context, callback)
}
}
throw new Error("Resolver not found.")
}
在我的前端,我按如下方式调用解析器:
import { API } from '@aws-amplify/api'
import Auth from '@aws-amplify/auth'
import config from '../src/aws-exports'
Amplify.configure(config)
const response = await API.graphql({
query: mutation,
variables: variables,
authMode: 'API_KEY'
})
如果我用简单的 String 或 AWSJSON 替换 FormAssessmentResponse,我可以从我的 Lambda 函数中返回这些类型。因此,这似乎与使用 FormAssessmentResponse 类型有关。
注意,我不想 @model 类型 FormAssessmentResponse,因为这会创建 DynamoDB 表,我不希望这样做。
【问题讨论】:
标签: amazon-web-services aws-lambda graphql amplify