【发布时间】:2022-01-17 13:52:43
【问题描述】:
我已经为我的后端服务器设置了带有 NodeJs 的 Typescript。在其中一个实体文件中,我使用了 graphql-type-json 包中的 GraphQLJSONObject。
例子:
import { GraphQLJSONObject } from 'graphql-type-json';
...
@Field(() => GraphQLJSONObject, { nullable: true })
field_name: GraphQLJSONObject; //error line
编译时出现如下错误:
'GraphQLJSONObject' refers to a value, but is being used as a type here. Did you mean 'typeof GraphQLJSONObject'?ts(2749)
所有GraphQLJSONObject 类型出现在存储库中都会发生这种情况。
示例 2: 在字段解析器的情况下。例如:
async user_details(
@Root() parent: Promise<User>,
): Promise<GraphQLJSONObject> { //error
const user = await parent;
if (user.is_super_admin) {
return { superAdmin: user.is_super_admin };
} else return null;
}
如果我添加 Promise<typeof GraphQLJSONObject> 而不是 Promise<GraphQLJSONObject>,那么我开始收到错误:
Type '{ superAdmin: true; }' is not assignable to type 'GraphQLScalarType'.ts(2322)
Type 'null' is not assignable to type 'GraphQLScalarType'.ts(2322)
我该如何解决这个问题,期待对此提供帮助。
【问题讨论】:
标签: node.js typescript graphql