Registry / storage / graphql-constraint-directive

graphql-constraint-directive

JSON →
library6.0.0jsnpmunverified

Validate GraphQL input fields using @constraint directive, inspired by the Constraints Directives RFC and OpenAPI. Current stable version is 6.0.0, with frequent releases tracking GraphQL ecosystem changes. Key differentiator: works as a schema wrapper (modifies schema, validates both input and response) or as server plugins (Envelop, Apollo) that keep schema unmodified but only validate incoming queries. Supports constraints like minLength, maxLength, format (regex), minimum, maximum, pattern, etc. Requires peer dependency graphql >=14.0.0. Ships TypeScript definitions.

npm install graphql-constraint-directive
INSTALL
IMPORT
SIG · GRAPHQL-CONSTRAINT
G
graphql-constraint-directive
storagejavascriptv6.0.0
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.

constraintDirective
import { constraintDirective } from 'graphql-constraint-directive'
const constraintDirective = require('graphql-constraint-directive').constraintDirective
ESM-only since v3; CommonJS require() still works but using .constraintDirective is error-prone. Prefer default import or destructure.
constraintDirectiveTypeDefs
import { constraintDirectiveTypeDefs } from 'graphql-constraint-directive'
import constraintDirectiveTypeDefs from 'graphql-constraint-directive/typeDefs'
TypeDefs are not a separate export path; they are named exports from the main module.
createApolloQueryValidationPlugin
import { createApolloQueryValidationPlugin } from 'graphql-constraint-directive'
const createApolloQueryValidationPlugin = require('graphql-constraint-directive').createApolloServer4Plugin
Apollo 3 plugin is named createApolloQueryValidationPlugin; Apollo 4 plugin is createApollo4QueryValidationPlugin. Check version compatibility.
createEnvelopQueryValidationPlugin
import { createEnvelopQueryValidationPlugin } from 'graphql-constraint-directive'
const createEnvelopQueryValidationPlugin = require('graphql-constraint-directive').envelopPlugin
Exported as createEnvelopQueryValidationPlugin since v3.

Creates a GraphQL schema with @constraint validation on input fields using the schema wrapper approach.

import { makeExecutableSchema } from '@graphql-tools/schema'; import { constraintDirectiveTypeDefs, constraintDirective } from 'graphql-constraint-directive'; const typeDefs = ` type Query { user(id: ID!): User } type User { name: String email: String } type Mutation { createUser(input: UserInput!): User } input UserInput { name: String! @constraint(minLength: 1, maxLength: 100) email: String! @constraint(format: "email") age: Int @constraint(minimum: 0, maximum: 150) } `; let schema = makeExecutableSchema({ typeDefs: [constraintDirectiveTypeDefs, typeDefs], }); schema = constraintDirective()(schema); export { schema };
Debug
Known issues
breakingVersion 3 changed to ESM-only exports. CommonJS require() still works but named exports are accessed differently.
fix
Use import statements or update require() to destructure correctly: use require('graphql-constraint-directive') and access .constraintDirective (not .default).
affects: >=3.0.0
breakingPlugin API changed between major versions: Apollo 3 plugin is createApolloQueryValidationPlugin, Apollo 4 is createApollo4QueryValidationPlugin.
fix
Check Apollo Server version: use createApolloQueryValidationPlugin for Apollo 3, createApollo4QueryValidationPlugin for Apollo 4.
affects: >=4.0.0
deprecatedv2 branch uses old approach: constraintDirective exported as default and typeDefs as separate import. v3 deprecated that.
fix
Use named exports from main module. Remove dependency on 'graphql-constraint-directive/typeDefs' path.
affects: >=3.0.0
gotchaSchema wrapper modifies scalars (Int, Float, String) to custom scalars; existing scalars may lose functionality like serialization.
fix
If you need unmodified schema, use the plugin approach (Envelop or Apollo) instead of schema wrapper.
affects: >=1.0.0
gotchaThe directive only validates input fields; it does not validate output fields unless using schema wrapper (which validates both).
fix
Choose schema wrapper if you need response validation; otherwise use plugins for query-only validation.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: constraintDirective is not a function
ESM import mistake - using default import instead of named import, or missing parentheses.
fix
Use import { constraintDirective } from 'graphql-constraint-directive' and then apply it: constraintDirective()(schema).
Error: Query root type must be provided.
constraintDirectiveTypeDefs was not included in typeDefs array, so the directive definition is missing.
fix
Add constraintDirectiveTypeDefs to the typeDefs array in makeExecutableSchema.
Cannot find module 'graphql-constraint-directive/typeDefs'
Importing from a subpath that was removed in v3.
fix
Use import { constraintDirectiveTypeDefs } from 'graphql-constraint-directive' instead of from subpath.
Directive "constraint" is not defined
The schema does not include the constraint directive definition. Either constraintDirectiveTypeDefs is missing or the schema was built without it.
fix
Ensure constraintDirectiveTypeDefs is included in typeDefs array when calling makeExecutableSchema.
Upgrade
Version history
6.0.0latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency required for GraphQL schema manipulation
@graphql-tools/schemaoptionalRequired for makeExecutableSchema usage (most common setup)
@envelop/coreoptionalRequired for Envelop plugin usage
apollo-server-plugin-baseoptionalRequired for Apollo Server 3 plugin usage
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
graphql-constraint-directive — npm install graphql-constraint-directive · libregistry