Querying the Storefront API.
import {useEffect, useState} from 'react';
import {
useApi,
reactExtension,
List,
ListItem,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const [data, setData] = useState();
const {query} = useApi();
useEffect(() => {
query(
`query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
{
variables: {first: 5},
},
)
.then(({data, errors}) => setData(data))
.catch(console.error);
}, [query]);
return (
<List>
{data?.products?.nodes.map((node) => (
<ListItem key={node.id}>
{node.title}
</ListItem>
))}
</List>
);
}
import {
extension,
List,
ListItem,
} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root, {query}) => {
query<any>(
`query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
{
variables: {first: 5},
},
)
.then(({data}) => {
const listItems =
data?.products?.nodes.map((node) =>
root.createComponent(
ListItem,
undefined,
node.title,
),
);
root.appendChild(
root.createComponent(
List,
undefined,
listItems,
),
);
})
.catch(console.error);
},
);
The base API object provided to this and other `customer-account` extension targets.
Used to query the Storefront GraphQL API with a prefetched token. See [storefront api access examples](/docs/api/customer-account-ui-extensions/apis/storefront-api#examples) for more information.
Union of supported storefront API versions
'2022-04' | '2022-07' | '2022-10' | '2023-01' | '2023-04' | '2023-07' | '2024-01' | '2024-04' | '2024-07' | '2024-10' | 'unstable'
GraphQL error returned by the Shopify Storefront APIs.
Querying the Storefront API.
You can access the [Storefront GraphQL API](/docs/api/storefront) using global `fetch()`. Ensure your extension can access the Storefront API via the [`api_access` capability](/docs/api/customer-account-ui-extensions/configuration#api-access).
import {useEffect, useState} from 'react';
import {
reactExtension,
List,
ListItem,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const [data, setData] = useState();
useEffect(() => {
const getProductsQuery = {
query: `query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
variables: {first: 5},
};
const apiVersion = 'unstable';
fetch(
`shopify://storefront/api/${apiVersion}/graphql.json`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(getProductsQuery),
},
)
.then((response) => response.json())
.then(({data, errors}) => setData(data))
.catch(console.error);
}, []);
return (
<List>
{data?.products?.nodes.map((node) => (
<ListItem key={node.id}>
{node.title}
</ListItem>
))}
</List>
);
}
import {
extension,
List,
ListItem,
} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root) => {
const apiVersion = 'unstable';
const getProductsQuery = {
query: `query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
variables: {first: 5},
};
fetch(
`shopify://storefront/api/${apiVersion}/graphql.json`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(getProductsQuery),
},
)
.then((response) => response.json())
.then(({data}) => {
const listItems =
data?.products?.nodes.map((node) =>
root.createComponent(
ListItem,
undefined,
node.title,
),
);
root.appendChild(
root.createComponent(
List,
undefined,
listItems,
),
);
})
.catch(console.error);
},
);
import {useEffect, useState} from 'react';
import {
reactExtension,
List,
ListItem,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const [data, setData] = useState();
useEffect(() => {
const getProductsQuery = {
query: `query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
variables: {first: 5},
};
const apiVersion = 'unstable';
fetch(
`shopify://storefront/api/${apiVersion}/graphql.json`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(getProductsQuery),
},
)
.then((response) => response.json())
.then(({data, errors}) => setData(data))
.catch(console.error);
}, []);
return (
<List>
{data?.products?.nodes.map((node) => (
<ListItem key={node.id}>
{node.title}
</ListItem>
))}
</List>
);
}
import {
extension,
List,
ListItem,
} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root) => {
const apiVersion = 'unstable';
const getProductsQuery = {
query: `query ($first: Int!) {
products(first: $first) {
nodes {
id
title
}
}
}`,
variables: {first: 5},
};
fetch(
`shopify://storefront/api/${apiVersion}/graphql.json`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(getProductsQuery),
},
)
.then((response) => response.json())
.then(({data}) => {
const listItems =
data?.products?.nodes.map((node) =>
root.createComponent(
ListItem,
undefined,
node.title,
),
);
root.appendChild(
root.createComponent(
List,
undefined,
listItems,
),
);
})
.catch(console.error);
},
);