def lex_second(s: str) -> Token: lexer = Lexer(Source(s)) lexer.advance() return lexer.advance()
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
def lex_one(s: str) -> Token: lexer = Lexer(Source(s)) return lexer.advance()
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'>"
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"
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
def create_source(location_offset): return Source("", "", location_offset)
def can_create_custom_attribute(): node = Source("foo") node.custom = "bar" # type: ignore assert node.custom == "bar" # type: ignore
def contains_references_to_source(): source = Source("{ id }") result = parse(source) assert result.loc and result.loc.source is source
def uses_default_arguments(): source = Source("") assert source.name == "GraphQL request" assert isinstance(source.location_offset, SourceLocation) assert source.location_offset == (1, 1)
def can_create_weak_reference(): source = Source("foo") ref = weakref.ref(source) assert ref() is source
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)
def accepts_location_offset(): location_offset = SourceLocation(2, 3) source = Source("", "", location_offset) assert source.location_offset is location_offset
def accepts_body_and_name(): source = Source("foo", "bar") assert source.body == "foo" assert source.name == "bar"
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)