繁体   English   中英

Nestjs graphql,如何参考它的自我输入类型?

[英]Nestjs graphql, How to make refer to it self input type?

我想自己参考它来制作nestjs graphql输入类型。 所以它可以是嵌套对象。

我想做的是

type Tree = {
  [key: string]: Tree | string | number;
};

// it can be like
const example1: Tree = {
  user: {
    name: 'foo',
  },
};

//or
const example2: Tree = {
  id: {
    $eq: 1,
  },
};

将其更改为 graphql 输入类型,但我无法弄清楚。

我试图做的是

@InputType()
export class TreeClass {
  @Field(() => TreeClass)
  @IsNotEmpty()
  tree: string;
}

@InputType()
export class AddQsInput {
...
  @Field(() => TreeClass, { nullable: true })
  @IsNotEmpty()
  @IsOptional()
  filters?: any;
...
}

错误如

GraphQLError: Cannot reference Input Object "TreeClass" within itself through a series of non-null fields: "tree".

我找到了方法。

这不是完美的答案,但可以替代。

答案是'graphql-type-json'。

有了这个,您可以在 input 或 type 中编写任何类型的 JSON

how to use

与 npm

$ npm i --save graphql-type-json

或用纱线

$ yarn add graphql-type-json

接着

//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;
...

参考

nestjs-标量类型

graphql-type-json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM