-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4184 from rmosolgo/one-of-input-object
Add @OneOf for input objects
- Loading branch information
Showing
23 changed files
with
643 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
module GraphQL | ||
class Schema | ||
class Directive < GraphQL::Schema::Member | ||
class OneOf < GraphQL::Schema::Directive | ||
description "Requires that exactly one field must be supplied and that field must not be `null`." | ||
locations(GraphQL::Schema::Directive::INPUT_OBJECT) | ||
default_directive true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
lib/graphql/static_validation/rules/one_of_input_objects_are_valid.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
module GraphQL | ||
module StaticValidation | ||
module OneOfInputObjectsAreValid | ||
def on_input_object(node, parent) | ||
return super unless parent.is_a?(GraphQL::Language::Nodes::Argument) | ||
|
||
parent_type = get_parent_type(context, parent) | ||
return super unless parent_type && parent_type.kind.input_object? && parent_type.one_of? | ||
|
||
validate_one_of_input_object(node, context, parent_type) | ||
super | ||
end | ||
|
||
private | ||
|
||
def validate_one_of_input_object(ast_node, context, parent_type) | ||
present_fields = ast_node.arguments.map(&:name) | ||
input_object_type = parent_type.to_type_signature | ||
|
||
if present_fields.count != 1 | ||
add_error( | ||
OneOfInputObjectsAreValidError.new( | ||
"OneOf Input Object '#{input_object_type}' must specify exactly one key.", | ||
path: context.path, | ||
nodes: ast_node, | ||
input_object_type: input_object_type | ||
) | ||
) | ||
return | ||
end | ||
|
||
field = present_fields.first | ||
value = ast_node.arguments.first.value | ||
|
||
if value.is_a?(GraphQL::Language::Nodes::NullValue) | ||
add_error( | ||
OneOfInputObjectsAreValidError.new( | ||
"Argument '#{input_object_type}.#{field}' must be non-null.", | ||
path: [*context.path, field], | ||
nodes: ast_node.arguments.first, | ||
input_object_type: input_object_type | ||
) | ||
) | ||
return | ||
end | ||
|
||
if value.is_a?(GraphQL::Language::Nodes::VariableIdentifier) | ||
variable_name = value.name | ||
variable_type = @declared_variables[variable_name].type | ||
|
||
unless variable_type.is_a?(GraphQL::Language::Nodes::NonNullType) | ||
add_error( | ||
OneOfInputObjectsAreValidError.new( | ||
"Variable '#{variable_name}' must be non-nullable to be used for OneOf Input Object '#{input_object_type}'.", | ||
path: [*context.path, field], | ||
nodes: ast_node, | ||
input_object_type: input_object_type | ||
) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
lib/graphql/static_validation/rules/one_of_input_objects_are_valid_error.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
module GraphQL | ||
module StaticValidation | ||
class OneOfInputObjectsAreValidError < StaticValidation::Error | ||
attr_reader :input_object_type | ||
|
||
def initialize(message, path:, nodes:, input_object_type:) | ||
super(message, path: path, nodes: nodes) | ||
@input_object_type = input_object_type | ||
end | ||
|
||
# A hash representation of this Message | ||
def to_h | ||
extensions = { | ||
"code" => code, | ||
"inputObjectType" => input_object_type | ||
} | ||
|
||
super.merge({ | ||
"extensions" => extensions | ||
}) | ||
end | ||
|
||
def code | ||
"invalidOneOfInputObject" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.