An open API service indexing awesome lists of open source software.

https://github.com/launchscout/react-apollo-subscribed-query

Wraps a react component a query and a subscription that updates it
https://github.com/launchscout/react-apollo-subscribed-query

apollo apollographql graphql react

Last synced: 5 months ago
JSON representation

Wraps a react component a query and a subscription that updates it

Awesome Lists containing this project

README

          

# react-apollo-subscribed-query

This module exports a single function that you can use like so
to wrap a React component with a graphql query and a subscription
that will update its results.

## Example

```javascript
import React, { Component } from 'react';
import {
ApolloClient,
gql,
graphql,
ApolloProvider,
} from 'react-apollo';
import ProcessOrder from './ProcessOrder';
import graphqlSubscribedQuery from 'react-apollo-subscribed-query';

class OrderDetail extends Component {

render() {
const {match} = this.props;
const {loading, error, order} = this.props.data;
if (loading) {
return

Loading ...

;
}
if (error) {
return

{error.message}

;
}
return (


Purchase Order Number

{order.purchaseOrderNumber}



Status

{order.status}



Production Quantity

{order.productionQuantity}



Test Quantity

{order.testQuantity}



Item Schema

{order.itemSchema}




);
}
}

export const orderQuery = gql`
query OrderQuery($orderId: ID) {
order(orderId: $orderId) {
id
purchaseOrderNumber
status
testQuantity
productionQuantity
itemSchema
}
}
`;

export const orderSubscription = gql`
subscription subscribeToOrders($orderId:ID!) {
orderStatusChange(orderId: $orderId) {
id
status
itemSchema
testQuantity
productionQuantity
purchaseOrderNumber
}
}
`;
const OrderDetailWithQuery = graphqlSubscribedQuery({
options: ({ match }) => ({ variables: { orderId: match.params.orderId } }),
query: orderQuery,
subscription: orderSubscription
})(OrderDetail);
export default OrderDetailWithQuery;
```

Caveats:

* query and subscription must take the same variables
* subscription data will replace query data
* This is really just a spike. It appears to work but there are no tests