Graphql 是什么导致React中Apollo客户端查询出现这种奇怪的400服务器错误?

Graphql 是什么导致React中Apollo客户端查询出现这种奇怪的400服务器错误?,graphql,react-apollo,apollo-client,Graphql,React Apollo,Apollo Client,我得到了一些非常奇怪的行为,我不完全确定系统输出的400错误以外的问题到底是什么。 我使用useQueryParams从URL获取变量,Apollo客户端在graphql查询中使用这些变量并显示数据 const Search = () => { const [query, setQuery] = useQueryParams({ location: withDefault(ArrayParam, []), bedrooms: withDefault

我得到了一些非常奇怪的行为,我不完全确定系统输出的400错误以外的问题到底是什么。 我使用useQueryParams从URL获取变量,Apollo客户端在graphql查询中使用这些变量并显示数据

const Search = () => {

    const [query, setQuery] = useQueryParams({
        location: withDefault(ArrayParam, []),
        bedrooms: withDefault(ArrayParam, []),
    });

    const { location, bedrooms } = query;

    const chosenLocation = location[0];
    const chosenBedrooms = bedrooms[0];

    console.log('chosenLocation: ', chosenLocation, 'chosenBedrooms', chosenBedrooms);

const { data, loading, error } = useQuery(GET_VILLAS,
    {
        variables: { chosenLocation, chosenBedrooms },
    }
);

if (loading) return 'Loading your search results...';
if (error) return `Search Results Error: ${error.message}`;

return (
    <div>
        {data.villas.map(villa => (
            <Card
                ...
            />
        ))}
    </div>
);
};

const GET_VILLAS = gql`
query GetVillas(
    $chosenLocation: String!,
    $chosenBedrooms: Int!,
    ) {
    villas(where:{
        villaLocation: $chosenLocation,
        bedrooms: $chosenBedrooms,
    }){
        id
        name
        villaLocation
        bedrooms
        ...
    }
}
`;

export default Search
返回正确的数据,如果我更改此项:

const chosenLocation = location[0];
const chosenBedrooms = bedrooms[0];
要硬编码console.log输出的内容,它可以工作

const chosenLocation = "Bali"
const chosenBedrooms = 2;
如果我删除对第二个变量“ChosenBeddrooms”的所有引用,它也会起作用,即从这里:

variables: { chosenLocation, chosenBedrooms }
在这里的查询中:

query GetVillas(
    $chosenLocation: String!,
    $chosenBedrooms: Int!,
    ) {
    villas(where:{
        villaLocation: $chosenLocation,
        bedrooms: $chosenBedrooms,
我使用“isNaN”检查了“ChosenBeddooms”是否作为数字返回。我不知道是什么原因导致了这一点,以及到底是什么问题。我认为问题在于将两个变量传递到查询中,但是如果我硬编码它们,它就会工作。这表明数据类型是错误的,但是如果我在GraphQL中运行这个精确的查询,它就可以正常工作。所以我想我需要知道400错误的原因。这快把我逼疯了! 干杯
Matt

如果您可以在Playerd中成功运行完全相同的查询,则表明变量值本身无效。无论哪种方式,都可以在浏览器的开发工具中检查服务器的响应。服务器返回的有效负载将包含一个
errors
数组,该数组包含错误的详细信息。请尝试准备整个/ready对象并将其传递给“where”arg-Ah yes,Dev tools output:{“message”:“变量'chosenbeddrooms'的预期类型'Int',get'String'”-我猜isNaN中的数字与整数不同。谢谢大家
query GetVillas(
    $chosenLocation: String!,
    $chosenBedrooms: Int!,
    ) {
    villas(where:{
        villaLocation: $chosenLocation,
        bedrooms: $chosenBedrooms,