Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content edit for subscriptions article #4880

Merged
merged 6 commits into from
Feb 9, 2021
Merged

Conversation

StephenBarlow
Copy link
Contributor

No description provided.


import {ExpansionPanel} from 'gatsby-theme-apollo-docs';

> **Subscriptions are not supported in [Apollo Federation](https://www.apollographql.com/docs/federation/).**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 'not yet'?


> **Subscriptions are not supported in [Apollo Federation](https://www.apollographql.com/docs/federation/).**

**Subscriptions** are GraphQL operations that maintain an active connection between client and server. This enables your server to _push_ updates to a subscription's result as they occur, instead of requiring your client to _poll_ for updates.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think maintaining an active connection is what defines subscriptions. What differentiates them from queries is that they are triggered by events, and they generate a new result for each such event. Although delivering results as soon as they are generated works best over a persistent connection, there is nothing stopping you from implementing subscriptions on top of polling.


## Schema definition

You define your schema's supported subscriptions as fields of the `Subscription` type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be a distinction we care about, but in the spec the fields defined on Subscription wouldn't themselves be called subscriptions, similar to how fields on Query aren't queries. These are root fields that get used in subscription (respectively query) operations. I realize referring to these as root fields makes this overly technical, and because subscription operations can only include a single root field there is a one to one correspondence. So not sure if we need to be struct about terminology here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah totally, I'll work on this. Also I wasn't aware sub operations can only have one root field! Makes a lot of sense.

};
```

The `subscribe` function must return an object of type `AsyncIterable`, part of Apollo Server's pub/sub API (described below).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncIterable isn't really part of Apollo Server's API, but defined in ES2018. Maybe we could mention that it's a standard JavaScript API that is used to return a stream of asynchronous results, and that that is the only required API surface, but that Apollo Server includes a PubSub class as a convenience to implement an event bus.

* The first parameter is the name of the event label you're publishing to, as a string.
* _You don't need to register a label name before publishing to it._
* The second parameter is the payload associated with the event.
* _The payload's structure should match the structure that clients expect from the associated subscription's return type._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to match the structure client's expect though. It's the task of the resolver to map between events and results (what is described as MapSourceToResponseEvent in the spec).

* The second parameter is the payload associated with the event.
* _The payload's structure should match the structure that clients expect from the associated subscription's return type._

When working with GraphQL subscriptions, you `publish` an event whenever a subscription's return value should be updated. One of the most common causes of such an update is a mutation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add that these events don't have to be triggered as a result of GraphQL operations, but can also connect to an existing event bus and be triggered by other systems.

@StephenBarlow StephenBarlow force-pushed the sb/subscriptions-edits branch from 1b0b122 to 3528236 Compare February 9, 2021 02:18
@StephenBarlow StephenBarlow merged commit 50902bd into main Feb 9, 2021
@StephenBarlow StephenBarlow deleted the sb/subscriptions-edits branch February 9, 2021 02:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants