Allows using @constraint as a directive to validate input data. Inspired by Constraints Directives RFC and OpenAPI.
This gem is an implementation of graphql-constraint-directive in Ruby.
Install the gem and add to the application's Gemfile by executing:
$ bundle add graphql-constraint-directive
This gem is an implemented as a plugin of graphql-ruby. Attach to your schema as follows:
class MySchema < GraphQL::Schema
use GraphQL::Constraint::Directive
end
Then, set the directive to the argument for which you want to set validation:
class CreateBook < BaseMutation
argument :title, String,
required: true,
directives: { GraphQL::Constraint::Directive::Constraint => { min_length: 1, max_length: 200 } }
field :book, Book, null: false
def resolve(title:)
# ...some codes
{
book: book
}
end
end
This will dump the following schema:
input CreateBookInput {
title: String! @constraint(minLength: 1, maxLength: 200)
}
type Mutation {
createBook(
input: CreateBookInput!
): CreateBookPayload
}
tips: You can use shorthand constraints:
option if you let your argument class include GraphQL::Constraint::Directive::ConstraintArgumentKeyword
helper.
# optional
# in your argument class
require 'graphql/constraint/directive/constraint_argument_keyword'
module Types
class BaseArgument < GraphQL::Schema::Argument
include GraphQL::Constraint::Directive::ConstraintArgumentKeyword
end
end
# optional
class CreateBook < BaseMutation
argument :title, String,
required: true,
# shorthand for `directives: { GraphQL::Constraint::Directive::Constraint =>...`
constraints: { min_length: 1, max_length: 200 }
@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
Warning
Unlike others, actual validation of pattern
is not implemented. You need to implement it on your application side.
@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
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/mh4gf/graphql-ruby-constraint-directive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Graphql::Ruby::Constraint::Directive project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.