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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
import dataclasses
import textwrap
from operator import getitem
import strawberry
from strawberry.printer import print_schema
from strawberry.schema.config import StrawberryConfig
from strawberry.types.field import StrawberryField
def test_custom_field():
class CustomField(StrawberryField):
def get_result(self, root, info, args, kwargs):
return getattr(root, self.python_name) * 2
@strawberry.type
class Query:
a: str = CustomField(default="Example") # type: ignore
schema = strawberry.Schema(query=Query)
query = "{ a }"
result = schema.execute_sync(query, root_value=Query())
assert not result.errors
assert result.data == {"a": "ExampleExample"}
def test_default_resolver_gets_attribute():
@strawberry.type
class User:
name: str
@strawberry.type
class Query:
@strawberry.field
def user(self) -> User:
return User(name="Patrick")
schema = strawberry.Schema(query=Query)
query = "{ user { name } }"
result = schema.execute_sync(query)
assert not result.errors
assert result.data
assert result.data["user"]["name"] == "Patrick"
def test_can_change_default_resolver():
@strawberry.type
class User:
name: str
@strawberry.type
class Query:
@strawberry.field
def user(self) -> User:
return {"name": "Patrick"} # type: ignore
schema = strawberry.Schema(
query=Query,
config=StrawberryConfig(default_resolver=getitem),
)
query = "{ user { name } }"
result = schema.execute_sync(query)
assert not result.errors
assert result.data
assert result.data["user"]["name"] == "Patrick"
def test_field_metadata():
@strawberry.type
class Query:
a: str = strawberry.field(default="Example", metadata={"Foo": "Bar"})
(a,) = dataclasses.fields(Query)
assert a.metadata == {"Foo": "Bar"}
def test_field_type_priority():
"""Prioritise the field annotation on the class over the resolver annotation."""
def my_resolver() -> str:
return "1.33"
@strawberry.type
class Query:
a: float = strawberry.field(resolver=my_resolver)
schema = strawberry.Schema(Query)
expected = """
type Query {
a: Float!
}
"""
assert print_schema(schema) == textwrap.dedent(expected).strip()
query = "{ a }"
result = schema.execute_sync(query, root_value=Query())
assert not result.errors
assert result.data == {
"a": 1.33,
}
def test_field_type_override():
@strawberry.type
class Query:
a: float = strawberry.field(graphql_type=str)
b = strawberry.field(graphql_type=int)
@strawberry.field(graphql_type=float)
def c(self):
return "3.4"
schema = strawberry.Schema(Query)
expected = """
type Query {
a: String!
b: Int!
c: Float!
}
"""
assert print_schema(schema) == textwrap.dedent(expected).strip()
query = "{ a, b, c }"
result = schema.execute_sync(query, root_value=Query(a=1.33, b=2))
assert not result.errors
assert result.data == {
"a": "1.33",
"b": 2,
"c": 3.4,
}
def test_field_type_default():
@strawberry.type
class User:
name: str = "James"
@strawberry.type
class Query:
@strawberry.field
def a(self) -> User:
return User()
schema = strawberry.Schema(Query)
expected = """
type Query {
a: User!
}
type User {
name: String!
}
"""
assert print_schema(schema) == textwrap.dedent(expected).strip()
|