All Projects → vic → apollo-phoenix-websocket

vic / apollo-phoenix-websocket

Licence: Apache-2.0 license
An Apollo networkInterface for executing GraphQL queries via Phoenix Channels

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to apollo-phoenix-websocket

Absinthe
The GraphQL toolkit for Elixir
Stars: ✭ 3,805 (+4081.32%)
Mutual labels:  apollo-client, absinthe
now-course
Proyecto para el curso de Now.sh en Platzi
Stars: ✭ 19 (-79.12%)
Mutual labels:  apollo-client, apollographql-subscriptions
pokedex
📕 DIY full-stack workshop teaching you Elixir, Absinthe GraphQL, React and Relay
Stars: ✭ 17 (-81.32%)
Mutual labels:  absinthe, absinthe-phoenix
fullstack-apollo-subscription-example
A minimal Apollo Server 2 with Apollo Client 2 with Subscriptions application.
Stars: ✭ 72 (-20.88%)
Mutual labels:  apollo-client, apollographql-subscriptions
opencensus absinthe
Opencensus integration with Absinthe
Stars: ✭ 21 (-76.92%)
Mutual labels:  absinthe
Helios
A Chrome Dev Tool for tracking state in Apollo and Vue.js.
Stars: ✭ 22 (-75.82%)
Mutual labels:  apollo-client
nap
[Deprecated] NextJS + Apollo + PassportJS
Stars: ✭ 52 (-42.86%)
Mutual labels:  apollo-client
matters-web
Website of Matters.News, built with Next.js.
Stars: ✭ 70 (-23.08%)
Mutual labels:  apollo-client
chanyeong
👨‍💻 chanyeong's portfolio and blog webpage
Stars: ✭ 39 (-57.14%)
Mutual labels:  apollo-client
react-native-appsync-s3
React Native app for image uploads to S3 and storing their records in Amazon DynamoDB using AWS Amplify and AppSync SDK
Stars: ✭ 18 (-80.22%)
Mutual labels:  apollo-client
devto-monorepo
Source code for the Dev.to article - Next.js, Apollo Client and Server on a single Express app
Stars: ✭ 33 (-63.74%)
Mutual labels:  apollo-client
uai shot
A multiplayer ship game built with Elixir, Phoenix Framework and Phaser. 🚀
Stars: ✭ 53 (-41.76%)
Mutual labels:  phoenix-channels
phoenix-channel-client
A Phoenix Channels client library for Elixir.
Stars: ✭ 20 (-78.02%)
Mutual labels:  phoenix-channels
react-native-feed
News feed built with react-native, apollo-client and graphql & powered by postgraphql
Stars: ✭ 99 (+8.79%)
Mutual labels:  apollo-client
apollo-cache-instorage
Apollo Cache implementation that facilitates locally storing resources
Stars: ✭ 98 (+7.69%)
Mutual labels:  apollo-client
boilerplate
Boilerplate for @prisma-cms
Stars: ✭ 22 (-75.82%)
Mutual labels:  apollo-client
batch loader
⚡ Powerful tool for avoiding N+1 DB or HTTP queries
Stars: ✭ 18 (-80.22%)
Mutual labels:  absinthe
lifemanager
⏱ 한 일을 기록하면 시각화 해서 보여주는 웹 앱⏱
Stars: ✭ 85 (-6.59%)
Mutual labels:  apollo-client
apollo-graphql-full-stack
Full-stack Apollo GraphQL app using React and Node JS
Stars: ✭ 31 (-65.93%)
Mutual labels:  apollo-client
pokedex-react
A Pokedex App using and teaching Apollo and React
Stars: ✭ 47 (-48.35%)
Mutual labels:  apollo-client

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 createNetworkInterface 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 ws://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
    options.params = {
      ...options.params,
      auth_token: 'my-secret-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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].