@4c/graphql-relay-subscription 中文文档教程
graphql-relay-subscription
Usage
与 graphql-relay-js 中的 mutationWithClientId
一样,subscriptionWithClientId
使用单个输入和客户端订阅 ID 创建订阅。
import { parse, subscribe } from 'graphql';
import { subscriptionWithClientId } from 'graphql-relay-subscription';
/* ... */
const UpdateWidgetSubscription = subscriptionWithClientId({
name: 'UpdateWidgetSubscription',
inputFields: {
widgetId: { type: GraphQLString },
},
outputFields: {
widget: Widget,
},
subscribe: ({ widgetId }) =>
createSubscription(`widgets:${widgetId}:updated`),
});
const subscription = await subscribe(
schema,
parse(`
subscription ($input_0: UpdateWidgetSubscriptionInput!) {
updateWidget(input: $input_0) {
widget {
name
}
clientSubscriptionId
}
}
`),
null,
null,
{
input_0: {
widgetId: 'foo',
clientSubscriptionId: '0',
},
},
);
graphql-relay-subscription
Relay subscription helper for GraphQL.js.
Usage
As with mutationWithClientId
in graphql-relay-js, subscriptionWithClientId
creates subscriptions with single inputs and client subscription IDs.
import { parse, subscribe } from 'graphql';
import { subscriptionWithClientId } from 'graphql-relay-subscription';
/* ... */
const UpdateWidgetSubscription = subscriptionWithClientId({
name: 'UpdateWidgetSubscription',
inputFields: {
widgetId: { type: GraphQLString },
},
outputFields: {
widget: Widget,
},
subscribe: ({ widgetId }) =>
createSubscription(`widgets:${widgetId}:updated`),
});
const subscription = await subscribe(
schema,
parse(`
subscription ($input_0: UpdateWidgetSubscriptionInput!) {
updateWidget(input: $input_0) {
widget {
name
}
clientSubscriptionId
}
}
`),
null,
null,
{
input_0: {
widgetId: 'foo',
clientSubscriptionId: '0',
},
},
);