Skip to content

Instantly share code, notes, and snippets.

@phawk
Created August 23, 2019 17:01
Show Gist options
  • Select an option

  • Save phawk/1a410ec6a2f34066538e0141aff49e9d to your computer and use it in GitHub Desktop.

Select an option

Save phawk/1a410ec6a2f34066538e0141aff49e9d to your computer and use it in GitHub Desktop.

Revisions

  1. phawk created this gist Aug 23, 2019.
    40 changes: 40 additions & 0 deletions graphql.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const https = require("https")
    const fetch = require("isomorphic-fetch")
    const Account = require("./models/account")
    const authenticated = require("./lib/auth")

    exports.handler = authenticated(async (event, context) => {
    const { id: shopId, shopifyToken } = context.account

    try {
    const resp = await fetch(`https://${shopId}/admin/api/2019-07/graphql.json`, {
    method: "POST",
    headers: {
    "X-Shopify-Access-Token": shopifyToken,
    "Accept": "application/json",
    "Content-Type": "application/json"
    },
    body: event.body,
    agent: new https.Agent({
    ca: require("ssl-root-cas/latest").create()
    })
    })

    const text = await resp.text()

    return {
    statusCode: resp.status,
    headers: { "Content-Type": "application/json" },
    body: text
    }
    } catch(e) {
    return {
    statusCode: 400,
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
    error: "Bad request",
    message: e.message
    })
    }
    }
    })