Awesome GraphQL Client
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
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ES module (ESM) package in a CommonJS (CJS) context.fixUpdate your import statements from `const Client = require('awesome-graphql-client');` to `import { AwesomeGraphQLClient } from 'awesome-graphql-client';`. Ensure your `package.json` contains `"type": "module"` or files use `.mjs` extension. -
TypeError: AwesomeGraphQLClient is not a constructor
cause CommonJS `require()` is being used, which returns the module object, not a direct constructor in an ESM package, or an incorrect named import.fixRefactor your import to `import { AwesomeGraphQLClient } from 'awesome-graphql-client';` for ESM, or if still in a CJS environment (which is not supported since v3), you might attempt `const { AwesomeGraphQLClient } = await import('awesome-graphql-client');` but full CJS compatibility is removed in v3. -
The package 'awesome-graphql-client' requires Node.js version ^20.19.0 || ^22.12.0 || >=23. Your current Node.js version is X.
cause Your Node.js runtime environment does not meet the minimum version requirements specified by the package.fixUpgrade your Node.js installation to a supported version (20.19.0, 22.12.0, or newer). Use a tool like `nvm` (Node Version Manager) for easy version switching. -
TypeError: fetch is not a function
cause The environment where `awesome-graphql-client` is running (e.g., an older Node.js version) does not provide a global `fetch` API.fixProvide a `fetch` polyfill to the client configuration: `new AwesomeGraphQLClient({ endpoint: '/graphql', fetch: require('node-fetch') })` (for Node.js versions without native `fetch`) or ensure your Node.js version is recent enough (v18+ for global fetch).
Warnings
- breaking Version 3.0.0 of `awesome-graphql-client` is now ESM-only. This means CommonJS `require()` statements are no longer supported and will result in runtime errors.
- breaking Version 2.0.0 introduced a breaking change requiring Node.js version 18.18.0 or newer. Older Node.js versions are not supported.
- breaking Version 0.13.0 dropped support for Node.js 12. Using this version or newer with Node.js 12 will result in compatibility issues.
- gotcha The `gql` export from `awesome-graphql-client` is an internal utility for query formatting, not an alias for the `graphql-tag` package. While it serves a similar purpose, do not confuse it with the external `graphql-tag` library.
- gotcha Node.js environment setup for file uploads requires either a global `File` constructor (available in Node.js v20+) or importing `File` from `node:buffer` for older Node.js versions (v16.17.0+). Additionally, `openAsBlob` from `node:fs` is crucial for handling local files.
Install
-
npm install awesome-graphql-client -
yarn add awesome-graphql-client -
pnpm add awesome-graphql-client
Imports
- AwesomeGraphQLClient
const AwesomeGraphQLClient = require('awesome-graphql-client');import { AwesomeGraphQLClient } from 'awesome-graphql-client'; - GraphQLRequestError
const { GraphQLRequestError } = require('awesome-graphql-client');import { GraphQLRequestError } from 'awesome-graphql-client'; - gql
import gql from 'graphql-tag';
import { gql } from 'awesome-graphql-client'; - isFileUpload
const isFileUpload = require('awesome-graphql-client').isFileUpload;import { isFileUpload } from 'awesome-graphql-client';
Quickstart
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();