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

@jetblack/graphql-client

v4.0.2

Published

Simple non-caching GraphQL client.

Downloads

3

Readme

jetblack-graphql-client

This is work in progress.

Overview

A simple non-caching GraphQL client for query, mutation and subscription.

I wanted a simple non-caching GraphQL subscription client written in ES6 javascript with no external dependencies.

The protocol for GraphQL WebSocket subscriptions can be found here.

This implementation is deliberately explicit and low on features as I wanted to keep the algorithm clear. Features like observables and caching should be implmented in other libraries. For example see here for a reconnecting subscriber.

Installation

Install from npm.

yarn add @jetblack/graphql-client

Usage

There are two functions:

  • graphQLSubscriber (url, options, callback, protocols = 'graphql-ws')
  • graphQLFetch (url, query, variables = {}, operationName = null, init = fetchOptions)

The graphQLSubscriber implements the WebSocket protocol. The function takes the url for the WebSocket, an options object which is simply passed as JSON to the server, and a callback with the prototype (error, subscribe). A function is returned which can be used to shutdown the subscriber. If both error and subscribe are null the connection has been closed normally.

The subscribe argument is a function with the prototype subscribe(query, variables, operationName, callback). When subscribe is called it returns a function that can be called to unsubscribe. The callback to the subscribe function has the prototype callback(error, data). If both error and data are null then connection hs been closed normally.

The protocols defaults to "graphql-ws". The documentation suggests this can be an array or strings, but the first should be the default.

The graphQLFetch function is a simple fetch implementation for query and mutation operations. There are numerous implementations of this available, and it is provided for convenience. The init parameter is passed through to fetch. It has the default value fetchOptions which is defined as:

const fetchOptions = {
  method: 'post',
  headers: { 'Content-Type': 'application/json' }
}

// The fetchOptions can be extended.
const myFetchOptions = {
    ...fetchOptions,
    mode: 'cors'
}

There follows an example of the graphQLSubscriber.

import { Subscriber } from '@jetblack/graphql-client'

const url = 'ws://localhost/subscriptions'
const options = {}

const query = `
subscription {
    mySubscription {
        someData
    }
}
`
variables = {}
operationName = null

const shutdown = graphQLSubscriber(
    url,
    options,
    (error, subscribe) => {
        if (!(error || subscribe)) {
            // Normal closure.
            return
        }
        if (error) {
            console.error(error)
            throw error
        }
        const unsubscribe = subscribe(
            query,
            variables,
            operationName,
            (error, data) => {
                if (!(error || subscribe)) {
                    // Normal closure
                    return
                }
                if (error) {
                    console.error(error)
                    throw error
                }
                console.log(data)
            })
        
        // Some time later ...
        unsubscribe()
    })

// Some time later ...
shutdown()

The graphQLFetch function is used as follows.

import { Fetcher } from '@jetblack/graphql-client'

const fetcher = new RetryFetcher('http://localhost/graphql')

// An example mutation.
graphQLFetch(
    `
mutate CreditAccount($account: ID!, $amount: Float!) {
    creditAccount(account: $account, amount: $amount) {
        balance
    }
}`,
    {
        account: '1234',
        amount: 19.99
    }
)
.then(respoonse => response.json())
.then(response => {
    console.log(response.data.balance)
})
.catch(error => {
    console.error(error)
})