我正在尝试运行一个Id
类型为标量Int
的graphql查询。
仅编写Int
无效。 Number
也不起作用。然后我尝试了这个:
import { Int } from "type-graphql";
interface WhereInput {
phoneNumber_contains?: String;
id?: typeof Int;
}
但是这也不起作用。
const where: WhereInput = {};
if (criteria == '6') {
if (searchItem) {
where.id = Number(searchItem);
loadUsers({
variables: {
where: { id: searchItem },
},
});
}
}
现在我在where.id
上看到了一个错误
Type 'number' is not assignable to type 'GraphQLScalarType | undefined'.
searchItem
是从文本字段读取的,默认情况下为字符串。如何将其更改为Int?
我的查询:
interface UserFilter {
phoneNumber_contains?: String;
id?: typeof Int;
}
export const LoadUsersQuery = gql`
query usersList($where: UserFilter) {
users(where: $where) {
...
}
}
`;
答案 0 :(得分:1)
您无法通过使用typeof
TS运算符来确定Int标量接受的类型。该运算符返回标量的类型为GraphQLScalarType
。为什么不简单地说:
interface WhereInput {
phoneNumber_contains?: string;
id?: number;
}
在前端,您可能不想使用type-graphql
。相反,您可以使用graphql-code-generator生成类型。
TypeScript中的原始类型以小写形式编写。