예제 #1
0
def lex_second(s: str) -> Token:
    lexer = Lexer(Source(s))
    lexer.advance()
    return lexer.advance()
예제 #2
0
def lex_value(s: str) -> Optional[str]:
    lexer = Lexer(Source(s))
    value = lexer.advance().value
    assert lexer.advance().kind == TokenKind.EOF, "Expected EOF"
    return value
예제 #3
0
def lex_one(s: str) -> Token:
    lexer = Lexer(Source(s))
    return lexer.advance()
예제 #4
0
    def can_be_stringified():
        source = Source("")
        assert str(source) == "<Source name='GraphQL request'>"

        source = Source("", "Custom source name")
        assert str(source) == "<Source name='Custom source name'>"
예제 #5
0
from typing import cast

from graphql.error import GraphQLError, format_error
from graphql.language import parse, OperationDefinitionNode, Source
from graphql.pyutils import dedent

source = Source(dedent("""
    {
      field
    }
    """))

ast = parse(source)
operation_node = ast.definitions[0]
operation_node = cast(OperationDefinitionNode, operation_node)
assert operation_node and operation_node.kind == "operation_definition"
field_node = operation_node.selection_set.selections[0]
assert field_node


def describe_graphql_error():
    def is_a_class_and_is_a_subclass_of_exception():
        assert type(GraphQLError) is type
        assert issubclass(GraphQLError, Exception)
        assert isinstance(GraphQLError("str"), Exception)
        assert isinstance(GraphQLError("str"), GraphQLError)

    def has_a_name_message_and_stack_trace():
        e = GraphQLError("msg")
        assert e.__class__.__name__ == "GraphQLError"
        assert e.message == "msg"
예제 #6
0
def lex_value(s: str) -> str:
    lexer = Lexer(Source(s))
    value = lexer.advance().value
    assert isinstance(value, str)
    assert lexer.advance().kind == TokenKind.EOF, "Expected EOF"
    return value
예제 #7
0
 def create_source(location_offset):
     return Source("", "", location_offset)
예제 #8
0
 def can_create_custom_attribute():
     node = Source("foo")
     node.custom = "bar"  # type: ignore
     assert node.custom == "bar"  # type: ignore
예제 #9
0
 def contains_references_to_source():
     source = Source("{ id }")
     result = parse(source)
     assert result.loc and result.loc.source is source
예제 #10
0
 def uses_default_arguments():
     source = Source("")
     assert source.name == "GraphQL request"
     assert isinstance(source.location_offset, SourceLocation)
     assert source.location_offset == (1, 1)
예제 #11
0
 def can_create_weak_reference():
     source = Source("foo")
     ref = weakref.ref(source)
     assert ref() is source
예제 #12
0
 def accepts_tuple_as_location_offset():
     # noinspection PyTypeChecker
     source = Source("", "", (2, 3))  # type: ignore
     assert isinstance(source.location_offset, SourceLocation)
     assert source.location_offset == (2, 3)
예제 #13
0
 def accepts_location_offset():
     location_offset = SourceLocation(2, 3)
     source = Source("", "", location_offset)
     assert source.location_offset is location_offset
예제 #14
0
 def accepts_body_and_name():
     source = Source("foo", "bar")
     assert source.body == "foo"
     assert source.name == "bar"
예제 #15
0
 def parse_provides_useful_error_when_using_source():
     with raises(GraphQLSyntaxError) as exc_info:
         parse(Source('query', 'MyQuery.graphql'))
     error = exc_info.value
     assert str(error) == ('Syntax Error: Expected {, found <EOF>\n\n'
                           'MyQuery.graphql (1:6)\n1: query\n        ^\n')
def validation_errors(query):
    """Helper function to test a query and the expected response."""
    source = Source(query, 'StarWars.graphql')
    ast = parse(source)
    return validate(star_wars_schema, ast)