【问题标题】:Define GraphQL object type in .graphql files not working在 .graphql 文件中定义 GraphQL 对象类型不起作用
【发布时间】:2021-09-12 04:08:04
【问题描述】:

我正在尝试在 type-graphql 中进行突变,其中一个参数是 Object 所以:

@Mutation(() => Boolean)
async createProduct(
    @Arg('name', () => String) name: string,
    @Arg('price', () => Int) price: number,
    @Arg('images', () => [InputImages]) images: [ImagesArray],
) {
    // Code
}

所以我为参数“images”创建了一个输入类型:

@InputType()
class InputImages {
  @Field(() => String)
  filename: string;
}

还有一个对象类型:

@ObjectType()
export class ImagesArray {
    @Field(() => String)
    filename: string
}

现在我正在尝试使用 GraphQL 代码生成器,我需要在客户端编写查询和突变作为 .graphql 文件扩展名,所以我有:

mutation CreateProduct(
    $name: String!
    $price: Int!
    $images: [ImagesArray]!
) {
    createProduct(
        name: $name
        price: $price
        images: $images
    );
}

现在当我尝试运行 "npm run gen" 时说:

GraphQLDocumentError:未知类型“ImagesArray”。

我尝试在上面添加:

type ImagesArray {
    filename: String
}

不能再工作了,知道吗?

【问题讨论】:

    标签: typescript graphql typegraphql graphql-tools graphql-codegen


    【解决方案1】:
    1. 你不需要ImagesArray 输入args:
    @Arg('images', () => [InputImages]) images: InputImages[],
    
    1. 更新您的突变类型名称:
    mutation CreateProduct(
        $name: String!
        $price: Int!
        $images: [InputImages!]!
    ) {
      # ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-15
      • 2021-01-09
      • 2021-11-20
      • 2019-06-24
      • 2020-03-29
      • 1970-01-01
      • 2018-08-15
      • 2021-11-18
      • 2019-05-17
      相关资源
      最近更新 更多