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

graphql-http-ws-client

v3.1.2

Published

### Node.js with HTTP and WS links, context, and [retry options](https://www.apollographql.com/docs/react/api/link/apollo-link-retry/)

Downloads

279

Readme

GraphQL client over HTTP/WS

Node.js with HTTP and WS links, context, and retry options

import { createGraphQLClient } from "graphql-http-ws-client";
import WebSocket from "ws";
import fetch from "node-fetch";

let addContext = async () => {
	return {
		headers: {
			Authorization: `Bearer MYAUTHTOKEN`
		}
	}
};

const { client } = createGraphQLClient("MY_GRAPHQL_URL", {
	createWSLink: true,
    createHTTPLink: true,
    websocket: WebSocket,
    httpLinkOptions: {
        fetch: fetch,
        setContext: addContext
    },
    wsLinkOptions: {
        connectionParams: addContext
    },
    retryLinkOptions: {
        delay: {
          initial: 300,
          max: 6000,
          jitter: true
        },
        attempts: {
          max: Infinity
        }
    }
});

Node.js with HTTP link only (no Subscriptions)

import { createGraphQLClient } from "graphql-http-ws-client";
import fetch from "node-fetch";

const { client } = createGraphQLClient("MY_GRAPHQL_URL", {
	httpLinkOptions: {
	    fetch: fetch
	},
	createWSLink: false
});

Node.js with WS link only

import { createGraphQLClient } from "graphql-http-ws-client";
import WebSocket from "ws";
import fetch from "node-fetch";

const { client } = createGraphQLClient("MY_GRAPHQL_URL", {
	websocket: WebSocket,
	createHTTPLink: false
});

Node.js with WS link and graphql-transport-ws subprotocol

import { createGraphQLClient } from "graphql-http-ws-client";
import WebSocket from "ws";
import fetch from "node-fetch";

const { client } = createGraphQLClient("MY_GRAPHQL_URL", {
	websocket: WebSocket,
    wsSubprotocol: "graphql-transport-ws",
	createHTTPLink: false
});

Node.js with WS link and graphql-ws subprotocol (default)

import { createGraphQLClient } from "graphql-ws";
import WebSocket from "ws";
import fetch from "node-fetch";

const { client } = createGraphQLClient("MY_GRAPHQL_URL", {
	websocket: WebSocket,
    wsSubprotocol: "graphql-ws",
	createHTTPLink: false
});

React with HTTP and WS links

import WebSocket from "ws";
import fetch from "node-fetch";
import {createGraphQLClient, gql} from "graphql-http-ws-client";
import { persistCache } from "apollo-cache-persist";
const { client, cache } = createGraphQLClient("MY_GRAPHQL_URL");

const waitOnCache = persistCache({
	cache: cache,
	storage: window.localStorage
});

waitOnCache.then(() => {
	ReactDOM.render(
		<ApolloProvider client={client}>
			<Router>
				<App/>
			</Router>
		</ApolloProvider>,
		document.getElementById('root')
	)
});

Simple Queries

Using the server example from graphql-http-ws-server

client.query({
    query: `query {
        hello
    }`
}).then(({data}) => {
    console.log("DATA", data);
});

Simple Subscriptions

Using the server example from graphql-http-ws-server

client.subscribe({
    query: `subscription {
        time
    }`
}).subscribe({
    next({data}) {
        console.log(data);
    }
});

Changes

v3.1

  • Retry Link options supported
  • Default retry link config changes to Apollo link default (notably, 5 attempts instead of infinte attempts)

v3.0

  • Exports ES6 and CJS versions

v2.0

  • Changed package type to module
  • Passed-in queries and mutations are now automatically wrapped with gql() tag, if they are not already wrapped

v0.3

  • Queries and mutations can now be passed as strings instead of being wrapped in the gql tag

v0.2

  • Module now requires graphql and subscriptions-transport-ws as peer dependencies
  • Module now exports gql and all exports from @apollo/client/core
  • Renamed createWebsocketLink to createWSLink and websocket option to ws for consistency with options
  • New httpLinkOptions and wsLinkOptions parameters
    • fetch option moves to httpLinkOptions option
    • all ws link options move to wsLinkOptions option