Skip to content

✨ Golang server implementing the GraphQL over WebSocket protocol. Depends on gorilla/websocket and graphql-go/graphql

Notifications You must be signed in to change notification settings

0xc0392b/go-graphql-websocket-server

Repository files navigation

GraphQL WebSocket server for Golang

✨ Golang server implementing the GraphQL over WebSocket protocol

Features

  • Supports queries, mutations, and subscriptions
  • Tested with Apollo WebSocket client
  • Built-in authentication mechanism
  • Example projects for reference

GraphQL schema

The graphql-go/graphql package is the required package for defining a GraphQL schema. For example:

userResolver := func(p graphql.ResolveParams) (interface{}, error) {
	  return testUser{1, "test user", "test@user.com"}, nil
}

userType := graphql.NewObject(
	  graphql.ObjectConfig{
		  Name: "User",
		  Fields: graphql.Fields{
			  "id":    &graphql.Field{Type: graphql.Int},
			  "name":  &graphql.Field{Type: graphql.String},
			  "email": &graphql.Field{Type: graphql.String},
		  },
	  },
)

queryType := graphql.NewObject(
	  graphql.ObjectConfig{
		  Name: "Query",
		  Fields: graphql.Fields{
			  "getUser": &graphql.Field{
				  Type:        userType,
				  Resolve:     userResolver,
				  Description: "Get test user",
			  },
		  },
	  },
)

if schema, err := graphql.NewSchema(
	  graphql.SchemaConfig{
		  Query:        queryType,
	  },
); err != nil {
	  return nil, err
} else {
	  return &schema, nil
}

The protocol

The protocol is defined in enisdenjo/graphql-ws and consists of the following eight message types:

  1. ConnectionInit (client -> server)
  2. ConnectionAck (client <- server)
  3. Ping (client <-> server)
  4. Pong (client <-> server)
  5. Subscribe (client -> server)
  6. Next (client <- server)
  7. Error (client <- server)
  8. Complete (client <-> server)

Authentication

Example usage

About

✨ Golang server implementing the GraphQL over WebSocket protocol. Depends on gorilla/websocket and graphql-go/graphql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages