【发布时间】:2021-07-29 21:27:31
【问题描述】:
我正在从 API 获取数据,并希望保存以使用 AWS Lambda 和 graphql 放大后端。该过程失败,但我不确定它为什么失败或我是否编写了正确的代码。这是我的第二个功能,我将不胜感激。我的代码如下:
/* Amplify Params - DO NOT EDIT
API_MOCKBETS_GRAPHQLAPIENDPOINTOUTPUT
API_MOCKBETS_GRAPHQLAPIIDOUTPUT
API_MOCKBETS_GRAPHQLAPIKEYOUTPUT
ENV
REGION
Amplify Params - DO NOT EDIT */
var express = require("express");
var bodyParser = require("body-parser");
var awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");
var app = express();
app.use(bodyParser.json());
app.use(awsServerlessExpressMiddleware.eventContext());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
next();
});
const axios = require("axios");
const gql = require("graphql-tag");
const graphql = require("graphql");
const { print } = graphql;
const createCountry = gql`
mutation createCountry($input: CreateCountryInput!) {
createCountry(input: $input) {
id
country_id
country_name
country_logo
}
}
`;
app.get("/leagues", function (req, res) {
axios
.get(
"https://apiv2.apifootball.com/?action=get_countries&APIkey=redacted"
)
.then(async (response) => {
for (const countryData of response.data) {
try {
const graphqlData = await axios({
url: process.env.API_URL,
method: "post",
headers: {
"x-api-key": process.env.API_mockbets_GRAPHQLAPIKEYOUTPUT,
},
data: {
query: print(createCountry),
variables: {
input: {
country_id: countryData.country_id,
country_name: countryData.country_name,
country_logo: countryData.country_logo,
},
},
},
});
res.json({
body: "successfully created country!",
});
} catch (err) {
res.json({
err,
});
}
}
})
.catch((err) => {
res.json({ err });
});
});
app.listen(3000, function () {
console.log("App started");
});
module.exports = app;
如何修改它以实现我的目标?
【问题讨论】:
标签: aws-lambda graphql aws-amplify