graphql-react-subscription v0.1.7
graphql-react-subscription
A GraphQL subscription client for graphql-react.
Inspired by graphql-subscriptions
Work in progress!
Koa server coming soon ...
API
Table of Contents
GraphQLSocket
GraphQLSocket manages the websocket connections.
Parameters
wsTimeoutnumber? specify the ws timeout duration. (optional, defaultInfinity)websocketObject? Websocket instance
Examples
import { GraphQLSocket } from 'react-graphql-subscription'
const graphqlSocket = new GraphQLSocket()subscribe
Handles subscriptions.
Parameters
urlstring The ws url.querystring The GraphQL subscription query.variablesstring query variables.keyIdnumber The key Id for the callback listener. (optional, default0)callbackfunction callback listener.
Consumer
A React component that gets the GraphQLSocket instance from the React context provided by the Provider.
Parameters
childrenConsumerRender Render function that receives a GraphQLSocket instance.
Examples
Subscribe component makes use of the GraphQLSocket instance passed via react context..
import { Consumer } from 'graphql-react'
const Subscribe = props => (
<Consumer>
{graphqlSocket => (
<GraphQLSubscribe graphqlSocket={graphqlSocket} {...props} />
)}
</Consumer>
)Returns ReactElement React virtual DOM element.
GraphQLSubscribe
Extends React.Component
A React component that requests subscription data.
Parameters
propsObject Component props.props.graphqlSocketGraphQLSocket GraphQLSocket instance.props.querystring GraphQL subscription query.props.urlstring Websocket server url.props.keyIdnumber? For identical duplicate subscribe components.props.subscribeOnMountboolean should componenet subscribe on mount.props.childrenRenderQuery Renders the subscription result.
MessageTypes
The WebSocket Client and server communication messages.
Properties
GQL_CONNECTION_INITstring Client to server string to initialize connection.GQL_CONNECTION_ACKstring The server response indicationg the websocket connection has been acknowledged.GQL_CONNECTION_ERRORstring A server connection error response.GQL_CONNECTION_KEEP_ALIVEstring Hearbeat of the ws subscription.GQL_CONNECTION_TERMINATEstring Client request to terminate the WebSocket connection.GQL_STARTstring Client request to start listening to a GraphQL subscription operation.GQL_DATAstring Server response with data.GQL_ERRORstring Server GraphQL Error response.GQL_COMPLETEstring Server response indicating GraphQL subscription operation is complete.GQL_STOPstring Client request stop listening to a subscription operation.
Provider
A React component provides a GraphQLSocket instance via React context for nested Consumer components to use.
Parameters
valueGraphQLSocket A GraphQLSocket instance.childrenReactNode A React node.
Examples
import { GraphQLSocket, Provider } from 'graphql-react-subscription'
const graphqlSocket = new GraphQLSocket()
const Page = () => (
<Provider value={graphqlSocket}>
Use Consumer or Subscribe components…
</Provider>
)Returns ReactElement React virtual DOM element.
Subscribe
A React component to manage a GraphQL subscription.
Parameters
propsObject Component props.props.urlstring websocket url.props.querystring GraphQL subscription query.props.subscribeOnMountboolean Should the subscription open when the component mounts. (optional, defaultfalse)props.keyIdnumber? Use if there are identical duplicate subscribe components.props.childrenSubscribeRender Renders the Subscribe status.
Examples
A subscription to listen for messages.
import { Subscribe } from 'graphql-react-subscription'
const Notification = () => (
<Subscribe
url={'ws://localhost:3000/subscribe'}
query={`subscription {
messages {
id
from
message
}
}`}
>
{({ subscribe, parseError, data, readyState }) => (
<>
{parseError && <strong>Error</strong>}
<ul>
{data.forEach(({ id, message, from }) => (
<li key={id}>
{from}: {message}
</li>
))}
</ul>
</>
)}
</Subscribe>
)Returns ReactElement React virtual DOM element.
SubscribeRender
Renders the status of a Subscription.
Type: function
Parameters
subscribefunction Connects a subscription on demand.parseErrorstring? Parse error message.dataobject? GraphQL response data.readyStatenumber? The ws readyState.
Examples
;({ subscribe, parseError, data, readyState }) => (
<aside>
<button onClick={subscribe}>connect</button>
{parseError && <strong>Error!</strong>}
{data && <h1>{data.user.name}</h1>}
</aside>
)Returns ReactElement React virtual DOM element.