Registry / devops / graphql-custom-directives

graphql-custom-directives

JSON →
library0.2.16jsnpmunverified

graphql-custom-directives v0.2.16: a collection of reusable GraphQL schema directives for transforming field values (date formatting, number/currency, string casing, trimming, templating, phone formatting) at query execution time. Built on Moment, Lodash, and Numeral-js. Compatible with graphql-js and Apollo Server. Current stable version is 0.2.16 (last updated 2019); release cadence is low – no updates since 2019. Key differentiator: provides ready-made directive implementations without needing to write custom directive classes. Contrast with graphql-custom-directive (a library for authoring new directives).

npm install graphql-custom-directives
INSTALL
IMPORT
SIG · GRAPHQL-CUSTOM-DIR
G
graphql-custom-directives
devopsjavascriptv0.2.16
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.

applySchemaCustomDirectives
import { applySchemaCustomDirectives } from 'graphql-custom-directives'
const applySchemaCustomDirectives = require('graphql-custom-directives').applySchemaCustomDirectives
Works in both ESM and CJS, but named import is idiomatic.
GraphQLUpperCaseDirective
import { GraphQLUpperCaseDirective } from 'graphql-custom-directives'
import GraphQLUpperCaseDirective from 'graphql-custom-directives'
Default export is an object containing all directives; named exports are preferred.
GraphQLDateDirective
import { GraphQLDateDirective } from 'graphql-custom-directives'
const { GraphQLDateDirective } = require('graphql-custom-directives')
CJS require works if using destructuring; but ESM import is recommended.
GraphQLCurrencyDirective
import { GraphQLCurrencyDirective } from 'graphql-custom-directives'
No common wrong pattern for this specific directive.

Demonstrates setting up a schema with a custom uppercase directive and executing a query.

import { GraphQLUpperCaseDirective, applySchemaCustomDirectives } from 'graphql-custom-directives'; import { GraphQLSchema, GraphQLObjectType, GraphQLString, graphql } from 'graphql'; const schema = new GraphQLSchema({ directives: [GraphQLUpperCaseDirective], query: new GraphQLObjectType({ name: 'Query', fields: { hello: { type: GraphQLString, resolve: () => 'hello' } } }) }); applySchemaCustomDirectives(schema); graphql(schema, '{ hello @uppercase }').then(result => console.log(result)); // { data: { hello: 'HELLO' } }
Debug
Known issues
deprecatedThe library has not been updated since 2019 and may not work with GraphQL v16+ due to changes in SchemaDirectiveVisitor API.
fix
Consider migrating to graphql-tools v7+ or using @graphql-tools/schema with mapSchema and MapperKind.
affects: >=0.2.16
breakingRequires an older version of graphql (v14 or v15). graphql v16 removed SchemaDirectiveVisitor, which this library relies on.
fix
Upgrade graphql to v14.x or v15.x, or use a different directive library such as @graphql-tools/schema.
affects: >=16.0.0
gotchaapplySchemaCustomDirectives mutates the schema object in place. This can cause unexpected side effects if the schema is reused.
fix
Always call applySchemaCustomDirectives immediately after schema creation and before executing queries.
affects: >=0.1.0
gotchaDirectives use Moment.js for date formatting, which is a large dependency and may be unwanted in modern builds.
fix
Consider using a lighter date library or custom directives.
affects: >=0.1.0
Errors
Common errors & fixes
GraphQLSchema validation error: Unknown directive "uppercase".
Directive is not registered in the schema's directives array.
fix
Add GraphQLUpperCaseDirective (or the appropriate directive) to the directives array when constructing the GraphQLSchema.
TypeError: Cannot read property 'directives' of undefined
applySchemaCustomDirectives was called before schema was fully built.
fix
Ensure the schema object is fully constructed before calling applySchemaCustomDirectives(schema).
Error: Directive 'date' expects argument 'format' but it is not provided.
Missing required argument for GraphQLDateDirective.
fix
Use @date(format: "YYYY-MM-DD") syntax in your schema SDL or field definition.
Upgrade
Version history
0.2.16latest on npm
Audit
Dependencies
graphqloptionalpeer dependency required to use SchemaDirectiveVisitor and schema directives
Agent activity
12 hits · last 30 days
node
12
Resources
graphql-custom-directives — npm install graphql-custom-directives · libregistry