【问题标题】:Save data using graphql and AWS Lambda使用 graphql 和 AWS Lambda 保存数据
【发布时间】: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


    【解决方案1】:

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

    /* 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-11
      • 2021-02-06
      • 1970-01-01
      • 2018-05-09
      • 2018-04-15
      • 2018-06-08
      • 2017-04-19
      • 2020-09-10
      相关资源
      最近更新 更多