[英]Nestjs graphql, How to make refer to it self input type?
I want to make nestjs graphql input type with refer to it self.我想自己参考它来制作nestjs graphql输入类型。 so it can be nested object.
所以它可以是嵌套对象。
I want to do is我想做的是
type Tree = {
[key: string]: Tree | string | number;
};
// it can be like
const example1: Tree = {
user: {
name: 'foo',
},
};
//or
const example2: Tree = {
id: {
$eq: 1,
},
};
Change it to graphql input type, but i can not figure out.将其更改为 graphql 输入类型,但我无法弄清楚。
I tried to do is我试图做的是
@InputType()
export class TreeClass {
@Field(() => TreeClass)
@IsNotEmpty()
tree: string;
}
@InputType()
export class AddQsInput {
...
@Field(() => TreeClass, { nullable: true })
@IsNotEmpty()
@IsOptional()
filters?: any;
...
}
error like错误如
GraphQLError: Cannot reference Input Object "TreeClass" within itself through a series of non-null fields: "tree".
I found how to.我找到了方法。
It isn't perfect answer, but it can be replacement.这不是完美的答案,但可以替代。
The answer is 'graphql-type-json'.答案是'graphql-type-json'。
With this, You can write any type of JSON in input or type有了这个,您可以在 input 或 type 中编写任何类型的 JSON
how to use
with npm与 npm
$ npm i --save graphql-type-json
or with yarn或用纱线
$ yarn add graphql-type-json
and then接着
//app.module.ts
import GraphQLJSON from 'graphql-type-json';
@Module({
imports: [
GraphQLModule.forRoot({
resolvers: { JSON: GraphQLJSON },
}),
],
})
export class AppModule {}
//user.input.ts
...
@Field((type) => GraphQLJSON)
info: JSON;
...
reference参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.