@sparklink-pro/apant
Version:
Apollo & Antd tools
22 lines • 1.23 kB
JavaScript
import { __rest } from "tslib";
import { useQuery } from '@apollo/client';
import { OPERATION_GET } from '../definitions';
import { replaceFragments } from '../helpers/gql';
import { useRegistry } from './useRegistry';
/**
* This hook returns the item for the given type and id. it use `useQuery` to perform a GraphQL query.
* It will use the default item operation defined in the registry (OPERATION_GET) for the type and the default fragment unless a custom fragment is provided.
* The query is expected to return a valid payload of the form `{ item: {} }`.
*/
export const useTypeItem = (_a) => {
var { type, id, fragment } = _a, props = __rest(_a, ["type", "id", "fragment"]);
const registry = useRegistry();
const operation = registry.getOperation(type, OPERATION_GET);
const { operations: { get: { getResult }, }, } = registry.getType(type);
const query = fragment ? replaceFragments(operation, { [registry.getTypeFragmentName(type)]: fragment }) : operation;
const { data, loading } = useQuery(query, Object.assign({ variables: { id }, skip: !id }, props));
const item = getResult(data);
return { loading, item };
};
export default useTypeItem;
//# sourceMappingURL=useTypeItem.js.map