Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core 3.1: Getting a "Query is required" error on the /graphql endpoint #1595

Closed
dahlsailrunner opened this issue Mar 5, 2020 · 5 comments
Labels
question Developer asks to help him deal with some problem

Comments

@dahlsailrunner
Copy link

dahlsailrunner commented Mar 5, 2020

Description

Getting Query is required error. I can't get the graphql endpoint working in a brand new and very simple project that uses the ASP.NET Core DI engine and an existing database.

Steps to reproduce

Current code is here: https://github.com/dahlsailrunner/northwind-core-api
It's a very simple API based on "northwind" data.

I did check this issue: #1444
but it doesn't seem to have anything that can help (or I missed it).

I've tried with the FuncDependencyResolver and the commented-out AddScoped<NorthwindQuery>. Also, I have set a breakpoint in the Schema constructor where it sets the Query property and it appears to get set to the proper object that I would have expected. But the error persists and doesn't let me run any actual queries.

I'm running the "latest stable" versions of the packages:

<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.4.0" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.4.0" />

The code from Startup.cs looks like this:

public void ConfigureServices(IServiceCollection services)
{
	services.AddControllersWithViews();

	// Data access
	Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
	services.AddScoped<IDbConnection, NpgsqlConnection>(p =>
		new NpgsqlConnection(Configuration.GetConnectionString("NorthwindDatabase")));
	services.AddScoped<INorthwindRepository, NorthwindRepository>();

	// GraphQL
	services.AddScoped<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
	//services.AddScoped<NorthwindQuery>();
	services.AddScoped<NorthwindSchema>();

	services.AddGraphQL(o => o.ExposeExceptions = true)
		.AddGraphTypes(ServiceLifetime.Scoped);

	// REST
	services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
	// REST
	app.UseSwagger();
	app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

	//GraphQL
	app.UseGraphQL<NorthwindSchema>();
	app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());

	//app.UseHttpsRedirection();
	app.UseRouting();
	//app.UseAuthorization();
	app.UseEndpoints(endpoints =>
	{
		endpoints.MapControllers();
		endpoints.MapControllerRoute(
			name: "default",
			pattern: "{controller=Home}/{action=Index}/{id?}");
	});
}

You can see the simple code blocks for the Query, Schema, and Type in the "Graph" folder of the repo.

Expected result

I'm just trying to get the playground running and operational.

Actual result

Here's the response I got from the /graphql endpoint:

{"errors":[{"message":"GraphQL.ExecutionError: A query is required.\r\n ---> GraphQL.ExecutionError: A query is required.\r\n   at GraphQL.DocumentExecuter.ValidateOptions(ExecutionOptions options)\r\n   at GraphQL.DocumentExecuter.ExecuteAsync(ExecutionOptions options)\r\n   --- End of inner exception stack trace ---","extensions":{"code":"EXECUTION_ERROR"}}]}

Environment

Windows 10 - the rest is probably not important here.

@dahlsailrunner
Copy link
Author

Incidentally, I have the same basic code working in .net core 2.2 and it's working fine.

@sungam3r
Copy link
Member

sungam3r commented Mar 5, 2020

https://github.com/graphql-dotnet/graphql-dotnet#installation

WARNING: The latest stable version 2.4.0 has many known issues that have been fixed in 3.0.0-preview-XXXX versions. If errors occur, it is recommended that you first check the behavior on the latest available preview version before reporting a issue. Latest 3.0.0-preview-XXXX versions are backwards incompatible with latest stable 2.4.0 version. You can see the changes in public APIs using fuget.org.

You reference 2.3.0 version, even older one.

Please upgrade to the latest preview and check again. Also you need the latest server package https://github.com/graphql-dotnet/server

@sungam3r sungam3r added needs confirmation The problem is most likely resolved and requires verification by the author question Developer asks to help him deal with some problem labels Mar 5, 2020
@dahlsailrunner dahlsailrunner changed the title Getting a "Query is required" error on the /graphql endpoint ASP.NET Core 3.1: Getting a "Query is required" error on the /graphql endpoint Mar 5, 2020
@dahlsailrunner
Copy link
Author

dahlsailrunner commented Mar 5, 2020

I found my answer:
https://khalidabuhakmeh.com/dotnet-core-3-dot-0-allowsynchronousio-workaround

I had to add these lines:

services.Configure<KestrelServerOptions>(options => options.AllowSynchronousIO = true);
services.Configure<IISServerOptions>(options => options.AllowSynchronousIO = true);

My repo (data access calls) where indeed synchronous, but even when I made them async (returning Task) I still get the error without those lines above. So I think something inside the Playground logic is doing a synchronous read.

Feel free to close this issue as I have a workaround, but something may need to be done inside the Playground logic.

@sungam3r
Copy link
Member

sungam3r commented Mar 6, 2020

So I think something inside the Playground logic is doing a synchronous read.

No. It is all about async read/write on the server side - Newtonsoft.Json serializer. Now we have new SystemTextJson package, see server repo readme. GraphQL Playground is a frontend application.

@sungam3r sungam3r removed the needs confirmation The problem is most likely resolved and requires verification by the author label Mar 6, 2020
@dahlsailrunner
Copy link
Author

Thanks for the clarification - I simply meant something initiated by the playground. But after updating the packages and source code the problem was eliminated. The repo that I link to above shows fully working code with the changes that i made. Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Developer asks to help him deal with some problem
Projects
None yet
Development

No branches or pull requests

2 participants