Having an issue with setting up mutations in my project using npm, apollo server, and typeorm. Every time I attempt to create a mutation, I receive the error message "Schema is not configured for mutations". Searching for solutions has been fruitless as most explanations involve different libraries that do not apply to my setup. Even after studying examples where importing mutations into resolver files seems successful, the same error persists. Could it be necessary to modify the tsconfig.json file to allow schemas with mutations?
This is what the mutation code looks like:
import {InputType, Field} from "type-graphql";
@InputType()
export class CreateBookInput{
@Field()
title: string;
@Field()
author: string;
}
The imported class is used in the resolver class:
import { Resolver, Query, Mutation, Arg } from "type-graphql";
import { Book } from "../models/book";
import {CreateBookInput} from "../inputs/createBookInput";
@Resolver()
export class BookResolver {
@Query(() => [Book])
books() {
return Book.find()
}
@Mutation(() => Book)
async createBook(@Arg("data") data: CreateBookInput) {
const book = Book.create(data);
await book.save();
return book;
}
}
The resolver class is then imported into the index.ts file where the schema is handled:
import "reflect-metadata";
import { createConnection } from "typeorm";
import { ApolloServer } from "apollo-server";
import { BookResolver } from "./resolvers/bookResolver"; // add this
import { buildSchema } from "type-graphql";
async function main() {
const connection = await createConnection()
const schema = await buildSchema({
resolvers: [BookResolver]
})
const server = new ApolloServer({ schema })
await server.listen(4000)
console.log("Server has started!")
}
main();
Appreciate any assistance or guidance on this matter.