🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis
Socket
Book a DemoInstallSign in
Socket

GraphQL.Server.Core

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

GraphQL.Server.Core

GraphQL Server Core

nugetNuGet
Version
5.2.2
Version published
Maintainers
1
Created
Source

GraphQL for .NET - Subscription Transport WebSockets

License

Join the chat at https://gitter.im/graphql-dotnet/graphql-dotnet

Run code tests Build artifacts Publish release CodeQL analysis

codecov Total alerts Language grade: C#

Activity Activity Activity

Size

GraphQL ASP.NET Core server on top of GraphQL.NET. Transport compatible with Apollo subscription protocol.

Provides the following packages:

PackageDownloadsNuGet Latest
GraphQL.Server.AllNugetNuget
GraphQL.Server.CoreNugetNuget
GraphQL.Server.Transports.AspNetCoreNugetNuget
GraphQL.Server.Transports.AspNetCore.NewtonsoftJsonNugetNuget
GraphQL.Server.Transports.AspNetCore.SystemTextJsonNugetNuget
GraphQL.Server.Transports.Subscriptions.AbstractionsNugetNuget
GraphQL.Server.Transports.Subscriptions.WebSockets
formerly known as GraphQL.Server.Transports.WebSockets
NugetNuget
GraphQL.Server.Ui.AltairNugetNuget
GraphQL.Server.Ui.PlaygroundNugetNuget
GraphQL.Server.Ui.GraphiQLNugetNuget
GraphQL.Server.Ui.VoyagerNugetNuget
GraphQL.Server.Authorization.AspNetCoreNugetNuget

You can install the latest stable versions via NuGet. Also you can get all preview versions from GitHub Packages. Note that GitHub requires authentication to consume the feed. See more information here.

Getting started

TL;DR Install GraphQL.Server.All meta package with all the packages you need to get started.

1. HTTP middleware for GraphQL

For just the HTTP middleware:

> dotnet add package GraphQL.Server.Transports.AspNetCore

The HTTP middleware needs an IGraphQLRequestDeserializer implementation.

.NET Core 3+:

> dotnet add package GraphQL.Server.Transports.AspNetCore.SystemTextJson

Legacy (prior to .NET Core 3):

> dotnet add package GraphQL.Server.Transports.AspNetCore.NewtonsoftJson

Or you can use your own IGraphQLRequestDeserializer implementation.

For more information on how to migrate from Newtonsoft.Json to System.Text.Json see this article.

2. WebSockets transport for subscriptions

For the WebSocket subscription protocol (depends on above) middleware:

> dotnet add package GraphQL.Server.Transports.Subscriptions.WebSockets

3. Authorization

For integration of GraphQL.NET validation subsystem into ASP.NET Core:

> dotnet add package GraphQL.Server.Authorization.AspNetCore

4. UI integration

For the UI middlewares:

> dotnet add package GraphQL.Server.Ui.Altair
> dotnet add package GraphQL.Server.Ui.GraphiQL
> dotnet add package GraphQL.Server.Ui.Playground
> dotnet add package GraphQL.Server.Ui.Voyager
public void Configure(IApplicationBuilder app)
{
    app.[Use|Map]GraphQLAltair();
    app.[Use|Map]GraphQLGraphiQL();
    app.[Use|Map]GraphQLPlayground();
    app.[Use|Map]GraphQLVoyager();
}

Also each middleware accepts options to configure its behavior and UI.

Configure

See the sample project's Startup.cs or StartupWithRouting.cs for full details. More information about ASP.NET Core routing here.

  • Without routing:
public void ConfigureServices(IServiceCollection services)
{
    // Add GraphQL services and configure options
    services
        .AddSingleton<IChat, Chat>()
        .AddSingleton<ChatSchema>();

    MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
        .AddServer(true)
        .ConfigureExecution(options =>
        {
            options.EnableMetrics = Environment.IsDevelopment();
            var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
            options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
        })
        // Add required services for GraphQL request/response de/serialization
        .AddSystemTextJson() // For .NET Core 3+
        .AddNewtonsoftJson() // For everything else
        .AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
        .AddWebSockets() // Add required services for web socket support
        .AddDataLoader() // Add required services for DataLoader support
        .AddGraphTypes(typeof(ChatSchema).Assembly) // Add all IGraphType implementors in assembly which ChatSchema exists 
}

public void Configure(IApplicationBuilder app)
{
    // this is required for websockets support
    app.UseWebSockets();

    // use websocket middleware for ChatSchema at default path /graphql
    app.UseGraphQLWebSockets<ChatSchema>();

    // use HTTP middleware for ChatSchema at default path /graphql
    app.UseGraphQL<ChatSchema>();

    // use GraphiQL middleware at default path /ui/graphiql with default options
    app.UseGraphQLGraphiQL();

    // use GraphQL Playground middleware at default path /ui/playground with default options
    app.UseGraphQLPlayground();

    // use Altair middleware at default path /ui/altair with default options
    app.UseGraphQLAltair();
    
    // use Voyager middleware at default path /ui/voyager with default options
    app.UseGraphQLVoyager();
}
  • With routing:
public void ConfigureServices(IServiceCollection services)
{
    // Add GraphQL services and configure options
    services
        .AddRouting()
        .AddSingleton<IChat, Chat>()
        .AddSingleton<ChatSchema>();

    MicrosoftDI.GraphQLBuilderExtensions.AddGraphQL(services)
        .AddServer(true)
        .ConfigureExecution(options =>
        {
            options.EnableMetrics = Environment.IsDevelopment();
            var logger = options.RequestServices.GetRequiredService<ILogger<Startup>>();
            options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occurred", ctx.OriginalException.Message);
        })
        // It is required when both GraphQL HTTP and GraphQL WebSockets middlewares are mapped to the same endpoint (by default 'graphql').
        .AddDefaultEndpointSelectorPolicy()
        // Add required services for GraphQL request/response de/serialization
        .AddSystemTextJson() // For .NET Core 3+
        .AddNewtonsoftJson() // For everything else
        .AddErrorInfoProvider(opt => opt.ExposeExceptionStackTrace = Environment.IsDevelopment())
        .AddWebSockets() // Add required services for web socket support
        .AddDataLoader() // Add required services for DataLoader support
        .AddGraphTypes(typeof(ChatSchema).Assembly); // Add all IGraphType implementors in assembly which ChatSchema exists 
}

public void Configure(IApplicationBuilder app)
{
    // this is required for websockets support
    app.UseWebSockets();

    // this is required for ASP.NET Core routing
    app.UseRouting();
    
    app.UseEndpoints(endpoints =>
    {
        // map websocket middleware for ChatSchema at default path /graphql
        endpoints.MapGraphQLWebSockets<ChatSchema>();

        // map HTTP middleware for ChatSchema at default path /graphql
        endpoints.MapGraphQL<ChatSchema, GraphQLHttpMiddlewareWithLogs<ChatSchema>>();

        // map GraphQL Playground middleware at default path /ui/playground with default options
        endpoints.MapGraphQLPlayground();

        // map GraphiQL middleware at default path /ui/graphiql with default options
        endpoints.MapGraphQLGraphiQL();

        // map Altair middleware at default path /ui/altair with default options
        endpoints.MapGraphQLAltair();

        // map Voyager middleware at default path /ui/voyager with default options
        endpoints.MapGraphQLVoyager();
}

UserContext and resolvers

UserContext of your resolver will be type of MessageHandlingContext. You can access the properties including your actual UserContext by using the Get<YourContextType>("UserContext") method. This will read the context from the properties of MessageHandlingContext. You can add any other properties as to the context in IOperationMessageListeners. See the sample for example of injecting ClaimsPrincipal.

Sample

Samples.Server shows a simple Chat example demonstrating the subscription transport. It supports various GraphQL client IDEs (by default opening GraphQL Playground).

Here are some example queries to get started. Use three browser tabs or better yet windows to view the changes.

Subscription 1

Query:

subscription MessageAddedByUser($id:String!) {
  messageAddedByUser(id: $id) {
    from { id displayName }
    content
  }
}

Variables:

{
  "id": "1"
}

Subscription 2

subscription MessageAdded {
  messageAdded {
    from { id displayName }
    content
  }
}

Mutation

Query:

mutation AddMessage($message: MessageInputType!) {
  addMessage(message: $message) {
    from {
      id
      displayName
    }
    content
  }
}

Variables:

{
  "message": {
    "content": "Message",
    "fromId": "1"
  }
}

API changes

You can see the changes in public APIs using fuget.org.

Keywords

GraphQL

FAQs

Package last updated on 19 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts