【问题标题】:Type 'number' is not assignable to type 'GraphQLScalarType | undefined'类型 'number' 不可分配给类型 'GraphQLScalarType |不明确的'
【发布时间】:2020-08-04 01:41:23
【问题描述】:

我正在尝试运行 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.idthat 上收到一个错误

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) {
      ...
    }
  }
`;

【问题讨论】:

    标签: javascript reactjs typescript graphql


    【解决方案1】:

    您无法通过使用typeof TS 运算符找出 Int 标量接受的类型。此运算符返回标量的类型,即GraphQLScalarType。为什么不简单地说:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2023-01-22
      • 2017-01-05
      • 2021-01-20
      • 2022-01-16
      相关资源
      最近更新 更多