npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-phoenix-websocket

v0.6.2

Published

Apollo networkInterface using Phoenix websockets

Downloads

17

Readme

APW - Apollo Phoenix Websocket

help maintain this lib

Apollo is a feature rich GQL client, APW implements an Apollo GraphQL Network Layer for it over Phoenix Channels allowing you to re-use a single bidirectional connection for executing your queries and mutations, the backend can send new data via subscriptions, and the Apollo client can update its internal store and update your views accordingly.

Since version 0.6.0, all Apollo operations are supported: queries, mutations, watchQueries (pooling) and subscriptions.

Using the Apollo Client, queries and mutations resolve to promises, and watchQueries and subscriptions resolve to observables.

See the Apollo client documentation for more info on how to invoke your GQL backend.

Installation

npm install --save apollo-phoenix-websocket

Usage

Just import the createNetworkInterface from APW and use it to create an ApolloClient.

The networkInterface function takes an options object, the only required property is uri which specifies your endpoint websocket address.

import ApolloClient from 'apollo-client'
import {createNetworkInterface} from 'apollo-phoenix-websocket'

// Nothing to configure if you are using an Absinthe backend
// Otherwise take a look at the Options section.
const networkInterface = createNetworkInterface({uri: 'ws://localhost:4000/socket'})

const apollo = new ApolloClient({networkInterface})

Options

Most likely, (as you are looking for a phoenix-websocket transport) you might be using the Absinthe library to implement your Elixir GQL server. APW is configured by default to work out of the box with an Absinthe backend.

But if need araises, you can supply some advanced options to customize how it works. Here's is a commented example of the options that you can set for APW, with their respective default values:

createNetworkInterface({

  // The websockets endpoint to connect to, like wss://example.com:4000/socket
  uri: WS_URI,

  // how to send queries and mutations
  channel: {
    topic: '__absinthe__:control',
    event: 'doc',
  },

  // for using websocket subscriptions
  subscription: (subscriptionResponse) => ({
    topic: subscriptionResponse.subscriptionId,
    event: 'subscription:data',

    // extract the data from the event payload
    map: payload => payload.result.data,

    // what to do when unsubscribing
    off: controlChannel => {
      controlChannel.push('unsubscribe', {
        subscriptionId: subscriptionResponse.subscriptionId
      })
    }
  }),

  // If you want to reuse an existing Phoenix Socket, just provide a function
  // for APW to get it. By default, it will use the Phoenix Socket module.
  Socket: options => new Socket(options),
})

Middlewares

You can use middlewares with use just like with the standard apollo network interface. For example, a middleware can set authorization token on every request.

networkInterface.use([{
  applyMiddleware({request, options}, next) {
    // Here you can modify the interface options, for example
    // you can change the socket/channel that will handle the request
    // For example for a channel expecting authenticated queries
    options.channel.topic = "gql:restricted"
    options.channel.params = {...paramsForTopicJoin}

    // Or Modify the request
    request.variables = {name: 'Luke'}
    
    // Or Just add authorization token
    request.context = {authorization: 'jwt_token'}

    next()
  }
}])

Afterware

You can use afterwares with useAfter just like the standard apollo network interface. An example use-case is for error handling:

networkInterface.useAfter([{
  applyAfterware({response, options}, next) {
    // throw an error that will trigger your error handler
    if (response.error) {
      throw new Error(response.error)
    }
    next();
  }
}])

Absinthe backend

Absinthe is an amazing project (kudos to @benwilson512 et al.). It's actually very simple to create a GQL backend with it.

Take a look at the following places for more information:

Made with love <3

If you want to provide feedback or even better if you want to contribute some code feel free to open a new issue. Possible thanks to the awesome work of our contributors.