Socket
Book a DemoInstallSign in
Socket

apollo-graphql-ws-link

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-graphql-ws-link

The graphql-ws apollo link recipe as package

latest
Source
npmnpm
Version
0.0.8
Version published
Weekly downloads
10
-50%
Maintainers
1
Weekly downloads
 
Created
Source

Just the graphql-ws "Client with Apollo Recipe" as package.

Install

  • run npm install apollo-graphql-ws-link
  • install peer dependencies npm run install @apollo/client@^3.4.0 graphql@^15.5.0 graphql-ws@^5.3.0

Usage

  • import WebSocketLink import { WebSocketLink } from 'apollo-graphql-ws-link'
  • create new WebSocketLink instance and pass graphql-ws client options new WebSocketLink({ url: 'ws://localhost:3000/graphql' })

Example:

import { ApolloClient, split, createHttpLink } from '@apollo/client'
import { WebSocketLink } from 'apollo-graphql-ws-link'

// create ws link - handling subscriptions
const wsLink = new WebSocketLink({
  url: `ws://localhost:3000/graphql`
})
// create http link - handling mutations and queries
const httpLink = createHttpLink({
  uri: `http://localhost:3000/graphql`
})

// Create client with links
// distinguish between subscriptions, queries and mutations
// ws link should handle subscriptions
// http link should handle mutations and queries
export const client = new ApolloClient({
  link: split(
    ({ query }) => {
      const mainDefinition = getMainDefinition(query)
      // If current operation is a "subscription" -> return true. So ws link handles it, else use http link
      if (mainDefinition.kind === 'OperationDefinition') {
        return mainDefinition.operation === 'subscription'
      } else {
        return false
      }
    },
    wsLink,
    httpLink
  )
})

Keywords

apollo

FAQs

Package last updated on 30 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts