在this documentation之后构建Lambda函数,以便为基于Amplify的应用程序做一些管理类型的事情。我已经为客户端设置了Cognito登录,并且一切正常。我添加了功能(脚本丢失),然后对其进行了修改:
> amplify update function
? Select which capability you want to update: Lambda function (serverless function)
? Select the Lambda function you want to update categoryAdder
? Do you want to update the Lambda function permissions to access other resources in this
project? Yes
? Select the category api
Api category has a resource called todoexample
? Select the operations you want to permit for todoexample create, read, update, delete
You can access the following resource attributes as environment variables from your Lambda
function
API_TODOEXAMPLE_GRAPHQLAPIENDPOINTOUTPUT
API_TODOEXAMPLE_GRAPHQLAPIIDOUTPUT
? Do you want to invoke this function on a recurring schedule? No
? Do you want to configure Lambda layers for this function? No
? Do you want to edit the local lambda function now? No
这是GraphQL模式:
type Todo @model
@auth(rules: [{ allow: owner, operations: [create, delete, update] }])
{
id: ID!
name: String!
description: String
category: Category
}
type Category @model
@auth (rules: [
{ allow: private, operations: [read] },
{ allow: private, provider: iam }
])
{
id: ID!
category: String!
}
type Mutation
{
addCategory(category: String!): Category @function(name: "categoryAdder-${env}")
}
这个想法是,只有Lambda函数才能运行addCategory
突变。
这是代码:
/* Amplify Params - DO NOT EDIT
API_TODOEXAMPLE_GRAPHQLAPIENDPOINTOUTPUT
API_TODOEXAMPLE_GRAPHQLAPIIDOUTPUT
ENV
REGION
Amplify Params - DO NOT EDIT */
const https = require('https')
const urlParse = require('url').URL
const appsyncUrl = process.env.API_TODOEXAMPLE_GRAPHQLAPIENDPOINTOUTPUT
const region = process.env.REGION
const endpoint = new urlParse(appsyncUrl).hostname.toString()
const AWS = require('aws-sdk')
require('es6-promise').polyfill()
require('isomorphic-fetch')
exports.handler = async (event) => {
const mutation = `
mutation addCategory($category: String!) {
addCategory(category: $category) {
id
category
}
}
`
AWS.config.update({
region,
credentials: new AWS.Credentials(
process.env.AWS_ACCESS_KEY_ID,
process.env.AWS_SECRET_ACCESS_KEY,
process.env.AWS_SESSION_TOKEN
),
})
const credentials = AWS.config.credentials
const req = new AWS.HttpRequest(appsyncUrl, region)
req.method = 'POST'
req.path = '/graphql'
req.headers.host = endpoint
req.headers['Content-Type'] = 'application/json'
req.body = JSON.stringify({
query: mutation,
operationName: 'addCategory',
variables: { category: 'Cats' },
})
const signer = new AWS.Signers.V4(req, 'appsync', true)
signer.addAuthorization(credentials, AWS.util.date.getDate())
console.log(`About to do promise...`)
const data = await new Promise((resolve, reject) => {
console.log(`About to do https.request...`)
const httpRequest = https.request({ ...req, host: endpoint }, (result) => {
console.log(`In https.request callback`)
result.on('data', (data) => {
console.log(`In result.on callback, data is`, data.toString())
resolve(JSON.parse(data.toString()))
})
})
console.log(`About to do httpRequest.write`)
httpRequest.write(req.body)
httpRequest.end()
})
console.log(`Returning data`, data)
return {
statusCode: 200,
body: data,
}
}
我得到的错误是“未授权在Mutation类型上访问addCategory”。
任何线索都值得赞赏!