当GraphQL查询';What’需要一个枚举(Apollo服务器)?

当GraphQL查询';What’需要一个枚举(Apollo服务器)?,graphql,apollo,apollo-server,keystonejs,Graphql,Apollo,Apollo Server,Keystonejs,我正在尝试在KeystoneJS应用程序中进行GraphQL查询,在该应用程序中,我需要确定是否存在某种类型的页面(如果存在这种类型的页面,我将不创建它——我将把每种页面类型实现为单例)。我的想法是搜索所有带有where条件的页面,在该条件下,我传递我试图创建的页面类型 const PAGE_QUERY = ` query allPages($where:PageWhereInput){ allPages(where:$where) { pageType } }

我正在尝试在KeystoneJS应用程序中进行GraphQL查询,在该应用程序中,我需要确定是否存在某种类型的页面(如果存在这种类型的页面,我将不创建它——我将把每种页面类型实现为单例)。我的想法是搜索所有带有
where
条件的页面,在该条件下,我传递我试图创建的页面类型

const PAGE_QUERY = `
    query allPages($where:PageWhereInput){
      allPages(where:$where) {
    pageType
  }
}
`;

export const doesPageTypeExist = async (query, articleType: string): Promise<boolean> => {
    const where = {
        pageType: articleType
    };
    const options = {
        skipAccessControl: false,
        variables: {
            where
        }
    };
    const { errors, data } = await query(allPages, options);
    if (errors) {
        log.error({ errors }, 'errors from GraphQL operation in Page list');
        throw new Error(errors.toString());
    } else {
        return data?.allPages.length !== 0;
    }
};

我给它传递了一个字符串,但我不知道如何传递我正在测试的值(来自Keystone),除了作为字符串。当用作变量时,枚举值总是序列化为字符串,所以问题不在于如何提交枚举值。根据错误,问题在于
$where
pageType
属性的值。似乎
pageType
的值实际上是对象
{pageType:“page2”}
,而不是
page2
。换句话说,您的
变量
属性如下所示:

{
  "where": {
    "pageType": {
      "pageType": "page2"
    }
  }
}

根据显示的代码,我猜传递给
DoesPagetTypeExist
articleType
参数可能不是其类型所表示的字符串。

的确如此!我自己刚刚发现了这一点,正在返回删除该问题!我在传递一个物体,我以为我在传递一根绳子。
{
  "where": {
    "pageType": {
      "pageType": "page2"
    }
  }
}