简体   繁体   中英

Graphql-Mutation : Schema Exception

I am trying to implement GraphQL -mutation using HotChocolate. But there is some issue with the schema (https://localhost:1234/graphql?sdl) and I am getting the unhandled exception:

The schema builder was unable to identify the query type of the schema. Either specify which type is the query type or set the schema builder to non-strict validation mode.

SchemaException: For more details look at the Errors property. 1. The schema builder was unable to identify the query type of the schema. Either specify which type is the query type or set the schema builder to non-strict validation mode.

My mutation code:

using HotChocolate

public class Book
{
    public int Id { get; set; }

    [GraphQLNonNullType]
    public string Title { get; set; }

    public int Pages { get; set; }

    public int Chapters { get; set; }
}

public class Mutation
{
    public async Task<Book> Book(string title, int pages, string author, int chapters)
    {
        var book = new Book
        {
            Title = title,
            Chapters = chapters,
            Pages = pages,
        };

        return book;
    }
}

I have added the following in the API startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQLServer().AddMutationType<Mutation>();
}

You can try this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQLServer()
            .ConfigureSchema(sb => sb.ModifyOptions(opts => opts.StrictValidation = false))
            .AddMutationType<Mutation>();
}

This seems to happen when you register only a mutation type. The error goes away if you also register a query type.

So you could add the following to your code

public class Query
{
    public string HelloWorld()
    {
        return "Hello, from GraphQL!";
    }
}

And register it in your services:

public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQLServer()
        .AddMutationType<Mutation>()
        .AddQueryType<Query>();
}

Obviously this isnt an ideal solution, as it's possible that you intend fr your API to only have mutations (although that's probably unlikely). If, like me, your API will support both queries and mutations but you just started building the mutations, you could add this query in as a place holder for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM