Awesome GraphQL Client

3.0.0 · active · verified Tue Apr 21

Awesome GraphQL Client is a lightweight, zero-dependency GraphQL client library designed for both NodeJS and browser environments. It currently ships as v3.0.0 and is ESM-only. The library's core feature set includes robust GraphQL File Upload support as per the `graphql-multipart-request-spec`, full TypeScript integration, and compatibility with GraphQL queries generated by `graphql-tag`. While it does not specify a strict release cadence, the project shows consistent maintenance with regular updates addressing features, bug fixes, and Node.js version compatibility. Key differentiators include its minimal footprint (around 2KB gzipped), built-in file upload capabilities, and suitability for modern React applications when paired with libraries like `react-query`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate `AwesomeGraphQLClient` in a NodeJS environment, perform a GraphQL mutation with file upload support using `node:fs` and the global `File` API (available in Node.js v20+), and includes basic error handling. It creates a temporary image file for the upload example.

import { openAsBlob } from 'node:fs';
import { AwesomeGraphQLClient, GraphQLRequestError } from 'awesome-graphql-client';
import { File } from 'node:buffer'; // For Node.js versions < 20, where global File isn't available
import fs from 'node:fs/promises';

const dummyFilePath = './temp_avatar.png';

async function run() {
  // Create a dummy 1x1 transparent PNG file for the example
  await fs.writeFile(dummyFilePath, Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'));

  const client = new AwesomeGraphQLClient({
    endpoint: 'http://localhost:8080/graphql', // IMPORTANT: Replace with your actual GraphQL endpoint
    onError: (error: GraphQLRequestError | Error) => {
      if (error instanceof GraphQLRequestError) {
        console.error('GraphQL API Error:', error.message, 'Details:', error.errors, 'Extensions:', error.extensions);
      } else {
        console.error('Network or client-side Error:', error.message);
      }
    }
  });

  const UploadUserAvatar = `
    mutation uploadUserAvatar($userId: Int!, $file: Upload!) {
      updateUser(id: $userId, input: { avatar: $file }) {
        id
        avatarUrl
      }
    }
  `;

  try {
    const blob = await openAsBlob(dummyFilePath);
    // Node.js v20+ provides a global `File` constructor. For older versions, import from 'node:buffer'.
    const file = new File([blob], 'avatar.png', { type: 'image/png' });

    console.log('Attempting to upload avatar for userId 10...');
    const data = await client.request(UploadUserAvatar, { file: file, userId: 10 });
    console.log('Successfully updated user:', data.updateUser.id, 'with new avatar URL:', data.updateUser.avatarUrl);
  } catch (error) {
    console.error('An unhandled error occurred during request:', error);
  } finally {
    // Clean up the dummy file
    await fs.unlink(dummyFilePath);
  }
}

run();

view raw JSON →