diff options
Diffstat (limited to 'doc/development/api_graphql_styleguide.md')
-rw-r--r-- | doc/development/api_graphql_styleguide.md | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/doc/development/api_graphql_styleguide.md b/doc/development/api_graphql_styleguide.md index 9c59a12bfb3..7d884737f5b 100644 --- a/doc/development/api_graphql_styleguide.md +++ b/doc/development/api_graphql_styleguide.md @@ -636,6 +636,37 @@ found, we should raise a `Gitlab::Graphql::Errors::ResourceNotAvailable` error. Which will be correctly rendered to the clients. +## Validating arguments + +For validations of single arguments, use the +[`prepare` option](https://github.com/rmosolgo/graphql-ruby/blob/master/guides/fields/arguments.md) +as normal. + +Sometimes a mutation or resolver may accept a number of optional +arguments, but still want to validate that at least one of the optional +arguments were given. In this situation, consider using the `#ready?` +method within your mutation or resolver to provide the validation. The +`#ready?` method will be called before any work is done within the +`#resolve` method. + +Example: + +```ruby +def ready?(**args) + if args.values_at(:body, :position).compact.blank? + raise Gitlab::Graphql::Errors::ArgumentError, + 'body or position arguments are required' + end + + # Always remember to call `#super` + super(args) +end +``` + +In the future this may be able to be done using `InputUnions` if +[this RFC](https://github.com/graphql/graphql-spec/blob/master/rfcs/InputUnion.md) +is merged. + ## GitLab's custom scalars ### `Types::TimeType` |