example to use @Mutation from type-graphql
- User
example to use @Mutation from type-graphql
- Assistant
Here is an example usage of @Mutation decorator from type-graphql:
import { Resolver, Mutation, Arg } from "type-graphql"; import { CreateBookInput } from "../inputs/create-book.input"; import { Book } from "../models/book.model"; @Resolver() export class BookResolver { @Mutation(() => Book) async createBook(@Arg("data") data: CreateBookInput): Promise<Book> { const newBook = await Book.create(data).save(); return newBook; } }In this example, we have created a resolver for a
createBookmutation. The@Mutationdecorator is applied to thecreateBookmethod to specify that this is a mutation resolver. The method takes aCreateBookInputobject as an argument and returns aBookobject.The
@Argdecorator is used to specify that thedataparameter is an input argument for the mutation. TheCreateBookInputinterface is defined elsewhere and represents the shape of the input data expected by this mutation.