Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

(core) - Expose client.subscription shortcut method (#838)

* Export client.subscription shortcut

* Update packages/core/src/client.ts

* Update docs/api/core.md

* Apply suggestions from code review

Co-authored-by: Phil Plückthun <phil@kitten.sh>

Daniel K 98b6edc2 0a399aea

Changed files
+47 -5
.changeset
docs
advanced
api
packages
core
src
+5
.changeset/hot-dryers-drive.md
···
+
---
+
'@urql/core': minor
+
---
+
+
Expose a `client.subscription`shortcut method, similar to `client.query` and `client.mutation`
+27 -4
docs/advanced/subscriptions.md
···
import { Client, defaultExchanges, subscriptionExchange } from 'urql';
import { SubscriptionClient } from 'subscriptions-transport-ws';
-
const subscriptionClient = new SubscriptionClient(
-
'wss://localhost/graphql',
-
{ reconnect: true }
-
);
+
const subscriptionClient = new SubscriptionClient('wss://localhost/graphql', { reconnect: true });
const client = new Client({
url: '/graphql',
···
the `handleSubscription` function. This works over time, so as
new messages come in, we will append them to the list of previous
messages.
+
+
## One-off Subscriptions
+
+
Whe you're using subscriptions directly without `urql`'s framework bindings, you can use the `Client`'s `subscription` method for one-off subscriptions. This method is similar to the ones for mutations and subscriptions [that we've seen before on the "Core Package" page.](../concepts/core-package.md#one-off-queries-and-mutations)
+
+
This method will always [returns a Wonka stream](../concepts/stream-patterns.md#the-wonka-library) and doesn't have a `.toPromise()` shortcut method, since promises won't return the multiple values that a subscription may deliver. Let's convert the above example to one without framework code, as we may use subscriptions in a Node.js environment.
+
+
```js
+
import { pipe, subscribe } from 'wonka';
+
+
const newMessages = `
+
subscription MessageSub {
+
newMessages {
+
id
+
from
+
text
+
}
+
}
+
`;
+
+
const { unsubscribe } = pipe(
+
client.subscription(MessageSub),
+
subscribe(result => {
+
console.log(result); // { data: ... }
+
})
+
);
+7 -1
docs/api/core.md
···
### client.executeSubscription
This is functionally the same as `client.executeQuery`, but creates operations for subscriptions
-
instead, with `operationName` set to `'mutation'`.
+
instead, with `operationName` set to `'subscription'`.
### client.executeMutation
···
[Read more about how to use this API on the "Core Package"
page.](../concepts/core-package.md#one-off-queries-and-mutations)
+
+
### client.subscription
+
+
This is similar to [`client.query`](#clientquery), but does not provide a `toPromise()` helper method on the streams it returns.
+
+
[Read more about how to use this API on the "Subscriptions" page.](../advanced/subscriptions.md)
#### client.reexecuteOperation
+8
packages/core/src/client.ts
···
return response$;
};
+
subscription<Data = any, Variables extends object = {}>(
+
query: DocumentNode | string,
+
variables?: Variables,
+
context?: Partial<OperationContext>
+
): Source<OperationResult<Data>> {
+
return this.executeSubscription(createRequest(query, variables), context);
+
}
+
executeSubscription = (
query: GraphQLRequest,
opts?: Partial<OperationContext>