{"id":47846,"library":"graphql-constraint-directive","title":"graphql-constraint-directive","description":"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.","status":"active","version":"6.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/confuser/graphql-constraint-directive","tags":["javascript","graphql","validate","validation","validator","directive","constraint","typescript"],"install":[{"cmd":"npm install graphql-constraint-directive","lang":"bash","label":"npm"},{"cmd":"yarn add graphql-constraint-directive","lang":"bash","label":"yarn"},{"cmd":"pnpm add graphql-constraint-directive","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for GraphQL schema manipulation","package":"graphql","optional":false},{"reason":"Required for makeExecutableSchema usage (most common setup)","package":"@graphql-tools/schema","optional":true},{"reason":"Required for Envelop plugin usage","package":"@envelop/core","optional":true},{"reason":"Required for Apollo Server 3 plugin usage","package":"apollo-server-plugin-base","optional":true}],"imports":[{"note":"ESM-only since v3; CommonJS require() still works but using .constraintDirective is error-prone. Prefer default import or destructure.","wrong":"const constraintDirective = require('graphql-constraint-directive').constraintDirective","symbol":"constraintDirective","correct":"import { constraintDirective } from 'graphql-constraint-directive'"},{"note":"TypeDefs are not a separate export path; they are named exports from the main module.","wrong":"import constraintDirectiveTypeDefs from 'graphql-constraint-directive/typeDefs'","symbol":"constraintDirectiveTypeDefs","correct":"import { constraintDirectiveTypeDefs } from 'graphql-constraint-directive'"},{"note":"Apollo 3 plugin is named createApolloQueryValidationPlugin; Apollo 4 plugin is createApollo4QueryValidationPlugin. Check version compatibility.","wrong":"const createApolloQueryValidationPlugin = require('graphql-constraint-directive').createApolloServer4Plugin","symbol":"createApolloQueryValidationPlugin","correct":"import { createApolloQueryValidationPlugin } from 'graphql-constraint-directive'"},{"note":"Exported as createEnvelopQueryValidationPlugin since v3.","wrong":"const createEnvelopQueryValidationPlugin = require('graphql-constraint-directive').envelopPlugin","symbol":"createEnvelopQueryValidationPlugin","correct":"import { createEnvelopQueryValidationPlugin } from 'graphql-constraint-directive'"}],"quickstart":{"code":"import { makeExecutableSchema } from '@graphql-tools/schema';\nimport { constraintDirectiveTypeDefs, constraintDirective } from 'graphql-constraint-directive';\n\nconst typeDefs = `\n  type Query {\n    user(id: ID!): User\n  }\n  type User {\n    name: String\n    email: String\n  }\n  type Mutation {\n    createUser(input: UserInput!): User\n  }\n  input UserInput {\n    name: String! @constraint(minLength: 1, maxLength: 100)\n    email: String! @constraint(format: \"email\")\n    age: Int @constraint(minimum: 0, maximum: 150)\n  }\n`;\n\nlet schema = makeExecutableSchema({\n  typeDefs: [constraintDirectiveTypeDefs, typeDefs],\n});\nschema = constraintDirective()(schema);\n\nexport { schema };","lang":"typescript","description":"Creates a GraphQL schema with @constraint validation on input fields using the schema wrapper approach."},"warnings":[{"fix":"Use import statements or update require() to destructure correctly: use require('graphql-constraint-directive') and access .constraintDirective (not .default).","message":"Version 3 changed to ESM-only exports. CommonJS require() still works but named exports are accessed differently.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Check Apollo Server version: use createApolloQueryValidationPlugin for Apollo 3, createApollo4QueryValidationPlugin for Apollo 4.","message":"Plugin API changed between major versions: Apollo 3 plugin is createApolloQueryValidationPlugin, Apollo 4 is createApollo4QueryValidationPlugin.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use named exports from main module. Remove dependency on 'graphql-constraint-directive/typeDefs' path.","message":"v2 branch uses old approach: constraintDirective exported as default and typeDefs as separate import. v3 deprecated that.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"If you need unmodified schema, use the plugin approach (Envelop or Apollo) instead of schema wrapper.","message":"Schema wrapper modifies scalars (Int, Float, String) to custom scalars; existing scalars may lose functionality like serialization.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Choose schema wrapper if you need response validation; otherwise use plugins for query-only validation.","message":"The directive only validates input fields; it does not validate output fields unless using schema wrapper (which validates both).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'14.0.0':76 '6.0.0':24 'apollo':51 'chang':31 'constraint':3,10,15,62,86 'current':20 'definit':79 'depend':74 'differenti':33 'direct':4,11,16,85 'ecosystem':30 'envelop':50 'etc':71 'field':8 'format':66 'frequent':26 'graphql':2,6,29,75,81 'graphql-constraint-direct':1 'incom':59 'input':7,43 'inspir':12 'javascript':80 'keep':53 'key':32 'like':63 'maximum':69 'maxlength':65 'minimum':68 'minlength':64 'modifi':39 'openapi':19 'pattern':70 'peer':73 'plugin':49 'queri':60 'regex':67 'releas':27 'requir':72 'respons':45 'rfc':17 'schema':37,40,54 'server':48 'ship':77 'stabl':21 'support':61 'track':28 'typescript':78,87 'unmodifi':55 'use':9 'valid':5,41,58,82,83,84 'version':22 'work':34 'wrapper':38","created_at":"2026-06-07T16:53:36.930575+00:00","updated_at":"2026-06-07T16:53:36.930575+00:00","problems":[{"fix":"Use import { constraintDirective } from 'graphql-constraint-directive' and then apply it: constraintDirective()(schema).","cause":"ESM import mistake - using default import instead of named import, or missing parentheses.","error":"TypeError: constraintDirective is not a function"},{"fix":"Add constraintDirectiveTypeDefs to the typeDefs array in makeExecutableSchema.","cause":"constraintDirectiveTypeDefs was not included in typeDefs array, so the directive definition is missing.","error":"Error: Query root type must be provided."},{"fix":"Use import { constraintDirectiveTypeDefs } from 'graphql-constraint-directive' instead of from subpath.","cause":"Importing from a subpath that was removed in v3.","error":"Cannot find module 'graphql-constraint-directive/typeDefs'"},{"fix":"Ensure constraintDirectiveTypeDefs is included in typeDefs array when calling makeExecutableSchema.","cause":"The schema does not include the constraint directive definition. Either constraintDirectiveTypeDefs is missing or the schema was built without it.","error":"Directive \"constraint\" is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/confuser/graphql-constraint-directive#readme","github":"ssh://git@github.com/confuser/graphql-constraint-directive","docs":null,"changelog":null,"pypi":null,"npm":"graphql-constraint-directive","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}