Skip to content

Commit 03277a5

Browse files
authored
Merge pull request #1412 from loft-orbital/issue-#1394_fix-required
2 parents 61f0d8a + 19ebf08 commit 03277a5

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

examples/starwars_relay/tests/snapshots/snap_test_objectidentification.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
name: String
5050
5151
"""The ships used by the faction."""
52-
ships(before: String = null, after: String = null, first: Int = null, last: Int = null): ShipConnection
52+
ships(before: String, after: String, first: Int, last: Int): ShipConnection
5353
}
5454
5555
"""An object with an ID"""

graphene/relay/node.py

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def node_resolver(cls, only_type, root, info, id):
8686
def get_node_from_global_id(cls, info, global_id, only_type=None):
8787
try:
8888
_type, _id = cls.from_global_id(global_id)
89+
if not _type:
90+
raise ValueError("Invalid Global ID")
8991
except Exception as e:
9092
raise Exception(
9193
f'Unable to parse global ID "{global_id}". '

graphene/tests/issues/test_1394.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from ...types import ObjectType, Schema, String, NonNull
2+
3+
4+
class Query(ObjectType):
5+
hello = String(input=NonNull(String))
6+
7+
def resolve_hello(self, info, input):
8+
if input == "nothing":
9+
return None
10+
return f"Hello {input}!"
11+
12+
13+
schema = Schema(query=Query)
14+
15+
16+
def test_required_input_provided():
17+
"""
18+
Test that a required argument works when provided.
19+
"""
20+
input_value = "Potato"
21+
result = schema.execute('{ hello(input: "%s") }' % input_value)
22+
assert not result.errors
23+
assert result.data == {"hello": "Hello Potato!"}
24+
25+
26+
def test_required_input_missing():
27+
"""
28+
Test that a required argument raised an error if not provided.
29+
"""
30+
result = schema.execute("{ hello }")
31+
assert result.errors
32+
assert len(result.errors) == 1
33+
assert (
34+
result.errors[0].message
35+
== "Field 'hello' argument 'input' of type 'String!' is required, but it was not provided."
36+
)

graphene/types/argument.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from itertools import chain
2+
from graphql import Undefined
23

34
from .dynamic import Dynamic
45
from .mountedtype import MountedType
@@ -41,7 +42,7 @@ class Argument(MountedType):
4142
def __init__(
4243
self,
4344
type_,
44-
default_value=None,
45+
default_value=Undefined,
4546
description=None,
4647
name=None,
4748
required=False,

graphene/types/tests/test_query.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ def resolve_test(self, info, **args):
229229

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

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

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

259259
result = test_schema.execute("{ test }", None)
260260
assert not result.errors
261-
assert result.data == {"test": '[null,{"a_input":null}]'}
261+
assert result.data == {"test": "[null,{}]"}
262262

263263
result = test_schema.execute('{ test(aInput: {aField: "String!"} ) }', "Source!")
264264
assert not result.errors

graphene/types/tests/test_type_map.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from graphql import Undefined
12
from graphql.type import (
23
GraphQLArgument,
34
GraphQLEnumType,
@@ -244,7 +245,9 @@ class MyObjectType(ObjectType):
244245
foo_field = fields["fooBar"]
245246
assert isinstance(foo_field, GraphQLField)
246247
assert foo_field.args == {
247-
"barFoo": GraphQLArgument(GraphQLString, default_value=None, out_name="bar_foo")
248+
"barFoo": GraphQLArgument(
249+
GraphQLString, default_value=Undefined, out_name="bar_foo"
250+
)
248251
}
249252

250253

@@ -267,7 +270,7 @@ class MyObjectType(ObjectType):
267270
assert isinstance(foo_field, GraphQLField)
268271
assert foo_field.args == {
269272
"bar_foo": GraphQLArgument(
270-
GraphQLString, default_value=None, out_name="bar_foo"
273+
GraphQLString, default_value=Undefined, out_name="bar_foo"
271274
)
272275
}
273276

graphene/utils/tests/test_deduplicator.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def test_does_not_modify_input():
9494
],
9595
"movies": {
9696
"1198359": {
97+
"id": "1198359",
9798
"name": "King Arthur: Legend of the Sword",
9899
"synopsis": (
99100
"When the child Arthur's father is murdered, Vortigern, "
@@ -159,7 +160,7 @@ def resolve_events(_, info):
159160
"date": "2017-05-19",
160161
"movie": {
161162
"__typename": "Movie",
162-
"id": "TW92aWU6Tm9uZQ==",
163+
"id": "TW92aWU6MTE5ODM1OQ==",
163164
"name": "King Arthur: Legend of the Sword",
164165
"synopsis": (
165166
"When the child Arthur's father is murdered, Vortigern, "
@@ -172,7 +173,7 @@ def resolve_events(_, info):
172173
"__typename": "Event",
173174
"id": "RXZlbnQ6MjM0",
174175
"date": "2017-05-20",
175-
"movie": {"__typename": "Movie", "id": "TW92aWU6Tm9uZQ=="},
176+
"movie": {"__typename": "Movie", "id": "TW92aWU6MTE5ODM1OQ=="},
176177
},
177178
]
178179
}

0 commit comments

Comments
 (0)