Vue 3 apollo 客户端 UseLazyQuery 获取多个结果

问题描述 投票:0回答:1

我正在尝试在 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;
  };
vue.js apollo apollo-client vue-apollo
1个回答
0
投票

如果我理解正确的话,每次变量 iqConfigSettingsFolder 更改时,您都需要调用

getSetting()
。我会使用Watcher

watch(iqConfigSettingsFolder,async (newPath, oldPath) => {
    await getSetting(configSettingName.value, configCategory.value);
  },
  { immediate: true } // Trigger on initialization
);
© www.soinside.com 2019 - 2024. All rights reserved.