Apollo-Client-WS
GraphQL WebSocket Network Interface for Apollo Client
About
This is a GraphQL
WebSocket
based NetworkInterface
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 could be used with any GraphQL server speaking plain (JSON-encoded) GraphQL
request/response messages over WebSocket connections.
Installation
$ npm install graphql-tag apollo-client apollo-client-ws
Usage
const gql = require("graphql-tag")
const ApolloClient = require("apollo-client")
const { NetworkInterfaceWS } = require("apollo-client-ws")
const networkInterface = new NetworkInterfaceWS({
uri: "ws://127.0.0.1:12345/api",
opts: {
debug: false,
log: (msg) => { console.log(msg) },
protocols: [],
compress: false,
encoding: "json",
keepalive: 0,
reconnectattempts: 3,
reconnectdelay: 2 * 1000
}
})
const apolloClient = new ApolloClient.ApolloClient({
networkInterface: networkInterface
})
apolloClient.query({ query: gql`{ ... }` })
.then((response) => { ... })
.catch((err) => { ... })
Notice
There is also the alternative module
Subscriptions-Transport-WS
for Apollo Client. While
Apollo-Client-WS sends plain GraphQL request/response messages over
WebSockets and intentionally has no direct built-in subscription support (although one
can easily add it on top of it with the message
event and the query:request
and query:response
hooks),
the Subscriptions-Transport-WS module uses an
own protocol
on top of WebSockets to support the subscription notification and
by design uses an opinionated variant of subscription support on the server side.
License
Copyright (c) 2017 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.