Skip to content

Commit

Permalink
Merge pull request #1420
Browse files Browse the repository at this point in the history
fix: add default param _variables to parse_literal #1419
  • Loading branch information
Cito committed May 6, 2022
2 parents 03277a5 + 181d9f7 commit 4e8a1e6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
53 changes: 53 additions & 0 deletions graphene/tests/issues/test_1419.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest

from ...types.base64 import Base64
from ...types.datetime import Date, DateTime
from ...types.decimal import Decimal
from ...types.generic import GenericScalar
from ...types.json import JSONString
from ...types.objecttype import ObjectType
from ...types.scalars import ID, BigInt, Boolean, Float, Int, String
from ...types.schema import Schema
from ...types.uuid import UUID


@pytest.mark.parametrize(
"input_type,input_value",
[
(Date, '"2022-02-02"'),
(GenericScalar, '"foo"'),
(Int, "1"),
(BigInt, "12345678901234567890"),
(Float, "1.1"),
(String, '"foo"'),
(Boolean, "true"),
(ID, "1"),
(DateTime, '"2022-02-02T11:11:11"'),
(UUID, '"cbebbc62-758e-4f75-a890-bc73b5017d81"'),
(Decimal, "1.1"),
(JSONString, '{key:"foo",value:"bar"}'),
(Base64, '"Q2hlbG8gd29ycmxkCg=="'),
],
)
def test_parse_literal_with_variables(input_type, input_value):
# input_b needs to be evaluated as literal while the variable dict for
# input_a is passed along.

class Query(ObjectType):
generic = GenericScalar(input_a=GenericScalar(), input_b=input_type())

def resolve_generic(self, info, input_a=None, input_b=None):
return input

schema = Schema(query=Query)

query = f"""
query Test($a: GenericScalar){{
generic(inputA: $a, inputB: {input_value})
}}
"""
result = schema.execute(
query,
variables={"a": "bar"},
)
assert not result.errors
2 changes: 1 addition & 1 deletion graphene/types/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def serialize(value):
return b64encode(value).decode("utf-8")

@classmethod
def parse_literal(cls, node):
def parse_literal(cls, node, _variables=None):
if not isinstance(node, StringValueNode):
raise GraphQLError(
f"Base64 cannot represent non-string value: {print_ast(node)}"
Expand Down
2 changes: 1 addition & 1 deletion graphene/types/decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def serialize(dec):
return str(dec)

@classmethod
def parse_literal(cls, node):
def parse_literal(cls, node, _variables=None):
if isinstance(node, (StringValueNode, IntValueNode)):
return cls.parse_value(node.value)

Expand Down
2 changes: 1 addition & 1 deletion graphene/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def identity(value):
parse_value = identity

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, (StringValueNode, BooleanValueNode)):
return ast.value
elif isinstance(ast, IntValueNode):
Expand Down
2 changes: 1 addition & 1 deletion graphene/types/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def serialize(dt):
return json.dumps(dt)

@staticmethod
def parse_literal(node):
def parse_literal(node, _variables=None):
if isinstance(node, StringValueNode):
return json.loads(node.value)

Expand Down
12 changes: 6 additions & 6 deletions graphene/types/scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def coerce_int(value):
parse_value = coerce_int

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, IntValueNode):
num = int(ast.value)
if MIN_INT <= num <= MAX_INT:
Expand Down Expand Up @@ -104,7 +104,7 @@ def coerce_int(value):
parse_value = coerce_int

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, IntValueNode):
return int(ast.value)

Expand All @@ -128,7 +128,7 @@ def coerce_float(value):
parse_value = coerce_float

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, (FloatValueNode, IntValueNode)):
return float(ast.value)

Expand All @@ -150,7 +150,7 @@ def coerce_string(value):
parse_value = coerce_string

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, StringValueNode):
return ast.value

Expand All @@ -164,7 +164,7 @@ class Boolean(Scalar):
parse_value = bool

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, BooleanValueNode):
return ast.value

Expand All @@ -182,6 +182,6 @@ class ID(Scalar):
parse_value = str

@staticmethod
def parse_literal(ast):
def parse_literal(ast, _variables=None):
if isinstance(ast, (StringValueNode, IntValueNode)):
return ast.value
2 changes: 1 addition & 1 deletion graphene/types/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def serialize(uuid):
return str(uuid)

@staticmethod
def parse_literal(node):
def parse_literal(node, _variables=None):
if isinstance(node, StringValueNode):
return _UUID(node.value)

Expand Down

0 comments on commit 4e8a1e6

Please sign in to comment.