Skip to main content

Storefront API

Querying the Storefront API.

The base API object provided to purchase extension targets.

Anchor to query
query
<Data = unknown, Variables = Record<string, unknown>>(query: string, options?: { variables?: Variables; version?: ; }) => Promise<{ data?: Data; errors?: []; }>
required

The method used to query the Storefront GraphQL API with a prefetched token.

Refer to Storefront API access examples for more information.

Examples

Preact

import '@shopify/ui-extensions/preact';
import {render} from 'preact';
import {useEffect, useState} from 'preact/hooks';

export default function extension() {
render(<Extension />, document.body);
}

function Extension() {
const {query} = shopify;
const [data, setData] = useState();

useEffect(() => {
shopify
.query(
`query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
{
variables: {first: 5},
},
)
.then(({data, errors}) => setData(data))
.catch(console.error);
}, [query]);

return (
<s-unordered-list>
{data?.products?.nodes.map((node) => (
<s-list-item key={node.id}>
{node.title}
</s-list-item>
))}
</s-unordered-list>
);
}
Was this page helpful?