Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the constraints option available #3

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ type Mutation {
}
```

*tips:* You can use shorthand `constraints:` option if you let your argument class include `GraphQL::Constraint::Directive::ConstraintArgumentKeyword` helper.

```ruby
# 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
```

```ruby
# optional
class CreateBook < BaseMutation
argument :title, String,
required: true,
# shorthand for `directives: { GraphQL::Constraint::Directive::Constraint =>...`
constraints: { min_length: 1, max_length: 200 }
```

## API
### String
#### minLength
Expand Down
23 changes: 23 additions & 0 deletions lib/graphql/constraint/directive/constraint_argument_keyword.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module GraphQL
module Constraint
module Directive
module ConstraintArgumentKeyword
def initialize(name, type, desc = nil, **kwargs)
if kwargs.key?(:constraints)
constraints = kwargs.delete(:constraints)
if constraints
directives = kwargs.delete(:directives) || {}
constraint_directive = GraphQL::Constraint::Directive::Constraint
directives[constraint_directive] = constraints
kwargs[:directives] = directives
end
end

super
end
end
end
end
end
Loading