找到预期值 [object, Object] 查询参数

Expected value found [object, Object] Query Params

我不明白,我在这里发送了正确的输入类型:

来自 JS 代码的调用者:

export interface FilterInput {
    limit?: number;
}

const filter: FilterInput = { limit: 1 };
const query = {
    query: `query { 
        companies(filter: ${filter}) {
            list {
                name
            }
        }
    }`,
};
const response = await sendQuery(query);
        

GraphQL 模式:

companies(filter: FilterInput): Companies

input FilterInput {
  limit: Int
}

错误: Expected value of type "FilterInput", found [object, Object].

当我在浏览器的 graphiql 中尝试这个时,它工作正常:

query {
    companies(filter: { limit: 6 }) {
        list {
            name
        }
    }
}

在模板字符串中使用 js 对象直接解析为 [object Object]${filter} 精确解析为 [object Object] 因此出现错误。

要解决它,您可以使用 JSON.stringify(filter)。这是与以前格式相同但有修正的字符串:

`query { 
    companies(filter: ${JSON.stringify(filter)}) {
        list {
            name
        }
    }
}`