Created
June 5, 2020 17:16
-
-
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
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
// 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