Aws lambda 使用graphql和AWS Lambda保存数据

Aws lambda 使用graphql和AWS Lambda保存数据,aws-lambda,graphql,aws-amplify,Aws Lambda,Graphql,Aws Amplify,我从一个API获取数据,并希望使用AWS Lambda和graphql保存以放大后端。这个过程失败了,但我不确定为什么失败,或者我是否编写了正确的代码。这是我的第二个功能,我将感谢您的帮助。我的代码如下: /* Amplify Params - DO NOT EDIT API_MOCKBETS_GRAPHQLAPIENDPOINTOUTPUT API_MOCKBETS_GRAPHQLAPIIDOUTPUT API_MOCKBETS_GRAPHQLAPIKEYOUTPUT

我从一个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;

如何修改它以实现我的目标?

我已经通过删除和重构一些部分来使代码正常工作。最终代码如下:

/* 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
    }
  }
`;
const listCountries = gql`
  mutation listCountries($input: CreateCountryInput!) {
    listCountries(input: $input) {
      id
      country_id
      country_name
      country_logo
    }
  }
`;

const apiURL = "redacted";
const apiKey = "redacted";

app.get("/leagues", function (req, res) {
  axios
    .get(
      "redacted"
    )
    .then(async (response) => {
      for (const countryData of response.data) {
      
        await axios({
          url:apiURL,
          method: "post",
          headers: {
            "x-api-key": apiKey,
          },
          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!",
  });
});

app.listen(3000, function () {
  console.log("App started");
});

module.exports = app;