Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Go realtime with GraphQL Subscriptions

Go realtime with GraphQL Subscriptions

Meetup GraphQL Paris - @ eFounders
5 September 2017

Pierre Renaudin

September 05, 2017
Tweet

More Decks by Pierre Renaudin

Other Decks in Technology

Transcript

  1. Subscriptions Concept • Events based • Usage of GraphQL Schema

    • Connection • Subscription type Channel { id: ID! createdAt: Date! name: String! isFavorite: Boolean! } extend type Subscription { channelCreated: Channel! channelUpdated: Channel! channelDeleted: Channel! }
  2. Pubsub • Object to publish data • Use to aggregate

    events • Use to filtering before sending data • Can use Redis or InMemory or RabbitMQ
  3. - React Relay: Easy and straightforward - Apollo: Not tied

    up with React with special packages for React, Angular etc…
  4. - React Relay: Easy and straightforward - Apollo: Not tied

    up with React with special packages for React, Angular etc…
  5. import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws' 
 
 const

    networkInterface = createNetworkInterface({ uri: 'https://api.graph.cool/simple/v1/<project-id>' }) const wsClient = new SubscriptionClient('__SUBSCRIPTION_API_ENDPOINT__', { reconnect: true, }) const networkInterfaceWithSubscriptions = addGraphQLSubscriptions( networkInterface, wsClient ) const client = new ApolloClient({ networkInterface: networkInterfaceWithSubscriptions }) Apollo Setup
  6. Listen and receive data _subscribeToNewChannels = () => { this.props.channelsQuery.subscribeTo({

    document: gql` subscription { channelCreated { id createdAt name isFavorite } } `, updateQuery: (previous, { subscriptionData }) => { // Dispatch your subscription data :) } }) }
  7. Go further How to - https://www.howtographql.com/react-apollo/8-subscriptions/ RFC - https://github.com/facebook/graphql/blob/master/rfcs/Subscriptions.md Demo

    - https://blog.graph.cool/how-to-build-a-real-time-chat-with-graphql- subscriptions-and-apollo-d4004369b0d4