Registry / testing / fetchql

fetchql

JSON →
library3.0.0jsnpmunverified

A lightweight GraphQL query client using the Fetch API for both browser and Node.js environments. Current version 3.0.0 (published 2019-10-28) adds TypeScript declaration file. Works with any fetch polyfill like node-fetch. Focuses on simplicity with built-in interceptors for request/response handling, request state callbacks, and enum type caching. Differentiates from Apollo and Relay by being minimal (no cache, no state management), ideal for small projects or when using GraphQL with vanilla fetch.

npm install fetchql
INSTALL
IMPORT
SIG · FETCHQL
F
fetchql
testingjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (FetchQL class)
import FetchQL from 'fetchql'
const { FetchQL } = require('fetchql')
Package exports default export. Named import 'FetchQL' is incorrect. Also supports require: const FetchQL = require('fetchql'). ESM support since v2.2.0.
FetchQL.query()
import FetchQL from 'fetchql'; const client = new FetchQL({ url: 'http://localhost:4000/graphql' }); client.query({ operationName: 'GetUser', query: '...', variables: {} }).then(data => ...)
new FetchQL().query({ query: '...' })
Operation name is required; without it query will fail. Variables are optional but must be object if provided.
FetchQL interceptors
import FetchQL from 'fetchql'; const client = new FetchQL({ url: '...', interceptors: [{ request: (url, config) => [url, config], response: response => response }] })
interceptors: { request: () => {}, response: () => {} }
Interceptors can be a single object or array of objects. Each interceptor must have functions: request, requestError, response, responseError.

Complete example: initialize FetchQL client with URL, auth headers, and callbacks; execute a query; fetch enum types; add dynamic interceptors and remove them.

import FetchQL from 'fetchql'; const client = new FetchQL({ url: process.env.GRAPHQL_URL ?? 'http://localhost:4000/graphql', headers: { Authorization: `Bearer ${process.env.TOKEN ?? ''}` }, onStart: (queueLength) => console.log(`Queue started, length: ${queueLength}`), onEnd: (queueLength) => console.log(`Queue ended, length: ${queueLength}`), omitEmptyVariables: true }); client.query({ operationName: 'GetUser', query: ` query GetUser($id: ID!) { user(id: $id) { id name } } `, variables: { id: '123' } }) .then(data => console.log('User:', data)) .catch(err => console.error('GraphQL error:', err)); // Get enum types client.getEnumTypes(['UserRole', 'Status']) .then(enumMap => console.log('Enums:', enumMap)) .catch(err => console.error('Enum fetch error:', err)); // Add interceptor dynamically const removeInterceptor = client.addInterceptors([{ request: (url, config) => { config.headers['X-Custom'] = 'value'; return [url, config]; }, response: response => response }]); // Remove the added interceptor later removeInterceptor(); // Clear all interceptors client.clearInterceptors();
Debug
Known issues
gotchaoperationName must be provided in query options; omitting it will cause query to reject.
fix
Always include operationName in the query call: { operationName: 'MyQuery', query: '...' }
affects: >=2.0.0
breakingSince v2.0.0, source code moved to ./src and distributed file to ./lib; file is now unminified.
fix
Update imports to use 'fetchql' (default module resolution) instead of relying on specific file paths.
affects: >=2.0.0 <3.0.0
gotchaThe package requires a fetch polyfill on Node.js (e.g., node-fetch). Missing fetch will cause runtime errors.
fix
Install node-fetch (or similar) and ensure global fetch is defined: require('node-fetch');
affects: *
gotchaomitEmptyVariables option (global or per-query) removes null or empty string ('') properties from variables; unexpected deletion of falsy values like 0 or false.
fix
Do not use omitEmptyVariables if you need to pass 0 or false as variables.
affects: *
Errors
Common errors & fixes
TypeError: Cannot destructure property 'operationName' of 'options' as it is undefined.
query() called without options object or with undefined.
fix
Ensure query() is called with an object containing operationName, query, and optionally variables: client.query({ operationName: '...', query: '...' })
Unhandled promise rejection: Response not OK: 400 Bad Request
GraphQL server returned error (e.g., missing variables or invalid query).
fix
Check GraphQL server response body for errors; ensure query syntax is correct and variables match schema.
fetch is not defined
Running in Node.js without a fetch polyfill.
fix
Install node-fetch and import it: import fetch from 'node-fetch'; global.fetch = fetch;
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetchql — npm install fetchql · libregistry