I’m trying to get Mutation Update query in GraphQL Playground. I’m basic level in GraphQL and in learning phase. I don’t know how to create udpate Mutation for the below Owner code. Any idea what I’m missing in my code / query?
—Resolver—
JavaScript
x
5
1
> @Mutation(() => Owner) updateOwner(
2
> @Args('id', { type: () => Int }) id: number,
3
> @Args('updateOwnerInput') updateOwnerInput: UpdateOwnerInput) {
4
> return this.ownersService.update(id, updateOwnerInput); }
5
—Service—
JavaScript
1
4
1
update(id: number, updateOwnerInput: UpdateOwnerInput) {
2
return this.ownersRepository.update(id, updateOwnerInput);
3
}
4
—dto—
JavaScript
1
7
1
@InputType()
2
export class UpdateOwnerInput extends PartialType(CreateOwnerInput) {
3
@Column()
4
@Field(() => Int)
5
id: number;
6
}
7
—entity—
JavaScript
1
17
17
1
@Entity()
2
@ObjectType()
3
export class Owner {
4
@PrimaryGeneratedColumn()
5
@Field(type => Int)
6
id: number;
7
8
@Column()
9
@Field()
10
name: string;
11
12
@OneToMany(() => Pet, pet => pet.owner)
13
@Field(type => [Pet], { nullable: true })
14
pets?: Pet[];
15
16
}
17
—schema—
JavaScript
1
42
42
1
type Pet {
2
id: Int!
3
name: String!
4
type: String
5
ownerId: Int!
6
owner: Owner!
7
}
8
9
type Owner {
10
id: Int!
11
name: String!
12
pets: [Pet!]
13
}
14
15
type Query {
16
getPet(id: Int!): Pet!
17
pets: [Pet!]!
18
owners: [Owner!]!
19
owner(id: Int!): Owner!
20
}
21
22
type Mutation {
23
createPet(createPetInput: CreatePetInput!): Pet!
24
createOwner(createOwnerInput: CreateOwnerInput!): Owner!
25
updateOwner(id: Int!, updateOwnerInput: UpdateOwnerInput!): Owner!
26
}
27
28
input CreatePetInput {
29
name: String!
30
type: String
31
ownerId: Int!
32
}
33
34
input CreateOwnerInput {
35
name: String!
36
}
37
38
input UpdateOwnerInput {
39
name: String
40
id: Int!
41
}
42
—GraphQL Query (I don’t know whether it is correct or wrong)
JavaScript
1
7
1
mutation {
2
updateOwner (updateOwnerInput:{
3
id:6,
4
name: "josh",
5
})
6
}
7
—error—
JavaScript
1
2
1
"message": "Field "updateOwner" of type "Owner!" must have a selection of subfields. Did you mean "updateOwner { }"?",
2
Advertisement
Answer
You need to make a selection of subfields to return (even if you’re not interested in any):
JavaScript
1
11
11
1
mutation {
2
updateOwner (updateOwnerInput:{
3
id:6,
4
name: "josh",
5
})
6
{
7
id
8
name
9
}
10
}
11