graphql-schema-value-validation
v0.1.1
Published
Exposes custom directives for doing validation in your schema
Downloads
4
Readme
GraphQL schema value validation
This package provides custom directives for validating values in GraphQL schema. If value do not satisfy directive restriction, query will throw an error.
Install
npm install graphql-schema-value-validation
Directives list
Used directives should be added to schema, as listed below:
@length
directive @length(min: Int, max: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@notEmpty
directive @notEmpty on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@range
directive @range (min: Int, max: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
directive @range (min: Int, max: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@regex
directive @regex (pattern: String) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@date
directive @date on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@url
directive @url on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
@jwt
directive @jwt on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
Usage
Just import the implementation of the directive:
const {
LengthDirective
} = require('graphql-schema-value-validation');
then pass it to makeExecutableSchema
via the code block schemaDirectives
argument:
const schema = makeExecutableSchema({
typeDefs,
schemaDirectives: {
length: LengthDirective
},
resolvers
});
and use it in schema:
type Query {
books(title: String @length(min: 1, max: 12)): [Book]
}
type Book {
title: String
publishDate: String
publisherUrl: String
author: Author
}