Created
August 23, 2019 17:01
-
-
Save phawk/1a410ec6a2f34066538e0141aff49e9d to your computer and use it in GitHub Desktop.
Lambda Shopify graphql proxy endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment