类型'number'不能分配给'GraphQLScalarType |类型。未定义”

时间:2020-04-21 10:28:54

标签: javascript reactjs typescript graphql

我正在尝试运行一个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) {
      ...
    }
  }
`;

1 个答案:

答案 0 :(得分:1)

您无法通过使用typeof TS运算符来确定Int标量接受的类型。该运算符返回标量的类型为GraphQLScalarType。为什么不简单地说:

interface WhereInput {
  phoneNumber_contains?: string;
  id?: number;
}

在前端,您可能不想使用type-graphql。相反,您可以使用graphql-code-generator生成类型。

TypeScript中的原始类型以小写形式编写。