Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
apollo-client-ws
Advanced tools
GraphQL WebSocket Network Link for Apollo Client
This is a GraphQL
WebSocket
based ApolloLink
layer for the JavaScript GraphQL client library
Apollo Client.
It was developed for and is intended to be used with the HAPI server
framework and its seamless WebSocket protocol integration module
HAPI-Plugin-WebSocket,
although it could be used with any server speaking GraphQL over a
framed WebSocket communication.
Apollo-Client-WS has deferred connection establishment, connection
keepalive support and can reconnect to the server automatically.
Additionally, beside the GraphQL request/response messages, it also
allows the application to send/receive arbitrary messages over the
WebSocket connection, too.
$ npm install graphql-tag apollo-client apollo-client-ws apollo-link
const gql = require("graphql-tag")
const { ApolloClient } = require("apollo-client")
const { ApolloClientWS } = require("apollo-client-ws")
const { InMemoryCache } = require("apollo-cache-inmemory")
const link = new ApolloClientWS({
uri: "ws://127.0.0.1:12345/api",
opts: {
/* (all options and their default values) */
debug: 0,
log: (msg) => { console.log(msg) },
protocols: [],
compress: false,
encoding: "json",
keepalive: 0,
reconnectattempts: 10,
reconnectdelay: 2 * 1000
}
})
const client = new ApolloClient({
link: link,
cache: new InMemoryCache()
})
client.query({ query: gql`{ ... }` })
.then((response) => { ... })
.catch((err) => { ... })
Apollo-Client-WS on the WebSocket connection speaks WebSocket-Framed, a very simple protocol based on the following frame format:
[ fid: number, rid: number, type: string, data: any ]
In particular, the following frames are used for the GraphQL requests and (their corresponding) responses:
request: [
fid: number = <fid>,
rid: number = 0,
type: string = "GRAPHQL-REQUEST",
data: { query: string, variables?: any, operationName?: string }
]
response: [
fid: number = <fid>,
rid: number = request.fid,
type: string = "GRAPHQL-RESPONSE",
data: { data?: any, error?: any[] }
]
When sending a custom message via ApolloClientWS::send(type: string, data: any)
,
the following frame is sent:
message: [
fid: number = <fid>,
rid: number = 0,
type: string = type,
data: any = data
]
When receiving such a custom frame, it is delivered via
ApolloClientWS::on("receive", { type, data }) => { ... })
.
There is also the alternative module Apollo-Link-WS and its underlying Subscriptions-Transport-WS for Apollo Client. In contrast to this module, Apollo-Client-WS intentionally has no direct built-in GraphQL subscription support. Also, Subscriptions-Transport-WS unfortunately, but by design, uses an opinionated way of implementing GraphQL subscriptions on the GraphQL engine side. The Apollo-Client-WS instead provides plain WebSocket communication, without any additional subscription protocol, and hence does not enfore any special support for subscriptions on the server side.
For implementing a GraphQL subscription or similar add-on protocol on top
of Apollo-Client-WS, simply use the send
method to send non-GraphQL
request messages to the server and use the receive
event for
receiving non-GraphQL response messages from the server.
/* send a subscribe command */
let data = [ "foo", 42 ]
link.send("SUBSCRIBE", data)
/* receive a notification command */
link.on("receive", ({ type, data }) => {
if (type === "NOTIFY")
notify(...data)
})
For a more elaborate out-of-the-box solution to GraphQL query subscriptions, check out GraphQL-IO. Under the hood, it already uses Apollo Client and WebSocket-Framed.
Copyright (c) 2017-2021 Dr. Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
GraphQL WebSocket Network Interface for Apollo Client
The npm package apollo-client-ws receives a total of 28 weekly downloads. As such, apollo-client-ws popularity was classified as not popular.
We found that apollo-client-ws demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.