Registry / testing / graphql-format-error-context-extension

graphql-format-error-context-extension

JSON →
library0.2.15jsnpmunverified

A GraphQL server extension (v0.2.15) that enables access to the request context inside the formatError function in Apollo Server (2.x). It bridges the gap between Apollo's formatError and the request context by injecting the context object as a second argument. Last release appears stable but uses deprecated graphql-extensions; no recent updates. Alternatives may use other approaches to pass context to error formatting.

npm install graphql-format-error-context-extension
INSTALL
IMPORT
SIG · GRAPHQL-FORMAT-ERR
G
graphql-format-error-context-extension
testingjavascriptv0.2.15
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.

FormatErrorWithContextExtension
const { FormatErrorWithContextExtension } = require('graphql-format-error-context-extension');
import { FormatErrorWithContextExtension } from 'graphql-format-error-context-extension';
Package uses CommonJS; ESM import may not work or require specific bundler configuration.
FormatErrorWithContextExtension
const FormatErrorWithContextExtension = require('graphql-format-error-context-extension').FormatErrorWithContextExtension;
const FormatErrorWithContextExtension = require('graphql-format-error-context-extension');
Default export does not exist; always destructure or access the named export.
FormatErrorWithContextExtension (default)
const FormatErrorWithContextExtension = require('graphql-format-error-context-extension').default;
const FormatErrorWithContextExtension = require('graphql-format-error-context-extension');
There is no default export; using require() directly returns an object with 'default' property only if entry point uses module.exports.default. This may be undefined.

Demonstrates how to create an Apollo Server 2.x instance using the extension to access context in formatError.

const { ApolloServer } = require('apollo-server-koa'); const { FormatErrorWithContextExtension } = require('graphql-format-error-context-extension'); const formatError = (err, context) => { // 'context' is the request context passed from Apollo Server return err; }; const server = new ApolloServer({ typeDefs: `type Query { hello: String }`, resolvers: { Query: { hello: () => 'world' } }, extensions: [() => new FormatErrorWithContextExtension(formatError)], context: ({ ctx }) => ctx, }); server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
Debug
Known issues
breakingThis package relies on the deprecated 'graphql-extensions' package, which is not supported in Apollo Server 3.x+.
fix
Upgrade to Apollo Server 3+ and use built-in formatError with context via plugin (e.g., use 'willSendResponse' plugin) or switch to a compatible plugin.
affects: >=0.2.15
deprecated'graphql-extensions' is deprecated in favor of Apollo Server plugins since v2.18.
fix
For Apollo Server 2.18+, consider using the plugin API instead of extensions.
affects: *
gotchaThe extension must be instantiated inside a factory function (() => new FormatErrorWithContextExtension(...)). Passing the instance directly will cause issues with extension lifecycle.
fix
Always wrap the extension creation in a function: extensions: [() => new FormatErrorWithContextExtension(formatError)].
affects: *
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'formatError')
FormatErrorWithContextExtension is not properly imported or the extension is not instantiated correctly.
fix
Ensure you are using require to import the package and wrap the instantiation in a function: const { FormatErrorWithContextExtension } = require('graphql-format-error-context-extension'); extensions: [() => new FormatErrorWithContextExtension(yourFormatErrorFn)];
Error: Schema must contain uniquely named types but contains multiple types named "FormatErrorWithContextExtension"
Accidentally importing the extension as a GraphQL type instead of as an extension object.
fix
Ensure you are using the exported class, not the graphql-extensions module directly. Check your import statement.
Upgrade
Version history
0.2.15latest on npm
Audit
Dependencies
graphql-extensionsrequiredPeer dependency; required for the extension to work with Apollo Server 2.x.
Agent activity
8 hits · last 30 days
node
8
Resources
graphql-format-error-context-extension — npm install graphql-format-error-context-extension · libregistry