Skip to main content

useDataSourceV2

This hook provides data fetching for single-page (non-paginated) data sources.

Import

import { useDataSourceV2 } from '@appmaker-xyz/core';

Basic Usage

const [queryResult, { item }] = useDataSourceV2({
dataSource: {
source: 'shopify',
methodName: 'productDetail',
params: { handle: 'my-product' },
dataExtractor: (response) => response?.data?.data?.product,
},
enabled: true,
});

if (queryResult.isLoading) return <Loading />;

// item contains the extracted data
console.log(item.title);

Parameters

ParameterTypeDescription
dataSourceObjectData source configuration (see below).
dataSource.sourcestringThe data source identifier (e.g., 'shopify').
dataSource.methodNamestringThe method to call on the data source.
dataSource.paramsObjectParameters passed to the data source method.
dataSource.dataExtractorFunctionOptional function to extract the desired data from the raw response.
enabledbooleanWhether the query should execute.
filterParamsObjectOptional filter parameters appended to the query key.

Return Value

Returns a tuple [queryResult, dataSourceResult]:

IndexTypeDescription
0ObjectQuery result object containing isLoading, isError, data, refetch, etc.
1ObjectContains item — the extracted data (via dataExtractor if provided, otherwise the raw response).

Applying a GraphQL Query

To fetch data using a custom Shopify Storefront API GraphQL query, use methodName: 'gqlQuery' with the query and variables params:

const [{ isLoading }, { item }] = useDataSourceV2({
dataSource: {
source: 'shopify',
methodName: 'gqlQuery',
params: {
query: `
query GetProduct($handle: String!) {
product(handle: $handle) {
id
title
description
priceRange {
minVariantPrice {
amount
currencyCode
}
}
}
}
`,
variables: { handle: 'my-product-handle' },
},
dataExtractor: (response) => response?.data?.data?.product,
},
enabled: !!handle,
});