简体   繁体   English

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

[英]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参考

nestjs-scalartype nestjs-标量类型

graphql-type-json graphql-type-json

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

相关问题 NestJs - Graphql 错误无法确定 GraphQL 输入类型<field> . 确保您的 class 已使用合适的装饰器进行装饰</field> - NestJs - Graphql Error Cannot determine a GraphQL input type for the <Field>. Make sure your class is decorated with an appropriate decorator NestJS 中与 GraphQL 的 Prisma 自关系 - Prisma Self Relation with GraphQL in NestJS 在 NestJS 中使用与 GraphQL 中的 Input 和 Object 类型相同的类 - Use the same class as Input and Object type in GraphQL in NestJS "错误:使用 Nestjs + Graphql + Typeorm 时无法确定 GraphQL 输入类型" - Error: Cannot determine a GraphQL input type, while using Nestjs + Graphql + Typeorm 如何在 Nestjs + Graphql Mutation Args 中定义 ID 类型? - How to define ID type in Nestjs + Graphql Mutation Args? Graphql NestJS:未定义类型错误。 确保您提供显式类型 - Graphql NestJS: Undefined type error. Make sure you are providing an explicit type Graphql Nestjs 将 arguments 添加到类型字段 - Graphql Nestjs add arguments to type field TypeScript可以参考自己的房产类型吗? - Is it possible to refer self property type in TypeScript? 如何使用 [nestjs/graphql] 在解析器中实现搜索 - How to implement a search in a resolver using [nestjs/graphql] 如何在 NestJS GraphQL 解析器中访问响应 object - How to access Response object in NestJS GraphQL resolver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM