Skip to content

Commit

Permalink
Add documentation for Subscriptions
Browse files Browse the repository at this point in the history
Summary:
Also included an example of adding a new node to a connection :-)
Closes #1772

Reviewed By: kassens

Differential Revision: D5078598

Pulled By: josephsavona

fbshipit-source-id: 235c69e07c70ae629d2dc1193b23565ce2c7e2b3
  • Loading branch information
josephsavona authored and facebook-github-bot committed May 17, 2017
1 parent 7880272 commit 752856f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/modern/Mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Mutations
layout: docs
category: Relay Modern
permalink: docs/mutations.html
next: babel-plugin-relay
next: subscriptions
---

Relay exposes the following APIs to perform mutations.
Expand Down
115 changes: 115 additions & 0 deletions docs/modern/Subscriptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
id: subscriptions
title: Subscriptions
layout: docs
category: Relay Modern
permalink: docs/subscriptions.html
next: babel-plugin-relay
---

Relay exposes the following APIs to create subscriptions.

```javascript
const {requestSubscription} = require('react-relay');

type Variables = {[name: string]: any};

requestSubscription(
environment: Environment,
config: {
subscription: GraphQLTaggedNode,
variables: Variables,
onCompleted?: ?() => void,
onError?: ?(error: Error) => void,
onNext?: ?(response: ?Object) => void,
updater?: ?(store: RecordSourceSelectorProxy) => void,
},
);
```
Now let's take a closer look at the `config`:
* `subscription`: the `graphql` tagged subscription query.
* `variables`: an object that contains the variables needed for the subscription.
* `onCompleted`: a callback function executed when the subscription is closed by
the peer without error.
* `onError`: a callback function executed when Relay or the server encounters an
error processing the subscription.
* `onNext`: a callback function executed each time a response is received from
the server, with the raw GraphQL response payload.
* `updater`: an optional function that can supply custom logic for updating the
in-memory Relay store based on the server response.
## Example
In a simple subscription, you only need `subscription` and `variables`. This is
appropriate when you are only changing the properties of existing records that
can be identified by their `id`:
```javascript
const {
requestSubscription,
graphql,
} = require('react-relay');

const subscription = graphql`
subscription MarkReadNotificationSubscription(
$storyID: ID!
) {
markReadNotification(storyID: $storyID) {
notification {
seenState
}
}
}
`;

const variables = {
storyID,
};

requestSubscription(
yourEnvironment, // see Environment docs
{
subscription,
variables,
// optional but recommended:
onComplete: () => {/* server closed the subscription */},
onError: error => console.error(error),
}
);
```
# Updating the client on each response
For more complex use-cases, you may wish to perform custom logic to update
Relay's in-memory cache when each subscription response is received. To do so,
pass an `updater` function:
```javascript
const {ConnectionHandler} = require('relay-runtime');

requestSubscription(
environment,

This comment has been minimized.

Copy link
@jamesone

jamesone May 17, 2017

I'd love to see the environment config that's been setup if it's available somewhere?

This comment has been minimized.

This comment has been minimized.

Copy link
@josephsavona

josephsavona May 17, 2017

Author Contributor

There's an example of how to set up the network layer too at https://facebook.github.io/relay/docs/network-layer.html#network-layer

This comment has been minimized.

Copy link
@jamesone

jamesone May 17, 2017

I'm pretty sure I used these the other day but was stopped in my tracks because the environment needed a custom network layer to handle subscriptions. I will take a look later and report back here!

This comment has been minimized.

Copy link
@josephsavona

josephsavona May 17, 2017

Author Contributor

Ah I see. Yes, NetworkLayer.create() accepts a second function parameter for sending subscriptions. It's similar to the fetch function but accepts callbacks for completion/error/payload. For now best bet is to look at NetworkLayer.js to see the signature - I'm on mobile so don't have the link handy.

This comment has been minimized.

Copy link
@jamesone

jamesone May 18, 2017

Yeah I did see that :) I'd still be keen to see an example usage 👍

This comment has been minimized.

Copy link
@josephsavona

josephsavona May 18, 2017

Author Contributor

The type signature should make the behavior pretty clear, but yeah we should add docs.

{
subscription,
variables,
updater: store => {
// Get the notification
const rootField = store.getRootField('markReadNotification');
const notification = rootField.getLinkedRecord('notification');
// Add it to a connection
const viewer = store.getRoot().getLinkedRecord('viewer');
const notifications =
ConnectionHandler.getConnection(viewer, 'notifications');
const edge = ConnectionHandler.createEdge(
store,
notifications,
notification,
'<TypeOfNotificationsEdge>',
);
ConnectionHandler.insertEdgeAfter(notifications, edge);
},
},
);
```

0 comments on commit 752856f

Please sign in to comment.