Hot Chocolatev14

Endpoints

Hot Chocolate comes with a set of ASP.NET Core middleware used for making the GraphQL server available via HTTP and WebSockets. There are also middleware for hosting our GraphQL IDE Nitro as well as an endpoint used for downloading the schema in its SDL representation.

MapGraphQL

We can call MapGraphQL() on the IEndpointRouteBuilder to register all of the middleware a standard GraphQL server requires.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL();
});

If you are using .NET 6 Minimal APIs, you can also call MapGraphQL() on the app builder directly, since it implements IEndpointRouteBuilder:

C#
var builder = WebApplication.CreateBuilder(args);
// Omitted code for brevity
var app = builder.Build();
app.MapGraphQL();
app.Run();

The middleware registered by MapGraphQL makes the GraphQL server available at /graphql per default.

We can customize the endpoint at which the GraphQL server is hosted like the following.

C#
endpoints.MapGraphQL("/my/graphql/endpoint");

Calling MapGraphQL() will enable the following functionality on the specified endpoint:

  • HTTP GET and HTTP POST GraphQL requests are handled (Multipart included)
  • WebSocket GraphQL requests are handled (if the ASP.NET Core WebSocket Middleware has been registered)
  • Including the query string ?sdl after the endpoint will download the GraphQL schema
  • Accessing the endpoint from a browser will load our GraphQL IDE Nitro

We can customize the combined middleware using GraphQLServerOptions as shown below or we can only include the parts of the middleware we need and configure them explicitly.

The following middleware are available:

GraphQLServerOptions

We can influence the behavior of the middleware registered by MapGraphQL using GraphQLServerOptions.

EnableSchemaRequests

C#
endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
{
EnableSchemaRequests = false
});

This setting controls whether the schema of the GraphQL server can be downloaded by appending ?sdl to the endpoint.

EnableGetRequests

C#
endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
{
EnableGetRequests = false
});

This setting controls whether the GraphQL server is able to handle GraphQL operations sent via the query string in a HTTP GET request.

AllowedGetOperations

C#
endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
{
AllowedGetOperations = AllowedGetOperations.Query
});

If EnableGetRequests is true we can control the allowed operations for HTTP GET requests using the AllowedGetOperations setting.

Per default only queries are accepted via HTTP GET. We can also allow mutations by setting AllowedGetOperations to AllowedGetOperations.QueryAndMutation.

EnableMultipartRequests

C#
endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
{
EnableMultipartRequests = false
});

This setting controls whether the GraphQL server is able to handle HTTP Multipart forms, i.e. file uploads.

Learn more about uploading files

Tool

We can specify options for the Nitro using the Tool property.

We could for example only enable Nitro during development.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL().WithOptions(new GraphQLServerOptions
{
Tool = {
Enable = env.IsDevelopment()
}
});
});

Learn more about possible GraphQLToolOptions

MapNitroApp

We can call MapNitroApp() on the IEndpointRouteBuilder to serve Nitro on a different endpoint than the actual GraphQL endpoint.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapNitroApp("/graphql/ui");
});

This would make Nitro accessible via a Web Browser at the /graphql/ui endpoint.

GraphQLToolOptions

We can configure Nitro using GraphQLToolOptions.

Enable

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
Enable = false
});

This setting controls whether Nitro should be served or not.

GraphQLEndpoint

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
GraphQLEndpoint = "/my/graphql/endpoint"
});

This setting sets the GraphQL endpoint to use when creating new documents within Nitro.

UseBrowserUrlAsGraphQLEndpoint

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
UseBrowserUrlAsGraphQLEndpoint = true
});

If set to true the current Web Browser URL is treated as the GraphQL endpoint when creating new documents within Nitro.

Warning

GraphQLEndpoint takes precedence over this setting.

Document

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
Document = "{ __typename }"
});

This setting allows us to set a default GraphQL document that should be a placeholder for each new document created using Nitro.

HttpMethod

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
HttpMethod = DefaultHttpMethod.Get
});

This setting controls the default HTTP method used to execute GraphQL operations when creating new documents within Nitro.

HttpHeaders

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
HttpHeaders = new HeaderDictionary
{
{ "Content-Type", "application/json" }
}
});

This setting allows us to specify default HTTP Headers that will be added to each new document created using Nitro.

IncludeCookies

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
IncludeCookies = true
});

This setting specifies the default for including cookies in cross-origin when creating new documents within Nitro.

Title

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
Title = "My GraphQL explorer"
});

This setting controls the tab name, when Nitro is opened inside of a Web Browser.

DisableTelemetry

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
DisableTelemetry = true
});

This setting allows us to disable telemetry events.

GaTrackingId

C#
endpoints.MapNitroApp("/ui").WithOptions(new GraphQLToolOptions
{
GaTrackingId = "google-analytics-id"
});

This setting allows us to set a custom Google Analytics Id, which in turn allows us to gain insights into the usage of Nitro hosted as part of our GraphQL server.

The following information is collected:

NameDescription
deviceIdRandom string generated on a per-device basis
operatingSystemName of the operating system: Windows, macOS, Linux & Unknown
userAgentUser-Agent header
applicationTypeThe type of application: app (Electron) or middleware
applicationVersionVersion of Nitro

MapGraphQLHttp

We can call MapGraphQLHttp() on the IEndpointRouteBuilder to make our GraphQL server available via HTTP at a specific endpoint.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQLHttp("/graphql/http");
});

With the above configuration we could now issue HTTP GET / POST requests against the /graphql/http endpoint.

GraphQLHttpOptions

The HTTP endpoint can be configured using GraphQLHttpOptions.

C#
endpoints.MapGraphQLHttp("/graphql/http").WithOptions(new GraphQLHttpOptions
{
EnableGetRequests = false
});

The GraphQLHttpOptions are the same as the GraphQLServerOptions except that there are no Tool and EnableSchemaRequests properties.

Learn more about GraphQLServerOptions

MapGraphQLWebsocket

We can call MapGraphQLWebSocket() on the IEndpointRouteBuilder to make our GraphQL server available via WebSockets at a specific endpoint.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQLWebSocket("/graphql/ws");
});

With the above configuration we could now issue GraphQL subscription requests via WebSocket against the /graphql/ws endpoint.

MapGraphQLSchema

We can call MapGraphQLSchema() on the IEndpointRouteBuilder to make our GraphQL schema available at a specific endpoint.

C#
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQLSchema("/graphql/schema");
});

With the above configuration we could now download our schema.graphql file from the /graphql/schema endpoint.