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();
Errors
Common errors & fixes
TypeError: Cannot destructure property 'operationName' of 'options' as it is undefined.
query() called without options object or with undefined.
fixEnsure 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).
fixCheck 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.
fixInstall node-fetch and import it: import fetch from 'node-fetch'; global.fetch = fetch;
Audit
Dependencies
No dependency data recorded yet.