Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude unset fields #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/graphql/execution/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def on_input_value_error(
)

coerced_values[var_name] = coerce_input_value(
value, var_type, on_input_value_error
value, var_type, on_input_value_error, exclude_unset=schema.exclude_unset
)

return coerced_values
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/type/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(
ast_node: Optional[ast.SchemaDefinitionNode] = None,
extension_ast_nodes: Optional[Collection[ast.SchemaExtensionNode]] = None,
assume_valid: bool = False,
exclude_unset: Optional[bool] = False
) -> None:
"""Initialize GraphQL schema.

Expand Down Expand Up @@ -202,6 +203,7 @@ def __init__(
self.query_type = query
self.mutation_type = mutation
self.subscription_type = subscription
self.exclude_unset = exclude_unset
# Provide specified directives (e.g. @include and @skip) by default
self.directives = specified_directives if directives is None else directives

Expand Down Expand Up @@ -302,6 +304,7 @@ def to_kwargs(self) -> GraphQLSchemaKwargs:
ast_node=self.ast_node,
extension_ast_nodes=self.extension_ast_nodes,
assume_valid=self._validation_errors is not None,
exclude_unset=self.exclude_unset,
)

def __copy__(self) -> "GraphQLSchema": # pragma: no cover
Expand Down Expand Up @@ -339,6 +342,7 @@ def __deepcopy__(self, memo_: Dict) -> "GraphQLSchema":
ast_node=deepcopy(self.ast_node),
extension_ast_nodes=deepcopy(self.extension_ast_nodes),
assume_valid=True,
exclude_unset=self.exclude_unset,
)

def get_root_type(self, operation: OperationType) -> Optional[GraphQLObjectType]:
Expand Down
43 changes: 25 additions & 18 deletions src/graphql/utilities/coerce_input_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def coerce_input_value(
type_: GraphQLInputType,
on_error: OnErrorCB = default_on_error,
path: Optional[Path] = None,
exclude_unset: Optional[bool] = False,
) -> Any:
"""Coerce a Python value given a GraphQL Input Type."""
if is_non_null_type(type_):
Expand Down Expand Up @@ -95,25 +96,31 @@ def coerce_input_value(
for field_name, field in fields.items():
field_value = input_value.get(field_name, Undefined)

if field_value is Undefined:
if field.default_value is not Undefined:
# Use out name as name if it exists (extension of GraphQL.js).
coerced_dict[field.out_name or field_name] = field.default_value
elif is_non_null_type(field.type): # pragma: no cover else
type_str = inspect(field.type)
on_error(
path.as_list() if path else [],
input_value,
GraphQLError(
f"Field '{field_name}' of required type '{type_str}'"
" was not provided."
),
if exclude_unset:
if field_name in input_value:
coerced_dict[field.out_name or field_name] = coerce_input_value(
field_value, field.type, on_error, Path(path, field_name, type_.name)
)
continue

coerced_dict[field.out_name or field_name] = coerce_input_value(
field_value, field.type, on_error, Path(path, field_name, type_.name)
)
else:
if field_value is Undefined:
if field.default_value is not Undefined:
# Use out name as name if it exists (extension of GraphQL.js).
coerced_dict[field.out_name or field_name] = field.default_value
elif is_non_null_type(field.type): # pragma: no cover else
type_str = inspect(field.type)
on_error(
path.as_list() if path else [],
input_value,
GraphQLError(
f"Field '{field_name}' of required type '{type_str}'"
" was not provided."
),
)
continue

coerced_dict[field.out_name or field_name] = coerce_input_value(
field_value, field.type, on_error, Path(path, field_name, type_.name)
)

# Ensure every provided field is defined.
for field_name in input_value:
Expand Down