Skip to content

Commit 4e8a1e6

Browse files
authored
Merge pull request #1420
fix: add default param _variables to parse_literal #1419
2 parents 03277a5 + 181d9f7 commit 4e8a1e6

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

graphene/tests/issues/test_1419.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
3+
from ...types.base64 import Base64
4+
from ...types.datetime import Date, DateTime
5+
from ...types.decimal import Decimal
6+
from ...types.generic import GenericScalar
7+
from ...types.json import JSONString
8+
from ...types.objecttype import ObjectType
9+
from ...types.scalars import ID, BigInt, Boolean, Float, Int, String
10+
from ...types.schema import Schema
11+
from ...types.uuid import UUID
12+
13+
14+
@pytest.mark.parametrize(
15+
"input_type,input_value",
16+
[
17+
(Date, '"2022-02-02"'),
18+
(GenericScalar, '"foo"'),
19+
(Int, "1"),
20+
(BigInt, "12345678901234567890"),
21+
(Float, "1.1"),
22+
(String, '"foo"'),
23+
(Boolean, "true"),
24+
(ID, "1"),
25+
(DateTime, '"2022-02-02T11:11:11"'),
26+
(UUID, '"cbebbc62-758e-4f75-a890-bc73b5017d81"'),
27+
(Decimal, "1.1"),
28+
(JSONString, '{key:"foo",value:"bar"}'),
29+
(Base64, '"Q2hlbG8gd29ycmxkCg=="'),
30+
],
31+
)
32+
def test_parse_literal_with_variables(input_type, input_value):
33+
# input_b needs to be evaluated as literal while the variable dict for
34+
# input_a is passed along.
35+
36+
class Query(ObjectType):
37+
generic = GenericScalar(input_a=GenericScalar(), input_b=input_type())
38+
39+
def resolve_generic(self, info, input_a=None, input_b=None):
40+
return input
41+
42+
schema = Schema(query=Query)
43+
44+
query = f"""
45+
query Test($a: GenericScalar){{
46+
generic(inputA: $a, inputB: {input_value})
47+
}}
48+
"""
49+
result = schema.execute(
50+
query,
51+
variables={"a": "bar"},
52+
)
53+
assert not result.errors

graphene/types/base64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def serialize(value):
2222
return b64encode(value).decode("utf-8")
2323

2424
@classmethod
25-
def parse_literal(cls, node):
25+
def parse_literal(cls, node, _variables=None):
2626
if not isinstance(node, StringValueNode):
2727
raise GraphQLError(
2828
f"Base64 cannot represent non-string value: {print_ast(node)}"

graphene/types/decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def serialize(dec):
2222
return str(dec)
2323

2424
@classmethod
25-
def parse_literal(cls, node):
25+
def parse_literal(cls, node, _variables=None):
2626
if isinstance(node, (StringValueNode, IntValueNode)):
2727
return cls.parse_value(node.value)
2828

graphene/types/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def identity(value):
2929
parse_value = identity
3030

3131
@staticmethod
32-
def parse_literal(ast):
32+
def parse_literal(ast, _variables=None):
3333
if isinstance(ast, (StringValueNode, BooleanValueNode)):
3434
return ast.value
3535
elif isinstance(ast, IntValueNode):

graphene/types/json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def serialize(dt):
2020
return json.dumps(dt)
2121

2222
@staticmethod
23-
def parse_literal(node):
23+
def parse_literal(node, _variables=None):
2424
if isinstance(node, StringValueNode):
2525
return json.loads(node.value)
2626

graphene/types/scalars.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def coerce_int(value):
7575
parse_value = coerce_int
7676

7777
@staticmethod
78-
def parse_literal(ast):
78+
def parse_literal(ast, _variables=None):
7979
if isinstance(ast, IntValueNode):
8080
num = int(ast.value)
8181
if MIN_INT <= num <= MAX_INT:
@@ -104,7 +104,7 @@ def coerce_int(value):
104104
parse_value = coerce_int
105105

106106
@staticmethod
107-
def parse_literal(ast):
107+
def parse_literal(ast, _variables=None):
108108
if isinstance(ast, IntValueNode):
109109
return int(ast.value)
110110

@@ -128,7 +128,7 @@ def coerce_float(value):
128128
parse_value = coerce_float
129129

130130
@staticmethod
131-
def parse_literal(ast):
131+
def parse_literal(ast, _variables=None):
132132
if isinstance(ast, (FloatValueNode, IntValueNode)):
133133
return float(ast.value)
134134

@@ -150,7 +150,7 @@ def coerce_string(value):
150150
parse_value = coerce_string
151151

152152
@staticmethod
153-
def parse_literal(ast):
153+
def parse_literal(ast, _variables=None):
154154
if isinstance(ast, StringValueNode):
155155
return ast.value
156156

@@ -164,7 +164,7 @@ class Boolean(Scalar):
164164
parse_value = bool
165165

166166
@staticmethod
167-
def parse_literal(ast):
167+
def parse_literal(ast, _variables=None):
168168
if isinstance(ast, BooleanValueNode):
169169
return ast.value
170170

@@ -182,6 +182,6 @@ class ID(Scalar):
182182
parse_value = str
183183

184184
@staticmethod
185-
def parse_literal(ast):
185+
def parse_literal(ast, _variables=None):
186186
if isinstance(ast, (StringValueNode, IntValueNode)):
187187
return ast.value

graphene/types/uuid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def serialize(uuid):
2121
return str(uuid)
2222

2323
@staticmethod
24-
def parse_literal(node):
24+
def parse_literal(node, _variables=None):
2525
if isinstance(node, StringValueNode):
2626
return _UUID(node.value)
2727

0 commit comments

Comments
 (0)