Shrimpis
Shrimpis

Reputation: 133

"Schema is not configured for mutations."

In my project I am using npm, apollo server and typeorm to set up my server. When I try to create a mutation i get this error message "Schema is not configured for mutations". I have tried searching for a solution but in all the other solutions they are using different libraries than me, so they don't really help... I have also looked at examples and in their code it works fine to import the mutation into the resolver file and then import the resolver into the schema. As it also says that the "schema is not configured" I wonder if I need to add a configuration to my tsconfig.json which allows the project to use schemas with mutations.

The mutation looks as such:

import {InputType, Field} from "type-graphql";

@InputType()
export class CreateBookInput{
    @Field()
    title: string;

    @Field()
    author: string;
}

I import this class to my 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;
    }

}

This is later on imported in my index.ts file where I also handle my schema:

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();

Any help is highly valued

Upvotes: 2

Views: 3302

Answers (1)

Shrimpis
Shrimpis

Reputation: 133

Somehow it works now.

I compiled the project again using tsc and launched it and this time it worked without failure. I thought using npm start also compiled the project at the same time, but I guess I was wrong.

Upvotes: 1

Related Questions