Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created June 5, 2020 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/3dbac04189a40640902f65a1d3ae337b to your computer and use it in GitHub Desktop.
Save james2doyle/3dbac04189a40640902f65a1d3ae337b to your computer and use it in GitHub Desktop.
Use the Shopify GraphQL API without having to install any libraries or additional frameworks. This example includes metafields
// You need to make your fields public first:
// https://shopify.dev/tutorials/retrieve-metafields-with-storefront-api#expose-metafields-to-the-storefront-api
await (await fetch("/api/2020-04/graphql.json", {
"method": "POST",
"credentials": "include",
"headers": {
// /admin/apps/private and create new app with storefront access
"x-shopify-storefront-access-token": "xxxxx",
"accept": "application/json",
"content-type": "application/json"
},
"body": JSON.stringify({
"query": `query($filter: String!, $namespace: String!) {
products(first: 10, query: $filter) {
edges {
node {
title
handle
metafields(first: 10, namespace: $namespace) {
edges {
node {
key
value
}
}
}
}
}
}
}`,
"variables": {
"filter": "id:000000000001",
"namespace": "metafield-namespace-to-include"
}
})
})).json();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment