Reputation: 1007
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>();
}
Upvotes: 1
Views: 3230
Reputation: 2629
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.
Upvotes: 1
Reputation: 89
You can try this:
public void ConfigureServices(IServiceCollection services)
{
services.AddGraphQLServer()
.ConfigureSchema(sb => sb.ModifyOptions(opts => opts.StrictValidation = false))
.AddMutationType<Mutation>();
}
Upvotes: 2