graphql 0.12.1 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql.rb +31 -41
- data/lib/graphql/argument.rb +23 -21
- data/lib/graphql/base_type.rb +5 -8
- data/lib/graphql/define/assign_argument.rb +5 -2
- data/lib/graphql/define/type_definer.rb +2 -1
- data/lib/graphql/directive.rb +34 -36
- data/lib/graphql/directive/include_directive.rb +3 -7
- data/lib/graphql/directive/skip_directive.rb +3 -7
- data/lib/graphql/enum_type.rb +78 -76
- data/lib/graphql/execution_error.rb +1 -3
- data/lib/graphql/field.rb +99 -95
- data/lib/graphql/input_object_type.rb +49 -47
- data/lib/graphql/interface_type.rb +31 -34
- data/lib/graphql/introspection.rb +19 -18
- data/lib/graphql/introspection/directive_location_enum.rb +8 -0
- data/lib/graphql/introspection/directive_type.rb +1 -3
- data/lib/graphql/introspection/field_type.rb +1 -1
- data/lib/graphql/introspection/fields_field.rb +1 -1
- data/lib/graphql/introspection/introspection_query.rb +1 -3
- data/lib/graphql/introspection/possible_types_field.rb +7 -1
- data/lib/graphql/introspection/schema_field.rb +13 -9
- data/lib/graphql/introspection/type_by_name_field.rb +13 -17
- data/lib/graphql/introspection/typename_field.rb +12 -8
- data/lib/graphql/language.rb +5 -9
- data/lib/graphql/language/lexer.rb +668 -0
- data/lib/graphql/language/lexer.rl +149 -0
- data/lib/graphql/language/parser.rb +842 -116
- data/lib/graphql/language/parser.y +264 -0
- data/lib/graphql/language/token.rb +21 -0
- data/lib/graphql/list_type.rb +33 -31
- data/lib/graphql/non_null_type.rb +33 -31
- data/lib/graphql/object_type.rb +52 -55
- data/lib/graphql/query.rb +83 -80
- data/lib/graphql/query/context.rb +5 -1
- data/lib/graphql/query/directive_resolution.rb +16 -0
- data/lib/graphql/query/executor.rb +3 -3
- data/lib/graphql/query/input_validation_result.rb +17 -15
- data/lib/graphql/query/serial_execution.rb +5 -5
- data/lib/graphql/query/serial_execution/execution_context.rb +4 -3
- data/lib/graphql/query/serial_execution/selection_resolution.rb +19 -21
- data/lib/graphql/query/serial_execution/value_resolution.rb +1 -1
- data/lib/graphql/query/type_resolver.rb +22 -18
- data/lib/graphql/query/variable_validation_error.rb +14 -12
- data/lib/graphql/schema.rb +87 -77
- data/lib/graphql/schema/each_item_validator.rb +16 -12
- data/lib/graphql/schema/field_validator.rb +14 -10
- data/lib/graphql/schema/implementation_validator.rb +26 -22
- data/lib/graphql/schema/middleware_chain.rb +2 -1
- data/lib/graphql/schema/possible_types.rb +34 -0
- data/lib/graphql/schema/printer.rb +122 -120
- data/lib/graphql/schema/type_expression.rb +1 -0
- data/lib/graphql/schema/type_map.rb +3 -10
- data/lib/graphql/schema/type_reducer.rb +65 -81
- data/lib/graphql/schema/type_validator.rb +45 -41
- data/lib/graphql/static_validation.rb +7 -9
- data/lib/graphql/static_validation/all_rules.rb +29 -24
- data/lib/graphql/static_validation/arguments_validator.rb +39 -35
- data/lib/graphql/static_validation/literal_validator.rb +44 -40
- data/lib/graphql/static_validation/message.rb +30 -26
- data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +15 -11
- data/lib/graphql/static_validation/rules/arguments_are_defined.rb +14 -10
- data/lib/graphql/static_validation/rules/directives_are_defined.rb +16 -12
- data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +59 -0
- data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +25 -21
- data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +28 -24
- data/lib/graphql/static_validation/rules/fields_will_merge.rb +84 -80
- data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +49 -43
- data/lib/graphql/static_validation/rules/fragment_types_exist.rb +22 -17
- data/lib/graphql/static_validation/rules/fragments_are_finite.rb +19 -15
- data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +25 -20
- data/lib/graphql/static_validation/rules/fragments_are_used.rb +36 -23
- data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +29 -25
- data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +21 -17
- data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +79 -70
- data/lib/graphql/static_validation/rules/variables_are_input_types.rb +24 -20
- data/lib/graphql/static_validation/rules/variables_are_used_and_defined.rb +122 -119
- data/lib/graphql/static_validation/type_stack.rb +138 -129
- data/lib/graphql/static_validation/validator.rb +29 -25
- data/lib/graphql/type_kinds.rb +42 -40
- data/lib/graphql/union_type.rb +22 -16
- data/lib/graphql/version.rb +1 -1
- data/readme.md +12 -27
- data/spec/graphql/base_type_spec.rb +3 -3
- data/spec/graphql/directive_spec.rb +10 -18
- data/spec/graphql/enum_type_spec.rb +7 -7
- data/spec/graphql/execution_error_spec.rb +1 -1
- data/spec/graphql/field_spec.rb +14 -13
- data/spec/graphql/id_type_spec.rb +6 -6
- data/spec/graphql/input_object_type_spec.rb +39 -39
- data/spec/graphql/interface_type_spec.rb +16 -32
- data/spec/graphql/introspection/directive_type_spec.rb +5 -9
- data/spec/graphql/introspection/input_value_type_spec.rb +10 -4
- data/spec/graphql/introspection/introspection_query_spec.rb +2 -2
- data/spec/graphql/introspection/schema_type_spec.rb +2 -2
- data/spec/graphql/introspection/type_type_spec.rb +34 -6
- data/spec/graphql/language/parser_spec.rb +299 -105
- data/spec/graphql/language/visitor_spec.rb +4 -4
- data/spec/graphql/list_type_spec.rb +11 -11
- data/spec/graphql/object_type_spec.rb +10 -10
- data/spec/graphql/query/arguments_spec.rb +7 -7
- data/spec/graphql/query/context_spec.rb +11 -3
- data/spec/graphql/query/executor_spec.rb +26 -19
- data/spec/graphql/query/serial_execution/execution_context_spec.rb +6 -6
- data/spec/graphql/query/serial_execution/value_resolution_spec.rb +2 -2
- data/spec/graphql/query/type_resolver_spec.rb +3 -3
- data/spec/graphql/query_spec.rb +6 -38
- data/spec/graphql/scalar_type_spec.rb +28 -19
- data/spec/graphql/schema/field_validator_spec.rb +1 -1
- data/spec/graphql/schema/middleware_chain_spec.rb +12 -1
- data/spec/graphql/schema/printer_spec.rb +12 -4
- data/spec/graphql/schema/rescue_middleware_spec.rb +1 -1
- data/spec/graphql/schema/type_expression_spec.rb +2 -2
- data/spec/graphql/schema/type_reducer_spec.rb +21 -36
- data/spec/graphql/schema/type_validator_spec.rb +9 -9
- data/spec/graphql/schema_spec.rb +1 -1
- data/spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/arguments_are_defined_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/directives_are_defined_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb +39 -0
- data/spec/graphql/static_validation/rules/fields_are_defined_on_type_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/fields_have_appropriate_selections_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragment_spreads_are_possible_spec.rb +1 -1
- data/spec/graphql/static_validation/rules/fragment_types_exist_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_finite_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_on_composite_types_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_used_spec.rb +3 -3
- data/spec/graphql/static_validation/rules/required_arguments_are_present_spec.rb +3 -3
- data/spec/graphql/static_validation/rules/variable_default_values_are_correctly_typed_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/variable_usages_are_allowed_spec.rb +3 -1
- data/spec/graphql/static_validation/rules/variables_are_input_types_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb +3 -3
- data/spec/graphql/static_validation/type_stack_spec.rb +3 -2
- data/spec/graphql/static_validation/validator_spec.rb +26 -6
- data/spec/graphql/union_type_spec.rb +5 -4
- data/spec/spec_helper.rb +2 -5
- data/spec/support/dairy_app.rb +30 -9
- data/spec/support/dairy_data.rb +1 -1
- data/spec/support/star_wars_data.rb +26 -26
- data/spec/support/star_wars_schema.rb +1 -1
- metadata +40 -21
- data/lib/graphql/language/transform.rb +0 -113
- data/lib/graphql/query/directive_chain.rb +0 -44
- data/lib/graphql/repl.rb +0 -27
- data/spec/graphql/language/transform_spec.rb +0 -156
@@ -1,50 +1,56 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class FragmentSpreadsArePossible
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
5
|
+
|
6
|
+
def validate(context)
|
7
|
+
|
8
|
+
context.visitor[GraphQL::Language::Nodes::InlineFragment] << -> (node, parent) {
|
9
|
+
fragment_parent = context.object_types[-2]
|
10
|
+
fragment_child = context.object_types.last
|
11
|
+
if fragment_child
|
12
|
+
validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
|
13
|
+
end
|
14
|
+
}
|
15
|
+
|
16
|
+
spreads_to_validate = []
|
17
|
+
|
18
|
+
context.visitor[GraphQL::Language::Nodes::FragmentSpread] << -> (node, parent) {
|
19
|
+
fragment_parent = context.object_types.last
|
20
|
+
spreads_to_validate << [node, fragment_parent]
|
21
|
+
}
|
22
|
+
|
23
|
+
context.visitor[GraphQL::Language::Nodes::Document].leave << -> (node, parent) {
|
24
|
+
spreads_to_validate.each do |spread_values|
|
25
|
+
node, fragment_parent = spread_values
|
26
|
+
fragment_child_name = context.fragments[node.name].type
|
27
|
+
fragment_child = context.schema.types[fragment_child_name]
|
28
|
+
validate_fragment_in_scope(fragment_parent, fragment_child, node, context)
|
29
|
+
end
|
30
|
+
}
|
25
31
|
end
|
26
|
-
}
|
27
|
-
end
|
28
32
|
|
29
|
-
|
33
|
+
private
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
def validate_fragment_in_scope(parent_type, child_type, node, context)
|
36
|
+
intersecting_types = get_possible_types(parent_type, context.schema) & get_possible_types(child_type, context.schema)
|
37
|
+
if intersecting_types.none?
|
38
|
+
name = node.respond_to?(:name) ? " #{node.name}" : ""
|
39
|
+
context.errors << message("Fragment#{name} on #{child_type.name} can't be spread inside #{parent_type.name}", node)
|
40
|
+
end
|
41
|
+
end
|
38
42
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
def get_possible_types(type, schema)
|
44
|
+
if type.kind.wraps?
|
45
|
+
get_possible_types(type.of_type, schema)
|
46
|
+
elsif type.kind.object?
|
47
|
+
[type]
|
48
|
+
elsif type.kind.resolves?
|
49
|
+
schema.possible_types(type)
|
50
|
+
else
|
51
|
+
[]
|
52
|
+
end
|
53
|
+
end
|
48
54
|
end
|
49
55
|
end
|
50
56
|
end
|
@@ -1,24 +1,29 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class FragmentTypesExist
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
FRAGMENTS_ON_TYPES = [
|
7
|
+
GraphQL::Language::Nodes::FragmentDefinition,
|
8
|
+
GraphQL::Language::Nodes::InlineFragment,
|
9
|
+
]
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def validate(context)
|
12
|
+
FRAGMENTS_ON_TYPES.each do |node_class|
|
13
|
+
context.visitor[node_class] << -> (node, parent) { validate_type_exists(node, context) }
|
14
|
+
end
|
15
|
+
end
|
14
16
|
|
15
|
-
|
17
|
+
private
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
def validate_type_exists(node, context)
|
20
|
+
return unless node.type
|
21
|
+
type = context.schema.types.fetch(node.type, nil)
|
22
|
+
if type.nil?
|
23
|
+
context.errors << message("No such type #{node.type}, so it can't be a fragment condition", node)
|
24
|
+
GraphQL::Language::Visitor::SKIP
|
25
|
+
end
|
26
|
+
end
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|
@@ -1,23 +1,27 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class FragmentsAreFinite
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def validate(context)
|
7
|
+
context.visitor[GraphQL::Language::Nodes::FragmentDefinition] << -> (node, parent) {
|
8
|
+
if has_nested_spread(node, [], context)
|
9
|
+
context.errors << message("Fragment #{node.name} contains an infinite loop", node)
|
10
|
+
end
|
11
|
+
}
|
8
12
|
end
|
9
|
-
}
|
10
|
-
end
|
11
13
|
|
12
|
-
|
14
|
+
private
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
def has_nested_spread(fragment_def, parent_fragment_names, context)
|
17
|
+
nested_spreads = fragment_def.selections
|
18
|
+
.select {|f| f.is_a?(GraphQL::Language::Nodes::FragmentSpread)}
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
nested_spreads.any? do |spread|
|
21
|
+
nested_def = context.fragments[spread.name]
|
22
|
+
parent_fragment_names.include?(spread.name) || has_nested_spread(nested_def, parent_fragment_names + [fragment_def.name], context)
|
23
|
+
end
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
@@ -1,27 +1,32 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class FragmentsAreOnCompositeTypes
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
HAS_TYPE_CONDITION = [
|
7
|
+
GraphQL::Language::Nodes::FragmentDefinition,
|
8
|
+
GraphQL::Language::Nodes::InlineFragment,
|
9
|
+
]
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def validate(context)
|
12
|
+
HAS_TYPE_CONDITION.each do |node_class|
|
13
|
+
context.visitor[node_class] << -> (node, parent) {
|
14
|
+
validate_type_is_composite(node, context)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
16
18
|
|
17
|
-
|
19
|
+
private
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def validate_type_is_composite(node, context)
|
22
|
+
type_name = node.type
|
23
|
+
return unless type_name
|
24
|
+
type_def = context.schema.types[type_name]
|
25
|
+
if type_def.nil? || !type_def.kind.composite?
|
26
|
+
context.errors << message("Invalid fragment on type #{type_name} (must be Union, Interface or Object)", node)
|
27
|
+
GraphQL::Language::Visitor::SKIP
|
28
|
+
end
|
29
|
+
end
|
25
30
|
end
|
26
31
|
end
|
27
32
|
end
|
@@ -1,30 +1,43 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class FragmentsAreUsed
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
v[GraphQL::Language::Nodes::FragmentSpread] << -> (node, parent) { used_fragments << node }
|
9
|
-
v[GraphQL::Language::Nodes::FragmentDefinition] << -> (node, parent) { defined_fragments << node}
|
10
|
-
v[GraphQL::Language::Nodes::Document].leave << -> (node, parent) { add_errors(context.errors, used_fragments, defined_fragments) }
|
11
|
-
end
|
6
|
+
def validate(context)
|
7
|
+
v = context.visitor
|
8
|
+
used_fragments = []
|
9
|
+
defined_fragments = []
|
12
10
|
|
13
|
-
|
11
|
+
v[GraphQL::Language::Nodes::Document] << -> (node, parent) {
|
12
|
+
defined_fragments = node.definitions.select { |defn| defn.is_a?(GraphQL::Language::Nodes::FragmentDefinition) }
|
13
|
+
}
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
v[GraphQL::Language::Nodes::FragmentSpread] << -> (node, parent) {
|
16
|
+
used_fragments << node
|
17
|
+
if defined_fragments.none? { |defn| defn.name == node.name }
|
18
|
+
GraphQL::Language::Visitor::SKIP
|
19
|
+
end
|
20
|
+
}
|
21
|
+
v[GraphQL::Language::Nodes::Document].leave << -> (node, parent) { add_errors(context.errors, used_fragments, defined_fragments) }
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
unused_fragments.each do |fragment|
|
23
|
-
errors << message("Fragment #{fragment.name} was defined, but not used", fragment)
|
24
|
-
end
|
25
|
-
end
|
24
|
+
private
|
26
25
|
|
27
|
-
|
28
|
-
|
26
|
+
def add_errors(errors, used_fragments, defined_fragments)
|
27
|
+
undefined_fragments = find_difference(used_fragments, defined_fragments.map(&:name))
|
28
|
+
undefined_fragments.each do |fragment|
|
29
|
+
errors << message("Fragment #{fragment.name} was used, but not defined", fragment)
|
30
|
+
end
|
31
|
+
|
32
|
+
unused_fragments = find_difference(defined_fragments, used_fragments.map(&:name))
|
33
|
+
unused_fragments.each do |fragment|
|
34
|
+
errors << message("Fragment #{fragment.name} was defined, but not used", fragment)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_difference(fragments, allowed_fragment_names)
|
39
|
+
fragments.select {|f| !allowed_fragment_names.include?(f.name) }
|
40
|
+
end
|
41
|
+
end
|
29
42
|
end
|
30
43
|
end
|
@@ -1,34 +1,38 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class RequiredArgumentsArePresent
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def validate(context)
|
7
|
+
v = context.visitor
|
8
|
+
v[GraphQL::Language::Nodes::Field] << -> (node, parent) { validate_field(node, context) }
|
9
|
+
v[GraphQL::Language::Nodes::Directive] << -> (node, parent) { validate_directive(node, context) }
|
10
|
+
end
|
9
11
|
|
10
|
-
|
12
|
+
private
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def validate_directive(ast_directive, context)
|
15
|
+
directive_defn = context.schema.directives[ast_directive.name]
|
16
|
+
assert_required_args(ast_directive, directive_defn, context)
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
def validate_field(ast_field, context)
|
20
|
+
return if context.skip_field?(ast_field.name)
|
21
|
+
defn = context.field_definition
|
22
|
+
assert_required_args(ast_field, defn, context)
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def assert_required_args(ast_node, defn, context)
|
26
|
+
present_argument_names = ast_node.arguments.map(&:name)
|
27
|
+
required_argument_names = defn.arguments.values
|
28
|
+
.select { |a| a.type.kind.non_null? }
|
29
|
+
.map(&:name)
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
missing_names = required_argument_names - present_argument_names
|
32
|
+
if missing_names.any?
|
33
|
+
context.errors << message("#{ast_node.class.name.split("::").last} '#{ast_node.name}' is missing required arguments: #{missing_names.join(", ")}", ast_node)
|
34
|
+
end
|
35
|
+
end
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
@@ -1,23 +1,27 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class VariableDefaultValuesAreCorrectlyTyped
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def validate(context)
|
7
|
+
literal_validator = GraphQL::StaticValidation::LiteralValidator.new
|
8
|
+
context.visitor[GraphQL::Language::Nodes::VariableDefinition] << -> (node, parent) {
|
9
|
+
if !node.default_value.nil?
|
10
|
+
validate_default_value(node, literal_validator, context)
|
11
|
+
end
|
12
|
+
}
|
9
13
|
end
|
10
|
-
}
|
11
|
-
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def validate_default_value(node, literal_validator, context)
|
16
|
+
value = node.default_value
|
17
|
+
if node.type.is_a?(GraphQL::Language::Nodes::NonNullType)
|
18
|
+
context.errors << message("Non-null variable $#{node.name} can't have a default value", node)
|
19
|
+
else
|
20
|
+
type = context.schema.type_from_ast(node.type)
|
21
|
+
if !literal_validator.validate(value, type)
|
22
|
+
context.errors << message("Default value for $#{node.name} doesn't match type #{type}", node)
|
23
|
+
end
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
@@ -1,86 +1,95 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module GraphQL
|
2
|
+
module StaticValidation
|
3
|
+
class VariableUsagesAreAllowed
|
4
|
+
include GraphQL::StaticValidation::Message::MessageHelper
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
def validate(context)
|
7
|
+
# holds { name => ast_node } pairs
|
8
|
+
declared_variables = {}
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
context.visitor[GraphQL::Language::Nodes::OperationDefinition] << -> (node, parent) {
|
11
|
+
declared_variables = node.variables.each_with_object({}) { |var, memo| memo[var.name] = var }
|
12
|
+
}
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
context.visitor[GraphQL::Language::Nodes::Argument] << -> (node, parent) {
|
15
|
+
return if !node.value.is_a?(GraphQL::Language::Nodes::VariableIdentifier)
|
16
|
+
if parent.is_a?(GraphQL::Language::Nodes::Field)
|
17
|
+
arguments = context.field_definition.arguments
|
18
|
+
elsif parent.is_a?(GraphQL::Language::Nodes::Directive)
|
19
|
+
arguments = context.directive_definition.arguments
|
20
|
+
elsif parent.is_a?(GraphQL::Language::Nodes::InputObject)
|
21
|
+
arguments = context.argument_definition.type.unwrap.input_fields
|
22
|
+
else
|
23
|
+
raise("Unexpected argument parent: #{parent}")
|
24
|
+
end
|
25
|
+
var_defn_ast = declared_variables[node.value.name]
|
26
|
+
# Might be undefined :(
|
27
|
+
# VariablesAreUsedAndDefined can't finalize its search until the end of the document.
|
28
|
+
var_defn_ast && validate_usage(arguments, node, var_defn_ast, context)
|
29
|
+
}
|
22
30
|
end
|
23
|
-
var_defn_ast = declared_variables[node.value.name]
|
24
|
-
# Might be undefined :(
|
25
|
-
# VariablesAreUsedAndDefined can't finalize its search until the end of the document.
|
26
|
-
var_defn_ast && validate_usage(arguments, node, var_defn_ast, context)
|
27
|
-
}
|
28
|
-
end
|
29
31
|
|
30
|
-
|
32
|
+
private
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
def validate_usage(arguments, arg_node, ast_var, context)
|
35
|
+
var_type = to_query_type(ast_var.type, context.schema.types)
|
36
|
+
if !ast_var.default_value.nil?
|
37
|
+
var_type = GraphQL::NonNullType.new(of_type: var_type)
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
+
arg_defn = arguments[arg_node.name]
|
41
|
+
arg_defn_type = arg_defn.type
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
+
var_inner_type = var_type.unwrap
|
44
|
+
arg_inner_type = arg_defn_type.unwrap
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
if var_inner_type != arg_inner_type
|
47
|
+
context.errors << create_error("Type mismatch", var_type, ast_var, arg_defn, arg_node)
|
48
|
+
elsif list_dimension(var_type) != list_dimension(arg_defn_type)
|
49
|
+
context.errors << create_error("List dimension mismatch", var_type, ast_var, arg_defn, arg_node)
|
50
|
+
elsif !non_null_levels_match(arg_defn_type, var_type)
|
51
|
+
context.errors << create_error("Nullability mismatch", var_type, ast_var, arg_defn, arg_node)
|
52
|
+
end
|
53
|
+
end
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
def to_query_type(ast_type, types)
|
56
|
+
if ast_type.is_a?(GraphQL::Language::Nodes::NonNullType)
|
57
|
+
GraphQL::NonNullType.new(of_type: to_query_type(ast_type.of_type, types))
|
58
|
+
elsif ast_type.is_a?(GraphQL::Language::Nodes::ListType)
|
59
|
+
GraphQL::ListType.new(of_type: to_query_type(ast_type.of_type, types))
|
60
|
+
else
|
61
|
+
types[ast_type.name]
|
62
|
+
end
|
63
|
+
end
|
62
64
|
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
def create_error(error_message, var_type, ast_var, arg_defn, arg_node)
|
66
|
+
message("#{error_message} on variable $#{ast_var.name} and argument #{arg_node.name} (#{var_type.to_s} / #{arg_defn.type.to_s})", arg_node)
|
67
|
+
end
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
def list_dimension(type)
|
70
|
+
if type.kind.list?
|
71
|
+
1 + list_dimension(type.of_type)
|
72
|
+
elsif type.kind.non_null?
|
73
|
+
list_dimension(type.of_type)
|
74
|
+
else
|
75
|
+
0
|
76
|
+
end
|
77
|
+
end
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
79
|
+
def non_null_levels_match(arg_type, var_type)
|
80
|
+
if arg_type.kind.non_null? && !var_type.kind.non_null?
|
81
|
+
false
|
82
|
+
elsif arg_type.kind.wraps? && var_type.kind.wraps?
|
83
|
+
# If var_type is a non-null wrapper for a type, and arg_type is nullable, peel off the wrapper
|
84
|
+
# That way, a var_type of `[DairyAnimal]!` works with an arg_type of `[DairyAnimal]`
|
85
|
+
if var_type.kind.non_null? && !arg_type.kind.non_null?
|
86
|
+
var_type = var_type.of_type
|
87
|
+
end
|
88
|
+
non_null_levels_match(arg_type.of_type, var_type.of_type)
|
89
|
+
else
|
90
|
+
true
|
91
|
+
end
|
92
|
+
end
|
84
93
|
end
|
85
94
|
end
|
86
95
|
end
|