Lokka HTTP Transport
Lokka-transport-http provides an isomorphic HTTP transport layer designed for the Lokka GraphQL client. It facilitates sending GraphQL queries over HTTP to `graphql-express` compatible endpoints. The package's current stable version is 1.6.1, with its last release approximately 9 years ago. It supports sending custom HTTP headers and integrates with existing cookie-based authentication mechanisms, or allows for explicit `Authorization` header configuration. A key differentiator is its minimalist approach as a transport layer, allowing Lokka (a simple GraphQL client itself) to focus on query construction. The package, and the broader Lokka ecosystem, appears to be unmaintained, with no recent updates or active development. This means no new features, bug fixes, or security patches are likely. By default, it throws an error on the first GraphQL error returned by the server, but this behavior can be customized.
Common errors
-
TypeError: lokka_transport_http_1.default is not a constructor
cause This error often occurs when attempting to import `HttpTransport` using CommonJS `require()` in a way that expects a direct default export, but the transpiled output puts the default export under a `.default` property.fixIf using CommonJS, adjust the import to `const HttpTransport = require('lokka-transport-http').default;` or ensure your build system is configured for proper interoperability between ESM and CJS. -
Error: GraphQL Error: [message from server]
cause The default error handling strategy of `lokka-transport-http` is to throw an `Error` object containing the message of the first GraphQL error returned by the server. This indicates that the GraphQL query itself resulted in server-side errors.fixCatch the error in your `.catch()` block and inspect the error object. For more granular control or to handle multiple GraphQL errors, provide a custom `handleErrors` function in the `HttpTransport` constructor options.
Warnings
- breaking The `lokka-transport-http` package, along with its parent `lokka` client, has been abandoned. There have been no code updates or maintenance activity for approximately 9 years, indicating a high risk of unpatched bugs, security vulnerabilities, and incompatibility with modern JavaScript environments or GraphQL server versions.
- gotcha This package does not handle authentication logic itself. Users are responsible for managing and providing authentication credentials, typically via custom HTTP headers (e.g., `Authorization`) or relying on existing cookie-based authentication mechanisms.
- gotcha By default, `lokka-transport-http` will throw a new `Error` object using only the *first* GraphQL error encountered in the response. This may hide other errors present in the GraphQL response array.
Install
-
npm install lokka-transport-http -
yarn add lokka-transport-http -
pnpm add lokka-transport-http
Imports
- HttpTransport
const HttpTransport = require('lokka-transport-http');import HttpTransport from 'lokka-transport-http';
Quickstart
import HttpTransport from 'lokka-transport-http';
import Lokka from 'lokka';
// Initialize Lokka with the HTTP transport
const client = new Lokka({
transport: new HttpTransport('http://graphql-swapi.parseapp.com/')
});
// Send a simple GraphQL query
client.query(`
{
allFilms {
films {
title
}
}
}
`).then(response => {
console.log('Query successful:', JSON.stringify(response, null, 2));
}).catch(error => {
console.error('Query failed:', error);
});
// Example with custom headers (e.g., for authentication)
const headers = {
'Authorization': `Bearer ${process.env.AUTH_TOKEN ?? ''}`,
'x-custom-header': 'some-value'
};
const authenticatedClient = new Lokka({
transport: new HttpTransport('http://your-authenticated-graphql-endpoint.com/graphql', { headers })
});
authenticatedClient.query(`{ viewer { id } }`)
.then(response => console.log('Authenticated query:', response))
.catch(error => console.error('Authenticated query failed:', error));