"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.graphql = void 0;

var _axios = _interopRequireDefault(require("axios"));

var _shopify = require("../helpers/shopify");

var _utils = require("../helpers/utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const graphql = async (req, res) => (0, _utils.handler)(_utils.onlyPOST, _utils.hasShopHeaders, _utils.hasBody, async data => {
  const {
    accessToken,
    graphqlEndpoint
  } = (0, _shopify.getShopifyHeaders)(data);

  try {
    const response = await (0, _axios.default)({
      method: "POST",
      url: graphqlEndpoint,
      data: data.body,
      headers: (0, _shopify.formatShopifyHeaders)(accessToken)
    }).then(result => result ? res.status(200).json(result.data) : res.status(200).json({
      status: "error",
      body: "No data returned"
    }));
    return response;
  } catch (err) {
    console.log(err.response && err.response.data.errors || err.message);
    return res.status(200).json({
      status: "error",
      body: err.response && err.response.data.errors
    });
  }
})(req, res);

exports.graphql = graphql;
//# sourceMappingURL=graphql.js.map