Code Monkey home page Code Monkey logo

server's Introduction

GraphQL for .NET - Subscription Transport WebSockets

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.

Build status

Transport compatible with Apollo subscription protocol.

Getting started

For just the HTTP middleware:

dotnet add package GraphQL.Server.Transports.AspNetCore

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

dotnet add package GraphQL.Server.Transports.WebSockets

For the UI middleware/s:

dotnet add package GraphQL.Server.Ui.GraphiQL
dotnet add package GraphQL.Server.Ui.Playground
dotnet add package GraphQL.Server.Ui.Voyager

Configure

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ChatSchema>();

    // Add GraphQL services and configure options
    services.AddGraphQL(options =>
    {
        options.EnableMetrics = true;
        options.ExposeExceptions = this.Environment.IsDevelopment();
    })
    .AddWebSockets() // Add required services for web socket support
    .AddDataLoader(); // Add required services for DataLoader support
}

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

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

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

    // use graphiQL middleware at default url /graphiql
    app.UseGraphiQLServer(new GraphiQLOptions());

    // use graphql-playground middleware at default url /ui/playground
    app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
    
    // use voyager middleware at default url /ui/voyager
    app.UseGraphQLVoyager(new GraphQLVoyagerOptions());
}

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 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.

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"
  }
}

server's People

Contributors

pekkah avatar rehansaeed avatar johnrutherford avatar benmccallum avatar joemcbride avatar dsilence avatar snys98 avatar sahb1239 avatar rose-a avatar andrewtyped avatar arturmoreirabp avatar cito avatar sumboxcar182 avatar eldar-ahmadov avatar lopes-felipe avatar glennblock avatar sungam3r avatar joshkel avatar kamsar avatar phillip-haydon avatar simoncropp avatar kenrim626 avatar superslowloris avatar wakemaster39 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.