diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-13 12:09:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-13 12:09:22 +0000 |
commit | 286fe61013674fe2d245ffc8d2233baf09923e70 (patch) | |
tree | 2037291f5863105e54e75be056b49f7d62007cae /doc/development/api_graphql_styleguide.md | |
parent | 4cb5e5011abfe8d50ac3a7ebd0018c563c6d7af4 (diff) | |
download | gitlab-ce-286fe61013674fe2d245ffc8d2233baf09923e70.tar.gz |
Add latest changes from gitlab-org/gitlab@master
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` |