• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An example of GraphQL akka-http server with stream-based subscriptions

An example of GraphQL akka-http server with SSE-based subscriptions powered by sangria. It features:

  • Implementation based on CQRS (Command Query Responsibility Segregation) + event-sourcing
  • Server Sent Events subscriptions based on akka-streams and akka-sse
  • Implemented with stream-based subscriptions
  • Optimistic concurrency control for mutation queries

This example is pretty rough around the edges at the moment. Subscriptions support in GraphQL and well as sangria is still in experimental phase, so expect big changes and improvements in near future (especially around the way subscriptions are implemented). This also means that your feedback is important and very welcome ;)

You can find a WebSocket example in a separate branch.

How to start

The only prerequisites are SBT and Java 8. After you cloned the project, you need to run an application with SBT:

sbt ~reStart

This uses an sbt-revolver plugin. It will automatically compile and restart the server on every change.

After you started the server, you can point your browser to following URLs:

High-level overview

High-level picture looks like this:

Event-stream based subscriptions

I also described this approach in much more detail here:

Event-stream based GraphQL subscriptions

Please not that this particular example is intended to demonstrate different concepts (in particular GraphQL subscriptions), so it does not have any persistence. This means that MemoryEventStore and views keep all of the data in memory.

Client-server interaction

If client makes a subscription query, then server will respond with text/event-stream. For any other query type server will respond with normal JSON reponse.

Here is an example of interation between client and server where one client subscribes to an event and another client makes a mutation that produces this type of events:

Client-server interaction

Since EventSource always makes a GET request to a SSE endpoint, I added support for GET method on /graphql endpoint. It takes a GraphQL query as a query parameter.

Optimistic concurrency control

I find optimistic concurrency control pretty important for this kind of architecture. So I decided to include it in this example application, even though it adds a bit of complexity.

This pattern helps clients to detect conflicts when they are doing mutations. Imagine that two clients would like to change an article at the same time with GraphQL query like this:

mutation NewText {
  changeArticleText(id: "123", version: 5, text: "bar")
}

Each client will first read an article and then make some decision based on the returned result. Let's say that they both have decided to update the article in different ways. In order to perform the mutation they both need to tell server which version of article they based their decision on. Thanks to version server is able to detect a conflict and only successfully perform one mutation, rejecting the other one:

Optimistic concurrency control

GraphQL subscription semantics

In this particular application a top-level fields on the Subscription type represent all available event types. Client only gets events that it subscribed to. For example, given following subscription:

subscription NewAuthors {
  authorCreated {
    id
    version
    firstName
    lastName
  }
}

and mutations:

mutation {
  createAuthor(firstName: "John", lastName: "Doe") {
    id, version
  }
}

mutation {
  changeAuthorName(
    id: "b4dd3963-3fdd-4d7a-8105-c33dfc7ddffc", 
    version: 1, 
    firstName: "Jane", 
    lastName: "Doe") {id, version}
}

mutation {
  deleteAuthor(id: "b4dd3963-3fdd-4d7a-8105-c33dfc7ddffc", version: 2) {
    firstName
    lastName
  }
}

client will only get following event:

{
  "data": {
    "authorCreated": {
      "id": "b4dd3963-3fdd-4d7a-8105-c33dfc7ddffc",
      "version": 1,
      "firstName": "John",
      "lastName": "Doe"
    }
  }
}

Multi-field subscriptions

Clint can also subscribe to multiple events. For example:

subscription {
  authorCreated {
    id
    version
    firstName
    lastName
  }
  
  authorDeleted {
    id
    version
  }
}

Given the 3 mutation queries mentioned above, client will get following events:

{
  "data": {
    "authorCreated": {
      "id": "b4dd3963-3fdd-4d7a-8105-c33dfc7ddffc",
      "version": 1,
      "firstName": "John",
      "lastName": "Doe"
    }
  }
}

{
  "data": {
    "authorDeleted": {
      "id": "b4dd3963-3fdd-4d7a-8105-c33dfc7ddffc",
      "version": 3
    }
  }
}

As you can see, at any given time client can only get 1 event. All other subscription fields would be null.

I also would like to mention, that this semantics is pretty arbitrary. GraphQL specification does not define semantics for subscription queries at the moment - it's still under active discussion. So you can treat semantics, that is defined in this example application, more as an experiment rather that a recommended way of doing things ;)

Feedback

Feedback is very welcome in any form :) Feel free to make PRs, post issues or join the chat.

More Repositories

1

sangria

Scala GraphQL implementation
Scala
1,962
star
2

sangria-akka-http-example

An example GraphQL server written with akka-http, circe and sangria
Scala
241
star
3

sangria-relay

Sangria Relay Support
Scala
90
star
4

sangria-playground

An example of GraphQL server written with Play and sangria
JavaScript
87
star
5

sangria-slowlog

Sangria middleware to log slow GraphQL queries (with OpenTracing and apollo-tracing extension)
Scala
43
star
6

macro-visit

A macro-based generic visitor generator
Scala
24
star
7

sangria-circe

Sangria circe marshalling
Scala
24
star
8

sangria-play-json

Sangria play-json marshalling
Scala
20
star
9

sangria-federated

Running a sangria server as federated
Scala
18
star
10

sangria-relay-playground

An example of GraphQL server supporting Relay written with Play and sangria
HTML
17
star
11

sangria-monix

Sangria monix integration
Scala
10
star
12

sangria-akka-streams

Sangria akka-streams integration
Scala
10
star
13

sangria-graphql.github.io

Project website
CSS
6
star
14

sangria-msgpack

Sangria MessagePack marshalling.
Scala
5
star
15

sangria-spray-json

Sangria spray-json marshalling
Scala
3
star
16

sangria-ion

Sangria Amazon Ion marshalling
Scala
3
star
17

sangria-marshalling-api

Sangria Marshalling API
Scala
3
star
18

sangria-streaming-api

Sangria Streaming API
Scala
2
star
19

sangria-argonaut

Sangria argonaut marshalling
Scala
2
star
20

sangria-jackson

Sangria Jackson Marshalling (Without Json4s)
Scala
2
star
21

sangria-logo

The Logo
2
star
22

sangria-json4s-jackson

Sangria json4s-jackson marshalling
Scala
1
star
23

sangria-rxscala

Sangria RxScala integration
Scala
1
star
24

sangria-json4s-native

Sangria json4s-native marshalling
Scala
1
star