Skip to content

Commit d52c0a8

Browse files
committed
Alignments with changes for branch coverage
Replicates graphql/graphql-js@e590dd2
1 parent 56cfc71 commit d52c0a8

File tree

5 files changed

+110
-129
lines changed

5 files changed

+110
-129
lines changed

src/graphql/utilities/extend_schema.py

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ def get_operation_types(
380380
return {
381381
operation_type.operation: get_named_type(operation_type.type)
382382
for node in nodes
383-
if node.operation_types
384-
for operation_type in node.operation_types
383+
for operation_type in node.operation_types or []
385384
}
386385

387386
# noinspection PyShadowingNames
@@ -426,38 +425,34 @@ def build_field_map(
426425
) -> GraphQLFieldMap:
427426
field_map: GraphQLFieldMap = {}
428427
for node in nodes:
429-
if node.fields:
430-
for field in node.fields:
431-
# Note: While this could make assertions to get the correctly typed
432-
# value, that would throw immediately while type system validation
433-
# with validate_schema() will produce more actionable results.
434-
field_map[field.name.value] = GraphQLField(
435-
type_=cast(GraphQLOutputType, get_wrapped_type(field.type)),
436-
description=field.description.value
437-
if field.description
438-
else None,
439-
args=build_argument_map(field.arguments),
440-
deprecation_reason=get_deprecation_reason(field),
441-
ast_node=field,
442-
)
428+
for field in node.fields or []:
429+
# Note: While this could make assertions to get the correctly typed
430+
# value, that would throw immediately while type system validation
431+
# with validate_schema() will produce more actionable results.
432+
field_map[field.name.value] = GraphQLField(
433+
type_=cast(GraphQLOutputType, get_wrapped_type(field.type)),
434+
description=field.description.value if field.description else None,
435+
args=build_argument_map(field.arguments),
436+
deprecation_reason=get_deprecation_reason(field),
437+
ast_node=field,
438+
)
443439
return field_map
444440

445441
def build_argument_map(
446442
args: Optional[Collection[InputValueDefinitionNode]],
447443
) -> GraphQLArgumentMap:
448444
arg_map: GraphQLArgumentMap = {}
449-
if args:
450-
for arg in args:
451-
# Note: While this could make assertions to get the correctly typed
452-
# value, that would throw immediately while type system validation
453-
# with validate_schema() will produce more actionable results.
454-
type_ = cast(GraphQLInputType, get_wrapped_type(arg.type))
455-
arg_map[arg.name.value] = GraphQLArgument(
456-
type_=type_,
457-
description=arg.description.value if arg.description else None,
458-
default_value=value_from_ast(arg.default_value, type_),
459-
ast_node=arg,
460-
)
445+
for arg in args or []:
446+
# Note: While this could make assertions to get the correctly typed
447+
# value, that would throw immediately while type system validation
448+
# with validate_schema() will produce more actionable results.
449+
type_ = cast(GraphQLInputType, get_wrapped_type(arg.type))
450+
arg_map[arg.name.value] = GraphQLArgument(
451+
type_=type_,
452+
description=arg.description.value if arg.description else None,
453+
default_value=value_from_ast(arg.default_value, type_),
454+
ast_node=arg,
455+
)
461456
return arg_map
462457

463458
def build_input_field_map(
@@ -467,39 +462,33 @@ def build_input_field_map(
467462
) -> GraphQLInputFieldMap:
468463
input_field_map: GraphQLInputFieldMap = {}
469464
for node in nodes:
470-
if node.fields:
471-
for field in node.fields:
472-
# Note: While this could make assertions to get the correctly typed
473-
# value, that would throw immediately while type system validation
474-
# with validate_schema() will produce more actionable results.
475-
type_ = cast(GraphQLInputType, get_wrapped_type(field.type))
476-
input_field_map[field.name.value] = GraphQLInputField(
477-
type_=type_,
478-
description=field.description.value
479-
if field.description
480-
else None,
481-
default_value=value_from_ast(field.default_value, type_),
482-
ast_node=field,
483-
)
465+
for field in node.fields or []:
466+
# Note: While this could make assertions to get the correctly typed
467+
# value, that would throw immediately while type system validation
468+
# with validate_schema() will produce more actionable results.
469+
type_ = cast(GraphQLInputType, get_wrapped_type(field.type))
470+
input_field_map[field.name.value] = GraphQLInputField(
471+
type_=type_,
472+
description=field.description.value if field.description else None,
473+
default_value=value_from_ast(field.default_value, type_),
474+
ast_node=field,
475+
)
484476
return input_field_map
485477

486478
def build_enum_value_map(
487479
nodes: Collection[Union[EnumTypeDefinitionNode, EnumTypeExtensionNode]]
488480
) -> GraphQLEnumValueMap:
489481
enum_value_map: GraphQLEnumValueMap = {}
490482
for node in nodes:
491-
if node.values:
492-
for value in node.values:
493-
# Note: While this could make assertions to get the correctly typed
494-
# value, that would throw immediately while type system validation
495-
# with validate_schema() will produce more actionable results.
496-
enum_value_map[value.name.value] = GraphQLEnumValue(
497-
description=value.description.value
498-
if value.description
499-
else None,
500-
deprecation_reason=get_deprecation_reason(value),
501-
ast_node=value,
502-
)
483+
for value in node.values or []:
484+
# Note: While this could make assertions to get the correctly typed
485+
# value, that would throw immediately while type system validation
486+
# with validate_schema() will produce more actionable results.
487+
enum_value_map[value.name.value] = GraphQLEnumValue(
488+
description=value.description.value if value.description else None,
489+
deprecation_reason=get_deprecation_reason(value),
490+
ast_node=value,
491+
)
503492
return enum_value_map
504493

505494
def build_interfaces(
@@ -514,25 +503,23 @@ def build_interfaces(
514503
) -> List[GraphQLInterfaceType]:
515504
interfaces: List[GraphQLInterfaceType] = []
516505
for node in nodes:
517-
if node.interfaces:
518-
for type_ in node.interfaces:
519-
# Note: While this could make assertions to get the correctly typed
520-
# value, that would throw immediately while type system validation
521-
# with validate_schema() will produce more actionable results.
522-
interfaces.append(cast(GraphQLInterfaceType, get_named_type(type_)))
506+
for type_ in node.interfaces or []:
507+
# Note: While this could make assertions to get the correctly typed
508+
# value, that would throw immediately while type system validation
509+
# with validate_schema() will produce more actionable results.
510+
interfaces.append(cast(GraphQLInterfaceType, get_named_type(type_)))
523511
return interfaces
524512

525513
def build_union_types(
526514
nodes: Collection[Union[UnionTypeDefinitionNode, UnionTypeExtensionNode]],
527515
) -> List[GraphQLObjectType]:
528516
types: List[GraphQLObjectType] = []
529517
for node in nodes:
530-
if node.types:
531-
for type_ in node.types:
532-
# Note: While this could make assertions to get the correctly typed
533-
# value, that would throw immediately while type system validation
534-
# with validate_schema() will produce more actionable results.
535-
types.append(cast(GraphQLObjectType, get_named_type(type_)))
518+
for type_ in node.types or []:
519+
# Note: While this could make assertions to get the correctly typed
520+
# value, that would throw immediately while type system validation
521+
# with validate_schema() will produce more actionable results.
522+
types.append(cast(GraphQLObjectType, get_named_type(type_)))
536523
return types
537524

538525
def build_object_type(ast_node: ObjectTypeDefinitionNode) -> GraphQLObjectType:

src/graphql/validation/rules/known_argument_names.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def __init__(self, context: Union[ValidationContext, SDLValidationContext]):
3131
ast_definitions = context.document.definitions
3232
for def_ in ast_definitions:
3333
if isinstance(def_, DirectiveDefinitionNode):
34-
directive_args[def_.name.value] = (
35-
[arg.name.value for arg in def_.arguments] if def_.arguments else []
36-
)
34+
directive_args[def_.name.value] = [
35+
arg.name.value for arg in def_.arguments or []
36+
]
3737

3838
self.directive_args = directive_args
3939

src/graphql/validation/rules/provided_required_arguments.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ def __init__(self, context: Union[ValidationContext, SDLValidationContext]):
4646
ast_definitions = context.document.definitions
4747
for def_ in ast_definitions:
4848
if isinstance(def_, DirectiveDefinitionNode):
49-
required_args_map[def_.name.value] = (
50-
{
51-
arg.name.value: arg
52-
for arg in filter(is_required_argument_node, def_.arguments)
53-
}
54-
if def_.arguments
55-
else {}
56-
)
49+
required_args_map[def_.name.value] = {
50+
arg.name.value: arg
51+
for arg in filter(is_required_argument_node, def_.arguments or [])
52+
}
5753

5854
self.required_args_map = required_args_map
5955

src/graphql/validation/rules/unique_enum_value_names.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,36 @@ def __init__(self, context: SDLValidationContext):
2222
self.known_value_names: Dict[str, Dict[str, NameNode]] = defaultdict(dict)
2323

2424
def check_value_uniqueness(self, node: EnumTypeDefinitionNode, *_args):
25-
if node.values:
26-
type_name = node.name.value
27-
value_names = self.known_value_names[type_name]
28-
existing_type_map = self.existing_type_map
29-
30-
for value_def in node.values:
31-
value_name = value_def.name.value
32-
33-
existing_type = existing_type_map.get(type_name)
34-
if (
35-
is_enum_type(existing_type)
36-
and value_name in cast(GraphQLEnumType, existing_type).values
37-
):
38-
self.report_error(
39-
GraphQLError(
40-
f"Enum value '{type_name}.{value_name}'"
41-
" already exists in the schema."
42-
" It cannot also be defined in this type extension.",
43-
value_def.name,
44-
)
25+
existing_type_map = self.existing_type_map
26+
type_name = node.name.value
27+
value_names = self.known_value_names[type_name]
28+
29+
for value_def in node.values or []:
30+
value_name = value_def.name.value
31+
32+
existing_type = existing_type_map.get(type_name)
33+
if (
34+
is_enum_type(existing_type)
35+
and value_name in cast(GraphQLEnumType, existing_type).values
36+
):
37+
self.report_error(
38+
GraphQLError(
39+
f"Enum value '{type_name}.{value_name}'"
40+
" already exists in the schema."
41+
" It cannot also be defined in this type extension.",
42+
value_def.name,
4543
)
46-
elif value_name in value_names:
47-
self.report_error(
48-
GraphQLError(
49-
f"Enum value '{type_name}.{value_name}'"
50-
" can only be defined once.",
51-
[value_names[value_name], value_def.name],
52-
)
44+
)
45+
elif value_name in value_names:
46+
self.report_error(
47+
GraphQLError(
48+
f"Enum value '{type_name}.{value_name}'"
49+
" can only be defined once.",
50+
[value_names[value_name], value_def.name],
5351
)
54-
else:
55-
value_names[value_name] = value_def.name
52+
)
53+
else:
54+
value_names[value_name] = value_def.name
5655

5756
return self.SKIP
5857

src/graphql/validation/rules/unique_field_definition_names.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,32 @@ def __init__(self, context: SDLValidationContext):
2222
self.known_field_names: Dict[str, Dict[str, NameNode]] = defaultdict(dict)
2323

2424
def check_field_uniqueness(self, node: ObjectTypeDefinitionNode, *_args):
25-
if node.fields:
26-
type_name = node.name.value
27-
field_names = self.known_field_names[type_name]
28-
existing_type_map = self.existing_type_map
25+
existing_type_map = self.existing_type_map
26+
type_name = node.name.value
27+
field_names = self.known_field_names[type_name]
2928

30-
for field_def in node.fields:
31-
field_name = field_def.name.value
29+
for field_def in node.fields or []:
30+
field_name = field_def.name.value
3231

33-
if has_field(existing_type_map.get(type_name), field_name):
34-
self.report_error(
35-
GraphQLError(
36-
f"Field '{type_name}.{field_name}'"
37-
" already exists in the schema."
38-
" It cannot also be defined in this type extension.",
39-
field_def.name,
40-
)
32+
if has_field(existing_type_map.get(type_name), field_name):
33+
self.report_error(
34+
GraphQLError(
35+
f"Field '{type_name}.{field_name}'"
36+
" already exists in the schema."
37+
" It cannot also be defined in this type extension.",
38+
field_def.name,
4139
)
42-
elif field_name in field_names:
43-
self.report_error(
44-
GraphQLError(
45-
f"Field '{type_name}.{field_name}'"
46-
" can only be defined once.",
47-
[field_names[field_name], field_def.name],
48-
)
40+
)
41+
elif field_name in field_names:
42+
self.report_error(
43+
GraphQLError(
44+
f"Field '{type_name}.{field_name}'"
45+
" can only be defined once.",
46+
[field_names[field_name], field_def.name],
4947
)
50-
else:
51-
field_names[field_name] = field_def.name
48+
)
49+
else:
50+
field_names[field_name] = field_def.name
5251

5352
return self.SKIP
5453

0 commit comments

Comments
 (0)