我试图在GraphQL Playground中获得突变更新查询。我在GraphQL和学习阶段的基础水平。我不知道如何为下面的所有者代码创建更新突变。你知道我在代码/查询中缺少什么吗?
——解析器
> @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
}
}
我尝试了下面的代码。Update Mutation工作,能够更新Database列中的字段。
mutation updateOwner{
updateOwner(id:2, updateOwnerInput: {
id:2,
name: "test2"
})
{
id,
name
}
}