Giter VIP home page Giter VIP logo

apollo-graphql-subscriptions-example's Introduction

Apollo GraphQL Subscriptions Example

Simple application used to demonstrate minimalistic setup for an Apollo GraphQL Subscriptions architecture.

System Architecture

  .---------.                            .--------.
  | Client  |-.        GET /             | Web    |
  | Browser | | -----------------------> | Server |
  '---------' |                          '--------'
    '---------'
         |
         |                               .---------.
         |          GET /graphiql        | GraphQL |
         '-----------------------------> | Server  |
                    Websocket            '---------'
  • Browser starts by connecting to Web App and fetch available messages
  • Web page opens websocket tunnel to GraphQL server and subscribes to new messages
  • GraphQL mutations can then be submitted to the GraphQL server and new messages submitted to websocket clients for browser update

The master branch implementation uses filtering to decide if a message is to be sent to a given subscriber. The client app generates a random number to be used as the auth token. All requests placed to the GraphQL server will include the auth token in it. At the server, the auth token will be stored in the GraphQL context and eventually used by the filter function that validates if a message is intended to be sent to any given subscriber. In a real application, the GraphQL server would have to first validate the auth token against a token provider before proceding with any request.

Start Susbcription Client and Server apps

In a terminal do:

cd server-app
yarn start

In another terminal

cd client-app
yarn start

Test it

  1. Open a browser window with the client page
  2. Open another browser window with GraphiQL Subscriptions and press ►
  3. Open another browser window with GraphiQL Mutations and press ►

Your client page as well as the GraphiQL subscription page should now be displaying the new message.

Using CURL to exercise GraphQL Mutation:

curl -k -H "Content-Type: application/json" -X POST -d '{ "operationName": null, "query": "mutation AddMessage { addMessage(message: \"My CURL message\", broadcast: false) }", "variables": "{}" }' http://localhost:5060/graphql

Using the subscription Observable

Check the observable branch for the simplest subscription implementation.

Using the withApollo decorator

For an example using the withApollo decorator see the withApollo branch.

Using Apollo's susbcribeToMore

Checkout subscribeToMore branch for an example implementation using Apollo's subscribeToMore subscription callback function.

apollo-graphql-subscriptions-example's People

Contributors

alexicum avatar bmsantos avatar thisisamurray avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.