![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
apollo-server-v2-constraint-directive
Advanced tools
Check the GraphQL input fields with directives
Allows using @constraint as a directive to validate input and output data. This module is for Apollo Server, and support the latest Apollo Server version 2.
It is mainly based on the module from graphql-constraint-directive, which is for Apollo version 1 only. This module is an Inspired by Constraints Directives RFC and OpenAPI
If you are looking for a playground checkout this project apollo-server-playground.
npm install apollo-server-v2-constraint-directive
const { ApolloServer, makeExecutableSchema, gql } = require('apollo-server')
const ConstraintDirective = require('apollo-server-constraint-directive')
const schemaDirectives = {
constraint: ConstraintDirective,
};
const typeDefs = gql`
scalar ValidateString
scalar ValidateNumber
directive @constraint(
# String constraints
minLength: Int
maxLength: Int
startsWith: String
endsWith: String
notContains: String
pattern: String
format: String
# Number constraints
min: Int
max: Int
exclusiveMin: Int
exclusiveMax: Int
multipleOf: Int
) on INPUT_FIELD_DEFINITION
type Query {
books: [Book]
}
type Book {
title: String
}
type Mutation {
createBook(input: BookInput): Book
}
input BookInput {
title: String! @constraint(minLength: 5, format: "email")
}`
// The ApolloServer constructor
const server = new ApolloServer({
schema: makeExecutableSchema({ typeDefs, schemaDirectives })
})
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
@constraint(minLength: 5)
Restrict to a minimum length
@constraint(maxLength: 5)
Restrict to a maximum length
@constraint(startsWith: "foo")
Ensure value starts with foo
@constraint(endsWith: "foo")
Ensure value ends with foo
@constraint(contains: "foo")
Ensure value contains foo
@constraint(notContains: "foo")
Ensure value does not contain foo
@constraint(pattern: "^[0-9a-zA-Z]*$")
Ensure value matches regex, e.g. alphanumeric
@constraint(format: "email")
Ensure value is in a particular format
Supported formats:
@constraint(passwordScore: 3)
Ensure password value has estimated strength. zxcvbn is used under the hood. Possible strength values are between 1 and 5. Heigher is better
@constraint(min: 3)
Ensure value is greater than or equal to
@constraint(max: 3)
Ensure value is less than or equal to
@constraint(exclusiveMin: 3)
Ensure value is greater than
@constraint(exclusiveMax: 3)
Ensure value is less than
@constraint(multipleOf: 10)
Ensure value is a multiple
Each validation error throws a ConstraintDirectiveError
. Combined with a formatError function, this can be used to customise error messages.
{
code: 'BAD_USER_INPUT',
fieldName: 'theFieldName',
context: [ { arg: 'argument name which failed', value: 'value of argument' } ]
}
const formatError = function (error) {
if (error.originalError && error.originalError && error.originalError.originalError && error.originalError.originalError.code === 'BAD_USER_INPUT') {
// return a custom object
}
return error
}
const apollo = new ApolloServer({
schema: makeExecutableSchema({ typeDefs, schemaDirectives }),
formatError
});
FAQs
Check the GraphQL input fields with directives
The npm package apollo-server-v2-constraint-directive receives a total of 2 weekly downloads. As such, apollo-server-v2-constraint-directive popularity was classified as not popular.
We found that apollo-server-v2-constraint-directive demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.