带有graphql和sqs的lambda在nodejs中向sqs发送2条消息?
Posted
技术标签:
【中文标题】带有graphql和sqs的lambda在nodejs中向sqs发送2条消息?【英文标题】:lambda with graphql and sqs send 2 messages to sqs in nodejs? 【发布时间】:2019-05-25 13:20:56 【问题描述】:我正在做一个项目,我需要编写一个 lambda 函数,该函数提供 AWS API 来处理 GraphQL 查询并将有效负载发送到 AWS SQS 一切正常,但是当我检查我的 AWS SQS 队列时,它每条都显示 2 条消息time 而不是 1 和 cloud watch 也只显示一次函数触发。下面我将与您分享我的代码,任何帮助将不胜感激。
index.js
const graphql = require("graphql");
const schema = require("./graphql/schema");
exports.handler = async (event) =>
// getting query from lambda event
const query = event.query;
// getting query variables from lambda event
const variables = event.variables;
return await graphql(schema, query, null, null, variables);
;
sqs.js
const AWS = require("aws-sdk");
AWS.config.update( region: "us-east-1");
// Create an SQS service object
const sqs = new AWS.SQS(apiVersion: '2012-11-05', "accessKeyId": process.env.ACCESS_KEY_ID, "secretAccessKey": process.env.SECRET_ACCESS_KEY);
const QueueUrl = process.env.SQS_QUEUE_URL;
const sendPayloadToSQS = message =>
const params =
MessageBody: JSON.stringify(message),
QueueUrl
;
await sqs.sendMessage(params, function(err, data)
if (err)
console.log("Message sending failed : ", err);
else
console.log("Message queued to SQS successfully : ", data.MessageId);
).promise();
;
module.exports = sendPayloadToSQS;
graphql 变异文件
const GraphQLNonNull = require("graphql");
const mutationWithClientMutationId = require("../../common");
const JobRequestEventResponse = require("../jobRequestEventResponse");
const JobRequestInput, JobEventMetadataInput = require("../jobSchema");
const sendPayloadToSQS = require("../../../sqs");
const newId = require("../../../newId");
const JobRequestEvent = mutationWithClientMutationId(
name: "JobRequestEvent",
inputFields:
eventMetadataInput:
type: new GraphQLNonNull(JobEventMetadataInput),
,
eventInput:
type: new GraphQLNonNull(JobRequestInput),
,
,
outputFields:
JobRequestEventResponse:
type: JobRequestEventResponse,
,
,
mutateAndGetPayload: async (params) =>
const new_id = newId();
if(params.eventInput.jobId === null || params.eventInput.jobId === undefined)
params.eventInput.jobId = new_id;
const payload =
_id: new_id,
transactionId: new_id,
name: params.eventMetadataInput.name,
userRole: params.eventMetadataInput.userRole,
date: params.eventMetadataInput.date,
languageCode: params.eventMetadataInput.languageCode,
eventInput: params.eventInput,
;
//send payload to sqs
await sendPayloadToSQS(payload);
return
JobRequestEventResponse:
id: payload._id,
transactionId: payload.transactionId,
status: "Success",
,
;
,
);
module.exports =
JobRequestEvent,
;
【问题讨论】:
【参考方案1】:我再次阅读了文档,发现回调是我的问题的根本原因:如果我向sendMessage
提供回调,它会触发我的函数,然后当我再次编写 promise() 时它会触发我的函数,所以我删除了回调仅如下所示。
参考:AWS Official documentation
而不是这个:
await sqs.sendMessage(params, function(err, data)
if (err)
console.log("Message sending failed : ", err);
else
console.log("Message queued to SQS successfully : ", data.MessageId);
).promise();
我写这个:
const request = sqs.sendMessage(params);
const result = await request.promise();
if(result)
console.log("Message queued to SQS successfully : ", result.MessageId);
else
console.log("Message queued failed");
【讨论】:
请在此处添加解决方案,而不是写感谢信。以上是关于带有graphql和sqs的lambda在nodejs中向sqs发送2条消息?的主要内容,如果未能解决你的问题,请参考以下文章