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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@unifiedsoftware/graphql-client

v1.0.2

Published

Flexible GraphQL client with event system, interceptors, and caching

Downloads

263

Readme

@unifiedsoftware/graphql-client

Flexible GraphQL client with event system, interceptors, and caching built on top of @unifiedsoftware/http-client.

Features

  • Built on HttpClient - Leverages the flexible HTTP client with interceptors and event system
  • Query Caching - Optional in-memory caching with configurable TTL
  • Type-Safe - Full TypeScript support with generic types
  • Error Handling - Comprehensive GraphQL error handling
  • Lightweight - Minimal dependencies, focused on core functionality

Installation

pnpm add @unifiedsoftware/graphql-client @unifiedsoftware/http-client

Quick Start

import { HttpClient, createFetchAdapter } from "@unifiedsoftware/http-client";
import { createGraphQLClient } from "@unifiedsoftware/graphql-client";

// Create HTTP client
const httpClient = new HttpClient({
  adapter: createFetchAdapter(),
  baseURL: "https://api.example.com",
  headers: {
    Authorization: "Bearer your-token",
  },
});

// Create GraphQL client
const graphql = createGraphQLClient(httpClient, {
  endpoint: "/graphql",
  cache: true,
  cacheTTL: 5 * 60 * 1000, // 5 minutes
});

// Execute a query
const data = await graphql.query(
  `
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }
`,
  { id: "123" }
);

console.log(data.user);

Usage

Queries

// Simple query
const data = await graphql.query(`
  query {
    users {
      id
      name
    }
  }
`);

// Query with variables
const data = await graphql.query(
  `
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }
`,
  { id: "123" }
);

// Query with operation name
const data = await graphql.query(
  `query GetUser($id: ID!) { ... }`,
  { id: "123" },
  "GetUser"
);

Mutations

// Execute a mutation
const result = await graphql.mutate(
  `
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) {
      id
      name
      email
    }
  }
`,
  {
    input: {
      name: "John Doe",
      email: "[email protected]",
    },
  }
);

console.log(result.createUser);

Type Safety

// Define your types
interface User {
  id: string;
  name: string;
  email: string;
}

interface GetUserResponse {
  user: User;
}

// Use with type parameter
const data = await graphql.query<GetUserResponse>(
  `
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }
`,
  { id: "123" }
);

// TypeScript knows the shape of data
console.log(data.user.name); // ✓ Type-safe

Error Handling

import { GraphQLError } from "@unifiedsoftware/graphql-client";

try {
  const data = await graphql.query(`...`);
} catch (error) {
  if (error instanceof GraphQLError) {
    console.error("GraphQL Errors:");
    error.messages.forEach((msg) => console.error(`  - ${msg}`));

    // Access detailed error information
    console.error("First error:", error.firstError);
    console.error("All errors:", error.errors);
  }
}

Caching

// Enable caching
const graphql = createGraphQLClient(httpClient, {
  endpoint: "/graphql",
  cache: true,
  cacheTTL: 5 * 60 * 1000, // 5 minutes
});

// First call - fetches from server
const data1 = await graphql.query(`query { users { id } }`);

// Second call - returns from cache
const data2 = await graphql.query(`query { users { id } }`);

// Clear specific query cache
graphql.clearCache(`query { users { id } }`);

// Clear all cache
graphql.clearCache();

Custom Headers

const graphql = createGraphQLClient(httpClient, {
  endpoint: "/graphql",
  headers: {
    "X-Custom-Header": "value",
    Authorization: "Bearer token",
  },
});

Raw Requests

// Execute a raw GraphQL request
const response = await graphql.request({
  query: `query { users { id } }`,
  variables: { id: "123" },
  operationName: "GetUsers",
});

// Access full response including errors
console.log(response.data);
console.log(response.errors);
console.log(response.extensions);

API Reference

createGraphQLClient(httpClient, config)

Creates a new GraphQL client instance.

Parameters:

  • httpClient: HttpClient - Instance of @unifiedsoftware/http-client
  • config: GraphQLClientConfig - Configuration options

Returns: GraphQLClient

GraphQLClientConfig

interface GraphQLClientConfig {
  endpoint: string; // GraphQL endpoint URL
  headers?: Record<string, string>; // Default headers
  cache?: boolean; // Enable caching (default: false)
  cacheTTL?: number; // Cache TTL in ms (default: 5 minutes)
}

GraphQLClient

query<T>(query, variables?, operationName?): Promise<T>

Execute a GraphQL query.

mutate<T>(mutation, variables?, operationName?): Promise<T>

Execute a GraphQL mutation.

request<T>(request): Promise<GraphQLResponse<T>>

Execute a raw GraphQL request.

clearCache(query?, variables?): void

Clear cache entries.

getHttpClient(): HttpClient

Get the underlying HTTP client instance.

Integration with Generated Clients

This package is designed to work seamlessly with clients generated by @unifiedsoftware/generator-graphql:

import { createGraphQLClient } from "@unifiedsoftware/graphql-client";
import { createPortalApiClient } from "./generated/portal-api";

const httpClient = new HttpClient({
  /* ... */
});
const graphql = createGraphQLClient(httpClient, {
  endpoint: "https://localhost:44357/graphql",
});

const client = createPortalApiClient(graphql);

// Use typed operations
const users = await client.queries.getUsers();
const user = await client.mutations.createUser({ name: "John" });

License

MIT