Registry / api-client / graphql.js

graphql.js

JSON →
library0.6.8jsnpmunverified

A lightweight, dependency-free GraphQL client for JavaScript (version 0.6.8, last updated in 2017). It supports both browser and Node.js environments, providing a simple API for executing queries and mutations with fragment management. Key differentiators: <4kB gzipped, zero dependencies, isomorphic, query merging, and no requirement for React or Relay. However, it is outdated and lacks TypeScript support, modern ESM, and subscription support. Development appears to be stagnant with no recent releases.

npm install graphql.js
INSTALL
IMPORT
SIG · GRAPHQL.JS
G
graphql.js
api-clientjavascriptv0.6.8
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
✓ import graphql from 'graphql.js'
✗ import { graphql } from 'graphql.js'
The package exports a single default function. Named import pattern is incorrect.
require
✓ const graphql = require('graphql.js')
✗ const { graphql } = require('graphql.js')
CommonJS require yields the default export as a function. Destructuring will fail.
graphql (global)
✓ <script src='graphql.min.js'></script>
In browser, the library sets a global `graphql` function. No import needed.

Shows connection setup with token, fragment definition, and execution of both a mutation and a query.

import graphql from 'graphql.js'; const graph = graphql('/graphql', { method: 'POST', headers: { 'Authorization': 'Bearer ' + (process.env.GRAPHQL_TOKEN ?? '') }, fragments: { user: 'on User { id, name }' } }); const allUsers = graph(`query { allUsers { ...user } }`); const createUser = graph(`mutation (@autodeclare) { createUser($firstName, $lastName) { ...user } }`); async function main() { const newUser = await createUser({ firstName: 'John', lastName: 'Doe' }); console.log('Created:', newUser); const users = await allUsers(); console.log('All users:', users); } main().catch(console.error);
Debug
Known issues
deprecatedPackage is no longer actively maintained. Last release was in 2017. Use modern alternatives like graphql-request or Apollo Client.
fix
Migrate to a maintained GraphQL client such as graphql-request.
affects: >=0.0.0
gotchaQuery preparation returns a function that must be called to execute. Forgetting to invoke results in a Promise-like object that is not thenable.
fix
Always call the prepared function: const result = await graph(query)(variables);
affects: >=0.0.0
gotchaThe library uses `@autodeclare` directive to automatically declare variables. If omitted, variables must be manually defined in the query string.
fix
Use `@autodeclare` in mutation/query string or explicitly declare variables (e.g., `$email: String!`).
affects: >=0.0.0
gotchaThe `graph` function returns the raw `data` object from the GraphQL response on success and the `errors` object on failure. There is no built-in error handling for network issues.
fix
Manually check for errors in the response or use a try-catch around the call.
affects: >=0.0.0
Errors
Common errors & fixes
Uncaught TypeError: graph is not a function
Importing the library incorrectly, e.g., using named import instead of default.
fix
Use `import graphql from 'graphql.js'` or `const graphql = require('graphql.js')`.
TypeError: graph(...) is not a function
Calling the `graph` function without passing a query string first. The initial call configures and returns a query executer.
fix
First call `graph(queryString)` to prepare a query, then call the returned function with variables: `const exec = graph('query...'); exec(variables)`.
Cannot read property 'data' of undefined
The server returned a non-200 status or the response format is unexpected. The library expects a JSON response with a `data` field.
fix
Check the GraphQL endpoint URL and ensure it returns valid JSON with `{ data: ... }`. Add error handling for network failures.
Upgrade
Version history
0.6.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
graphql.js — npm install graphql.js · libregistry