簡體   English   中英

GraphQL - 類型為 \"Owner!\" 的“字段 \"updateOwner\" 必須選擇子字段。您的意思是 \"updateOwner { ... }\"嗎?”

[英]GraphQL - "Field \"updateOwner\" of type \"Owner!\" must have a selection of subfields. Did you mean \"updateOwner { ... }\"?"

我正在嘗試在 GraphQL Playground 中獲取 Mutation Update 查詢。 我在 GraphQL 和學習階段處於基礎水平。 我不知道如何為以下所有者代碼創建 udpate Mutation。 知道我的代碼/查詢中缺少什么嗎?

---解析器---

>   @Mutation(() => Owner)   updateOwner(
>     @Args('id', { type: () => Int }) id: number,
>     @Args('updateOwnerInput') updateOwnerInput: UpdateOwnerInput) {
>     return this.ownersService.update(id, updateOwnerInput);   }

- -服務 - -

  update(id: number, updateOwnerInput: UpdateOwnerInput) {
    return this.ownersRepository.update(id, updateOwnerInput);
  }

---dto---

@InputType()
export class UpdateOwnerInput extends PartialType(CreateOwnerInput) {
  @Column()
  @Field(() => Int)
  id: number;
}

- -實體 - -

@Entity()
@ObjectType()
export class Owner {
  @PrimaryGeneratedColumn()
  @Field(type => Int)
  id: number;

  @Column()
  @Field()
  name: string;

  @OneToMany(() => Pet, pet => pet.owner)
  @Field(type => [Pet], { nullable: true })
  pets?: Pet[];

}

---架構---

type Pet {
  id: Int!
  name: String!
  type: String
  ownerId: Int!
  owner: Owner!
}

type Owner {
  id: Int!
  name: String!
  pets: [Pet!]
}

type Query {
  getPet(id: Int!): Pet!
  pets: [Pet!]!
  owners: [Owner!]!
  owner(id: Int!): Owner!
}

type Mutation {
  createPet(createPetInput: CreatePetInput!): Pet!
  createOwner(createOwnerInput: CreateOwnerInput!): Owner!
  updateOwner(id: Int!, updateOwnerInput: UpdateOwnerInput!): Owner!
}

input CreatePetInput {
  name: String!
  type: String
  ownerId: Int!
}

input CreateOwnerInput {
  name: String!
}

input UpdateOwnerInput {
  name: String
  id: Int!
}

---GraphQL查詢(不知道對錯)

mutation {
  updateOwner (updateOwnerInput:{
    id:6,
    name: "josh",    
  })
}

- -錯誤 - -

"message": "Field \"updateOwner\" of type \"Owner!\" must have a selection of subfields. Did you mean \"updateOwner { ... }\"?",

您需要選擇要返回的子字段(即使您對任何子字段都不感興趣):

mutation {
  updateOwner (updateOwnerInput:{
    id:6,
    name: "josh",    
  })
  {
    id
    name
  }
}

我嘗試了下面的代碼。 更新突變工作並能夠更新數據庫列中的字段。

mutation updateOwner{
  updateOwner(id:2, updateOwnerInput: {
    id:2,
    name: "test2"
  })
  {
  id,
  name
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM