WARNING: not tested in heavy production use! That said if you are using this in production drop me a line to tell me how's it working for you. Maybe I can take this disclaimer off.
Transport compatible with Apollo subscription protocol.
Add graphql-dotnet MyGet feed to your nuget.config
For just the ASP.NET Core middleware:
dotnet add GraphQL.Server.Transports.AspNetCore
For WebSocket subscription protocol (depends on above)
dotnet add GraphQL.Server.Transports.WebSockets
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ChatSchema>();
// add http transport
services.AddGraphQLHttp();
// add websocket transport for ChatSchema
services.AddGraphQLWebSocket<ChatSchema>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// this is required for websockets support
app.UseWebSockets();
// add websocket for ChatSchema at default url /graphql
app.UseGraphQLWebSocket<ChatSchema>(new GraphQLWebSocketsOptions());
// add http for ChatSchema at default url /graphql
app.UseGraphQLHttp<ChatSchema>(new GraphQLHttpOptions());
// use graphql-playground at default url /ui/playground
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
}
Samples.Server shows a simple Chat style example of how subscription transport is used with GraphiQL integration.
Here are example queries to get started. Use three browser tabs or better yet windows to view the changes.
Query:
subscription MessageAddedByUser($id:String!) {
messageAddedByUser(id: $id) {
from { id displayName }
content
}
}
Variables:
{
"id": "1"
}
subscription MessageAdded {
messageAdded {
from { id displayName }
content
}
}
Query:
mutation AddMessage($message: MessageInputType!) {
addMessage(message: $message) {
from {
id
displayName
}
content
}
}
Variables:
{
"message": {
"content": "Message",
"fromId": "1"
}
}