1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
import pytest
import strawberry
from strawberry.schema.config import StrawberryConfig
def test_runs_parsing():
@strawberry.type
class Query:
name: str
schema = strawberry.Schema(query=Query)
query = """
query {
example
"""
result = schema.execute_sync(query)
assert len(result.errors) == 1
assert result.errors[0].message == "Syntax Error: Expected Name, found <EOF>."
@pytest.mark.asyncio
@pytest.mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
async def test_errors_when_running_async_in_sync_mode():
@strawberry.type
class Query:
@strawberry.field
async def name(self) -> str:
return "Patrick"
schema = strawberry.Schema(query=Query)
query = """
query {
name
}
"""
result = schema.execute_sync(query)
assert len(result.errors) == 1
assert isinstance(result.errors[0].original_error, RuntimeError)
assert (
result.errors[0].message
== "GraphQL execution failed to complete synchronously."
)
@pytest.mark.asyncio
async def test_runs_parsing_async():
@strawberry.type
class Query:
example: str
schema = strawberry.Schema(query=Query)
query = """
query {
example
"""
result = await schema.execute(query)
assert len(result.errors) == 1
assert result.errors[0].message == "Syntax Error: Expected Name, found <EOF>."
def test_suggests_fields_by_default():
@strawberry.type
class Query:
name: str
schema = strawberry.Schema(query=Query)
query = """
query {
ample
}
"""
result = schema.execute_sync(query)
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Cannot query field 'ample' on type 'Query'. Did you mean 'name'?"
)
def test_can_disable_field_suggestions():
@strawberry.type
class Query:
name: str
schema = strawberry.Schema(
query=Query, config=StrawberryConfig(disable_field_suggestions=True)
)
query = """
query {
ample
}
"""
result = schema.execute_sync(query)
assert len(result.errors) == 1
assert result.errors[0].message == "Cannot query field 'ample' on type 'Query'."
def test_can_disable_field_suggestions_multiple_fields():
@strawberry.type
class Query:
name: str
age: str
schema = strawberry.Schema(
query=Query, config=StrawberryConfig(disable_field_suggestions=True)
)
query = """
query {
ample
ag
}
"""
result = schema.execute_sync(query)
assert len(result.errors) == 2
assert result.errors[0].message == "Cannot query field 'ample' on type 'Query'."
assert result.errors[1].message == "Cannot query field 'ag' on type 'Query'."
|