我正在尝试在 vue 中运行 useLazyQuery 以从 Apollo 客户端获取数据。我传递参数,查询根据传递的参数返回不同的结果。
我的问题是,Apollo 文档中提到的第一次加载后结果变为 false:
load(document?, variables?, options?): function to start querying. Returns Promise<Result> if it is the first time the query is called, false otherwise.
我希望能够在每次更改参数或调用 load() 函数时重新加载数据。我的代码如下:
const iqConfigSettingsFolder = computed(
() =>
'/sitecore/content/ABC/BCD/Settings/Config Settings Folder/' +
configCategory.value +
'/' +
configSettingName.value
);
const { load, refetch } = useLazyQuery(
configSettingsQuery,
{
path: iqConfigSettingsFolder, //data changes based on path change
},
{ fetchPolicy: 'no-cache' }
);
const getSetting = async (
settingName: string,
settingCategory: SettingCategory
): Promise<string | number | boolean | undefined> => {
configCategory.value = settingCategory;
configSettingName.value = settingName;
let result = await load(); //this one returns false after first run
if (!result) {
result = await refetch(); //this one returns the same results after every run
}
const setting = result?.item?.fields?.filter((x: any) => x.name == 'Value')[0]?.value;
if (setting) {
const isBool = setting.toLowerCase() == 'true' || setting.toLowerCase() == 'false';
if (isBool) return toBool(setting);
if (parseFloat(setting)) return toNumber(setting);
return setting;
}
return;
};
如果我理解正确的话,每次变量 iqConfigSettingsFolder 更改时,您都需要调用
getSetting()
。我会使用Watcher。
watch(iqConfigSettingsFolder,async (newPath, oldPath) => {
await getSetting(configSettingName.value, configCategory.value);
},
{ immediate: true } // Trigger on initialization
);