Skip to content

Allow omitting optional arguments with serialize_variables=True #338

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

Merged
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
1 change: 1 addition & 0 deletions gql/utilities/serialize_variable_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def serialize_value(type_: GraphQLType, value: Any) -> Any:
return {
field_name: serialize_value(field.type, value[field_name])
for field_name, field in type_.fields.items()
if field_name in value
}

raise GraphQLError(f"Impossible to serialize value with type: {inspect(type_)}.")
Expand Down
28 changes: 27 additions & 1 deletion tests/custom_scalars/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ def resolve_seconds(root, _info, interval):
IntervalInputType = GraphQLInputObjectType(
"IntervalInput",
fields={
"start": GraphQLInputField(DatetimeScalar),
"start": GraphQLInputField(
DatetimeScalar,
default_value=datetime(2021, 11, 12, 11, 58, 13, 461161),
),
"end": GraphQLInputField(DatetimeScalar),
},
)
Expand Down Expand Up @@ -216,3 +219,26 @@ def test_seconds():
print(result)

assert result["seconds"] == 432000


@pytest.mark.skipif(
not hasattr(datetime, "fromisoformat"), reason="fromisoformat is new in Python 3.7+"
)
def test_seconds_omit_optional_start_argument():
client = Client(schema=schema)

in_five_days = datetime.fromisoformat("2021-11-17T11:58:13.461161")

query = gql(
"query seconds($interval: IntervalInput) {seconds(interval: $interval)}"
)

variable_values = {"interval": {"end": in_five_days}}

result = client.execute(
query, variable_values=variable_values, serialize_variables=True
)

print(result)

assert result["seconds"] == 432000