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

fix: default value for argument should be Undefined (Issue #1394) #1412

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
name: String

"""The ships used by the faction."""
ships(before: String = null, after: String = null, first: Int = null, last: Int = null): ShipConnection
ships(before: String, after: String, first: Int, last: Int): ShipConnection
}

"""An object with an ID"""
Expand Down
2 changes: 2 additions & 0 deletions graphene/relay/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def node_resolver(cls, only_type, root, info, id):
def get_node_from_global_id(cls, info, global_id, only_type=None):
try:
_type, _id = cls.from_global_id(global_id)
if not _type:
raise ValueError("Invalid Global ID")
except Exception as e:
raise Exception(
f'Unable to parse global ID "{global_id}". '
Expand Down
36 changes: 36 additions & 0 deletions graphene/tests/issues/test_1394.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from ...types import ObjectType, Schema, String, NonNull


class Query(ObjectType):
hello = String(input=NonNull(String))

def resolve_hello(self, info, input):
if input == "nothing":
return None
return f"Hello {input}!"


schema = Schema(query=Query)


def test_required_input_provided():
"""
Test that a required argument works when provided.
"""
input_value = "Potato"
result = schema.execute('{ hello(input: "%s") }' % input_value)
assert not result.errors
assert result.data == {"hello": "Hello Potato!"}


def test_required_input_missing():
"""
Test that a required argument raised an error if not provided.
"""
result = schema.execute("{ hello }")
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Field 'hello' argument 'input' of type 'String!' is required, but it was not provided."
)
3 changes: 2 additions & 1 deletion graphene/types/argument.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from itertools import chain
from graphql import Undefined

from .dynamic import Dynamic
from .mountedtype import MountedType
Expand Down Expand Up @@ -41,7 +42,7 @@ class Argument(MountedType):
def __init__(
self,
type_,
default_value=None,
default_value=Undefined,
description=None,
name=None,
required=False,
Expand Down
6 changes: 3 additions & 3 deletions graphene/types/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ def resolve_test(self, info, **args):

result = test_schema.execute("{ test }", None)
assert not result.errors
assert result.data == {"test": '[null,{"a_str":null,"a_int":null}]'}
assert result.data == {"test": "[null,{}]"}

result = test_schema.execute('{ test(aStr: "String!") }', "Source!")
assert not result.errors
assert result.data == {"test": '["Source!",{"a_str":"String!","a_int":null}]'}
assert result.data == {"test": '["Source!",{"a_str":"String!"}]'}

result = test_schema.execute('{ test(aInt: -123, aStr: "String!") }', "Source!")
assert not result.errors
Expand All @@ -258,7 +258,7 @@ def resolve_test(self, info, **args):

result = test_schema.execute("{ test }", None)
assert not result.errors
assert result.data == {"test": '[null,{"a_input":null}]'}
assert result.data == {"test": "[null,{}]"}

result = test_schema.execute('{ test(aInput: {aField: "String!"} ) }', "Source!")
assert not result.errors
Expand Down
7 changes: 5 additions & 2 deletions graphene/types/tests/test_type_map.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from graphql import Undefined
from graphql.type import (
GraphQLArgument,
GraphQLEnumType,
Expand Down Expand Up @@ -244,7 +245,9 @@ class MyObjectType(ObjectType):
foo_field = fields["fooBar"]
assert isinstance(foo_field, GraphQLField)
assert foo_field.args == {
"barFoo": GraphQLArgument(GraphQLString, default_value=None, out_name="bar_foo")
"barFoo": GraphQLArgument(
GraphQLString, default_value=Undefined, out_name="bar_foo"
)
}


Expand All @@ -267,7 +270,7 @@ class MyObjectType(ObjectType):
assert isinstance(foo_field, GraphQLField)
assert foo_field.args == {
"bar_foo": GraphQLArgument(
GraphQLString, default_value=None, out_name="bar_foo"
GraphQLString, default_value=Undefined, out_name="bar_foo"
)
}

Expand Down
5 changes: 3 additions & 2 deletions graphene/utils/tests/test_deduplicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_does_not_modify_input():
],
"movies": {
"1198359": {
"id": "1198359",
"name": "King Arthur: Legend of the Sword",
"synopsis": (
"When the child Arthur's father is murdered, Vortigern, "
Expand Down Expand Up @@ -159,7 +160,7 @@ def resolve_events(_, info):
"date": "2017-05-19",
"movie": {
"__typename": "Movie",
"id": "TW92aWU6Tm9uZQ==",
"id": "TW92aWU6MTE5ODM1OQ==",
"name": "King Arthur: Legend of the Sword",
"synopsis": (
"When the child Arthur's father is murdered, Vortigern, "
Expand All @@ -172,7 +173,7 @@ def resolve_events(_, info):
"__typename": "Event",
"id": "RXZlbnQ6MjM0",
"date": "2017-05-20",
"movie": {"__typename": "Movie", "id": "TW92aWU6Tm9uZQ=="},
"movie": {"__typename": "Movie", "id": "TW92aWU6MTE5ODM1OQ=="},
},
]
}