0.16.0 • Published 11 months ago

graphqlex v0.16.0

Weekly downloads
3
License
MIT
Repository
github
Last release
11 months ago

graphqlex

A minimal client library for executing GraphQL APIs, supporting both HTTP queries/mutations and WebSockets subscriptions.

npm install graphqlex

Usage

import { gql, Api } from "graphqlex"

const api = new Api("http://localhost:5000/graphql")

// Sample query taken from postgraphile tutorial
const query = gql`
  query {
    allPosts ( offset: 0 ) {
      nodes { 
        headline 
      }
    }
  }
`

// Postgraphile tutorial extended with live queries
const subscription = gql`
  subscription {
    allPosts ( offset: 0 ) {
      nodes { 
        headline 
      }
    }
  }
`

// Run a query
const result = await api.run(query) // you can pass variables too
const posts = result.allPosts.nodes
// posts is an array of objects with property headline


// Subscribe to a live query
api.subscribe({
  query: subscription, 
  channelName: "posts", // If missing a random channel name is assigned 
  onData: data => {
  	console.log(data.allPosts.nodes)
	}
)

Why?

There are more powerful and advanced GraphQL client libraries out there including Apollo Client, which has lots of power, such as being able to validate queries, cache results etc. But you may find that all you need is to execute queries and mutations and connect to subscriptions. In that case GraphQLEx is smaller, more straightforward, and can be loaded directly into a modern browser via ES6 without being transpiled or bundled.

graphqlex doesn't parse, process or introspect your queries - it just executes them and marshals their responses back to your app. Because of that, it adds less code, along with the time taken to download and parse it on page load, and to execute it on each GraphQL operation. In a modern, fast JS runtime on a high-speed network you and your users may well not notice the difference, so whether this is an advantage depends on your situation.

graphqlex is published as a single ES6 module, suitable for loading directly in a browser application. In development, you would typically use something like es-dev-server to enable "naked" imports as shown above, or maybe look into using import maps as support rolls out across modern browsers. For production you would generally use a bundler such as rollup to package graphqlex along with your application modules.

0.16.0

11 months ago

0.15.0

1 year ago

0.10.0

2 years ago

0.11.0

2 years ago

0.9.0

2 years ago

0.12.0

2 years ago

0.13.0

1 year ago

0.8.0

2 years ago

0.7.0

3 years ago

0.5.0

3 years ago

0.6.0

3 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago