Ejemplo n.º 1
0
 def correct_message_for_unexpected_token():
     with raises(GraphQLSyntaxError) as exc_info:
         parse_value(":")
     assert exc_info.value == {
         "message": "Syntax Error: Unexpected ':'.",
         "locations": [(1, 1)],
     }
Ejemplo n.º 2
0
 def correct_message_for_incomplete_variable():
     with raises(GraphQLSyntaxError) as exc_info:
         parse_value("$")
     assert exc_info.value == {
         "message": "Syntax Error: Expected Name, found <EOF>.",
         "locations": [(1, 2)],
     }
 def _value_from(
     value_text: str,
     type_: GraphQLInputType,
     variables: Optional[Dict[str, Any]] = None,
 ):
     ast = parse_value(value_text)
     return value_from_ast(ast, type_, variables)
Ejemplo n.º 4
0
 def _test_case(type_, value_text, expected):
     value_node = parse_value(value_text)
     value = value_from_ast(value_node, type_)
     if isinstance(expected, float) and isnan(expected):
         assert isnan(value)
     else:
         assert value == expected
Ejemplo n.º 5
0
def load_literal_value(node, ptype):
    if isinstance(node, IntValueNode):
        return int(node.value)
    elif isinstance(node, FloatValueNode):
        return float(node.value)
    elif isinstance(node, BooleanValueNode):
        return bool(node.value)
    elif isinstance(node, StringValueNode):
        return node.value
    elif isinstance(node, NullValueNode):
        return None
    elif isinstance(node, ListValueNode):
        return [load_literal_value(v.value) for v in node.values]
    elif isinstance(node, EnumValueNode):
        value = getattr(ptype, node.value)
        if not value:
            raise RuntimeError(f'{node.value} is not a valid member of {type}',
                               node)
        return value
    elif isinstance(node, ObjectValueNode):
        return ptype.__resolve__(node)
    elif isinstance(node, VariableNode):
        name = node.name.value
        variables = types.context.get().variables
        if name not in variables:
            raise RuntimeError(f'Can not find variable {name}')
        node = parse_value(variables[name])
        return load_literal_value(node, ptype)
    raise RuntimeError(f'Can not convert {node.value}', node)
Ejemplo n.º 6
0
    def check_const_value_node():
        assert is_const_value_node(parse_value('"value"')) is True
        assert is_const_value_node(parse_value("$var")) is False

        assert is_const_value_node(parse_value('{ field: "value" }')) is True
        assert is_const_value_node(parse_value("{ field: $var }")) is False

        assert is_const_value_node(parse_value('[ "value" ]')) is True
        assert is_const_value_node(parse_value("[ $var ]")) is False
Ejemplo n.º 7
0
    def supports_traversal_of_input_values():
        visited = []

        schema = build_schema("""
            input ComplexInput {
              stringListField: [String]
            }
            """)
        complex_input_type = schema.get_type("ComplexInput")
        assert complex_input_type is not None
        type_info = TypeInfo(schema, complex_input_type)

        ast = parse_value('{ stringListField: ["foo"] }')

        class TestVisitor(Visitor):
            @staticmethod
            def enter(node: Node, *_args):
                type_ = type_info.get_input_type()
                visited.append((
                    "enter",
                    node.kind,
                    node.value if isinstance(node, NameNode) else None,
                    str(type_),
                ))

            @staticmethod
            def leave(node: Node, *_args):
                type_ = type_info.get_input_type()
                visited.append((
                    "leave",
                    node.kind,
                    node.value if isinstance(node, NameNode) else None,
                    str(type_),
                ))

        visit(ast, TypeInfoVisitor(type_info, TestVisitor()))

        assert visited == [
            ("enter", "object_value", None, "ComplexInput"),
            ("enter", "object_field", None, "[String]"),
            ("enter", "name", "stringListField", "[String]"),
            ("leave", "name", "stringListField", "[String]"),
            ("enter", "list_value", None, "String"),
            ("enter", "string_value", None, "String"),
            ("leave", "string_value", None, "String"),
            ("leave", "list_value", None, "String"),
            ("leave", "object_field", None, "[String]"),
            ("leave", "object_value", None, "ComplexInput"),
        ]
Ejemplo n.º 8
0
 def parses_list_values():
     result = parse_value('[123 "abc"]')
     assert isinstance(result, ListValueNode)
     assert result.loc == (0, 11)
     values = result.values
     assert isinstance(values, list)
     assert len(values) == 2
     value = values[0]
     assert isinstance(value, IntValueNode)
     assert value.loc == (1, 4)
     assert value.value == "123"
     value = values[1]
     assert isinstance(value, StringValueNode)
     assert value.loc == (5, 10)
     assert value.value == "abc"
Ejemplo n.º 9
0
 def parses_block_strings():
     result = parse_value('["""long""" "short"]')
     assert isinstance(result, ListValueNode)
     assert result.loc == (0, 20)
     values = result.values
     assert isinstance(values, list)
     assert len(values) == 2
     value = values[0]
     assert isinstance(value, StringValueNode)
     assert value.loc == (1, 11)
     assert value.value == "long"
     assert value.block is True
     value = values[1]
     assert isinstance(value, StringValueNode)
     assert value.loc == (12, 19)
     assert value.value == "short"
     assert value.block is False
Ejemplo n.º 10
0
    def supports_traversal_of_input_values():
        visited = []

        complex_input_type = test_schema.get_type("ComplexInput")
        assert complex_input_type is not None
        type_info = TypeInfo(test_schema, None, complex_input_type)

        ast = parse_value('{ stringListField: ["foo"] }')

        # noinspection PyMethodMayBeStatic
        class TestVisitor(Visitor):
            def enter(self, node: Node, *_args):
                type_ = type_info.get_input_type()
                visited.append((
                    "enter",
                    node.kind,
                    node.value if isinstance(node, NameNode) else None,
                    str(type_),
                ))

            def leave(self, node: Node, *_args):
                type_ = type_info.get_input_type()
                visited.append((
                    "leave",
                    node.kind,
                    node.value if isinstance(node, NameNode) else None,
                    str(type_),
                ))

        visit(ast, TypeInfoVisitor(type_info, TestVisitor()))

        assert visited == [
            ("enter", "object_value", None, "ComplexInput"),
            ("enter", "object_field", None, "[String]"),
            ("enter", "name", "stringListField", "[String]"),
            ("leave", "name", "stringListField", "[String]"),
            ("enter", "list_value", None, "String"),
            ("enter", "string_value", None, "String"),
            ("leave", "string_value", None, "String"),
            ("leave", "list_value", None, "String"),
            ("leave", "object_field", None, "[String]"),
            ("leave", "object_value", None, "ComplexInput"),
        ]
Ejemplo n.º 11
0
 def allows_variables():
     result = parse_value("{ field: $var }")
     assert isinstance(result, ObjectValueNode)
     assert result.loc == (0, 15)
     fields = result.fields
     assert len(fields) == 1
     field = fields[0]
     assert isinstance(field, ObjectFieldNode)
     assert field.loc == (2, 13)
     name = field.name
     assert isinstance(name, NameNode)
     assert name.loc == (2, 7)
     assert name.value == "field"
     value = field.value
     assert isinstance(value, VariableNode)
     assert value.loc == (9, 13)
     name = value.name
     assert isinstance(name, NameNode)
     assert name.loc == (10, 13)
     assert name.value == "var"
 def _test_case(value_text, expected):
     value_node = parse_value(value_text)
     assert value_from_ast_untyped(value_node) == expected
 def _test_case_with_vars(value_text, variables, expected):
     value_node = parse_value(value_text)
     assert value_from_ast_untyped(value_node, variables) == expected
Ejemplo n.º 14
0
 def parses_empty_strings():
     result = parse_value('""')
     assert isinstance(result, StringValueNode)
     assert result.value == ""
     assert result.loc == (0, 2)
Ejemplo n.º 15
0
 def parses_null_value():
     result = parse_value("null")
     assert isinstance(result, NullValueNode)
     assert result.loc == (0, 4)
 def _value_from(value_text, type_, variables=None):
     ast = parse_value(value_text)
     return value_from_ast(ast, type_, variables)
Ejemplo n.º 17
0
 def _test_case_expect_nan(type_, value_text):
     value_node = parse_value(value_text)
     assert isnan(value_from_ast(value_node, type_))
Ejemplo n.º 18
0
 def _test_case(type_, value_text, expected):
     value_node = parse_value(value_text)
     assert value_from_ast(value_node, type_) == expected
Ejemplo n.º 19
0
 def test_case_with_vars(variables, type_, value_text, expected):
     value_node = parse_value(value_text)
     assert value_from_ast(value_node, type_, variables) == expected
Ejemplo n.º 20
0
 def _test_case_with_vars(value_text, variables, expected):
     value_node = parse_value(value_text)
     _compare_value(value_from_ast_untyped(value_node, variables), expected)
Ejemplo n.º 21
0
 def _test_case(value_text, expected):
     value_node = parse_value(value_text)
     _compare_value(value_from_ast_untyped(value_node), expected)
Ejemplo n.º 22
0
 def _expect_value_from_vars(value_text: str,
                             variables: Optional[Dict[str,
                                                      Any]], expected: Any):
     ast = parse_value(value_text)
     value = value_from_ast_untyped(ast, variables)
     _compare_value(value, expected)
Ejemplo n.º 23
0
 def _expect_value_from(value_text: str, expected: Any):
     ast = parse_value(value_text)
     value = value_from_ast_untyped(ast)
     _compare_value(value, expected)