Skip to content

Commit

Permalink
Finished documenting subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Klish committed Sep 30, 2021
1 parent 7016d87 commit d60c156
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
50 changes: 50 additions & 0 deletions _data/snippets/v6/subscription-custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
files:
- ChatBot.java
- ChatBotCreateHook.java

ChatBot.java: |+2
```java
@Include
@Data
@LifeCycleHookBinding(
hook = ChatBotCreateHook.class,
operation = LifeCycleHookBinding.Operation.CREATE,
phase = LifeCycleHookBinding.TransactionPhase.POSTCOMMIT
)
public class ChatBot {
@Id
long id;
String name;
}
```
ChatBotCreateHook.java: |+2
```java
@Data
public class ChatBotCreateHook implements LifeCycleHook<ChatBot> {
@Inject
ConnectionFactory connectionFactory;
@Override
public void execute(
LifeCycleHookBinding.Operation operation,
LifeCycleHookBinding.TransactionPhase phase,
ChatBot bot,
RequestScope requestScope,
Optional<ChangeSpec> changes) {
NotifyTopicLifeCycleHook<Chat> publisher = new NotifyTopicLifeCycleHook<>(
connectionFactory,
new ObjectMapper(),
JMSContext::createProducer
);
publisher.publish(new Chat(1, "Hello!"), CHAT);
publisher.publish(new Chat(2, "How is your day?"), CHAT);
publisher.publish(new Chat(3, "My name is " + bot.getName()), CHAT);
}
}
```
40 changes: 30 additions & 10 deletions pages/guide/v6/11.1-subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: GraphQL Subscriptions
version: 6
---

# 1-Minute Overview
# 5-Minute Overview

We'll be leveraging the [elide-spring-boot-example][example-project] project to illustrate how to use subscriptions.

Expand Down Expand Up @@ -45,7 +45,7 @@ class Book {
}
```

Whenever a model is manipulated (created, deleted, or updated), elide will post a JSON serialized model to a JMS topic for that operation. Only the fields decorated with `@SubscriptionField` will be serialized (and hence available to be consumed in the subscription). It is also possible to define [custom operations](#custom) that are triggered by your service business logic.
Whenever a model is manipulated (created, deleted, or updated), elide will post a JSON serialized model to a JMS topic for that operation. Only the fields decorated with `@SubscriptionField` will be serialized (and hence available to be consumed in the subscription). It is also possible to define [custom operations](#custom-subscriptions) that are triggered by your service business logic.


## Run Queries
Expand Down Expand Up @@ -126,14 +126,6 @@ Elide honors [ReadPermission annotations](/pages/guide/v{{ page.version }}/03-se

See the section on [Authentication](#authentication) for details on how to build an Elide user principal.

# Custom Subscriptions

While Elide makes it easy to subscribe to model manipulations (create, update, and delete), it is also possible to add a subscription topic for another event tied to your business logic.


TBD


# Configuration

## JMS Message Broker
Expand Down Expand Up @@ -166,6 +158,34 @@ It is possible to override some of the default settings for messages published t

{% include code_example example="subscription-message-settings" %}

# Custom Subscriptions

While Elide makes it easy to subscribe to model manipulations (create, update, and delete), it is also possible to add a subscription topic for another event tied to your business logic. A custom subscription is simply an Elide model annotated with the `@Subscription` annotation that explicitly sets the list of operations to empty:

```java
@Include

//This is a custom subscription
@Subscription(operations = {})
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Chat {


@Id
long id;

@SubscriptionField
String message;
}
```

To publish to your subscription, you can create a lifecycle hook on another model that posts `Chat` messages.


{% include code_example example="subscription-custom" %}

# Recommendations

Even though the [example project][example-project] runs GraphQL queries, mutations, and subscriptions in the same service, it is highly recommended that subscriptions run as a separate service. Because websockets are long lived and stateful, they impose different resource constraints and performance characteristics from queries and mutations.
Expand Down

0 comments on commit d60c156

Please sign in to comment.