无服务器框架+ AWS + Lambda + DynamoDB + GraphQL + Apollo Server =无法使POST请求工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无服务器框架+ AWS + Lambda + DynamoDB + GraphQL + Apollo Server =无法使POST请求工作相关的知识,希望对你有一定的参考价值。
好的,我几天就遇到了这个问题。
我尝试学习本教程:https://serverless.com/blog/make-serverless-graphql-api-using-lambda-dynamodb/
并使其与apollo-server-lambda一起使用。本教程帮助:
https://medium.com/vessels/apollo-server-serverless-graphql-bliss-68e8e15195ac
问题是,当您尝试将apollo server lambda与DynamoDB实际连接时,没有任何工作(对我而言)。我得到了一个我不记得的错误列表,这只是令人沮丧。
这是我的代码:
# serverless.yml
service: graphql-api
provider:
name: aws
runtime: nodejs6.10
region: eu-west-3
stage: dev
environment:
DYNAMODB_TABLE: ${self:service}-${self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:UpdateItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
resources:
Resources:
NicknamesTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: firstName
AttributeType: S
KeySchema:
- AttributeName: firstName
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE}
functions:
graphql:
handler: handler.graphql
events:
- http:
path: graphql
method: post
cors: true
- http:
path: graphql
method: get
cors: true
我的经纪人:
# handler.js
const AWS = require('aws-sdk');
const server = require("apollo-server-lambda");
const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const promisify = foo => new Promise((resolve, reject) => {
foo((error, result) => {
if (error) {
reject(error)
} else {
resolve(result)
}
})
})
const getGreeting = firstName => promisify(callback =>
dynamoDb.get({
TableName: process.env.DYNAMODB_TABLE,
Key: { firstName },
}, callback))
.then(result => {
if (!result.Item) {
return firstName
}
return result.Item.nickname
})
.then(name => `Hello, ${name}.`)
// add method for updates
const changeNickname = (firstName, nickname) => promisify(callback =>
dynamoDb.update({
TableName: process.env.DYNAMODB_TABLE,
Key: { firstName },
UpdateExpression: 'SET nickname = :nickname',
ExpressionAttributeValues: {
':nickname': nickname
}
}, callback))
.then(() => nickname)
const typeDefs = `
type Query {
greeting(firstName: String!): String
}
type Mutation {
changeNickname(
firstName: String!
nickname: String!
): String
}
`;
const resolvers = {
Query: {
greeting: (_, { firstName }) => getGreeting(firstName),
},
Mutation: {
changeNickname: (_, { firstName, nickname }) => changeNickname(firstName, nickname),
}
};
exports.graphql = function (event, context, callback) {
const callbackFilter = function (error, output) {
output.headers = output.header || {};
output.headers['Access-Control-Allow-Origin'] = '*';
callback(error, output);
};
const handler = server.graphqlLambda({ schema: makeExecutableSchema({ typeDefs, resolvers }) });
return handler(event, context, callbackFilter);
};
我尝试使用Apollo 1和2,没什么用。我回到版本1,因为有更多关于它的论坛帖子。大多数情况下,我有“内部服务器错误”。我尝试了在apollo docs上找到的不同版本的服务器,但我的所有请求都失败了,在终端上使用curl或直接在AWS上的API网关上测试该功能。我按照以下文档编写了请求正文:https://www.apollographql.com/docs/apollo-server/requests.html
这是我的cloudwatch日志:
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SyntaxError: Unexpected token = in JSON at position 8
任何帮助将非常感谢!!
好的,我发现了问题。当您使用put函数时,您无法获得刚刚在dynamoDB中插入的新项目。你必须在你的params对象(put函数的第一个参数)中放入“ReturnValues:'ALL_OLD'”,就像没有抛出错误一样,你应该返回你想要的值,因为你刚刚在db中输入了它们。
更多细节在这里:
https://github.com/aws/aws-sdk-js/issues/803
以上是关于无服务器框架+ AWS + Lambda + DynamoDB + GraphQL + Apollo Server =无法使POST请求工作的主要内容,如果未能解决你的问题,请参考以下文章
AWS Lambda 上的 Nestjs(无服务器框架)|如何访问事件参数?
无服务器框架 + AWS + Lambda + DynamoDB + GraphQL + Apollo Server = 无法使 POST 请求工作
使用 graphql、aws lambda 和无服务器框架的多个 url 路径选项错误
如何从 AWS Lambda 函数 + 无服务器框架的 URL 中删除阶段?
无服务器框架+ AWS + Lambda + DynamoDB + GraphQL + Apollo Server =无法使POST请求工作