def test_cyclic_import(): from .type_a import TypeA from .type_b import TypeB @strawberry.type class Query: a: TypeA b: TypeB expected = """ type Query { a: TypeA! b: TypeB! } type TypeA { listOfB: [TypeB!] typeB: TypeB! } type TypeB { typeA: TypeA! } """ schema = strawberry.Schema(Query) assert print_schema(schema) == textwrap.dedent(expected).strip()
def test_forward_reference(): global MyType @strawberry.type class Query: me: MyType = strawberry.field(name="myself") @strawberry.type class MyType: id: strawberry.ID expected_representation = """ type MyType { id: ID! } type Query { myself: MyType! } """ schema = strawberry.Schema(Query) assert print_schema(schema) == textwrap.dedent( expected_representation).strip() del MyType
def test_input_defaults(): @strawberry.input class MyInput: s: Optional[str] = None i: int = 0 x: Optional[int] = UNSET @strawberry.type class Query: @strawberry.field def search(self, input: MyInput) -> int: return input.i expected_type = """ input MyInput { s: String = null i: Int! = 0 x: Int } type Query { search(input: MyInput!): Int! } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_interface(): @strawberry.interface class Node: id: strawberry.ID @strawberry.type class User(Node): name: str @strawberry.type class Query: user: User expected_type = """ interface Node { id: ID! } type Query { user: User! } type User implements Node { id: ID! name: String! } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_input_simple_required_types(): @strawberry.input class MyInput: s: str i: int b: bool f: float id: strawberry.ID uid: UUID @strawberry.type class Query: @strawberry.field def search(self, input: MyInput) -> str: return input.s expected_type = """ input MyInput { s: String! i: Int! b: Boolean! f: Float! id: ID! uid: ID! } type Query { search(input: MyInput!): String! } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_simple_required_types(): @strawberry.type class Query: s: str i: int b: bool f: float id: strawberry.ID uid: UUID expected_type = """ type Query { s: String! i: Int! b: Boolean! f: Float! id: ID! uid: UUID! } scalar UUID """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_print_schema(): @strawberry.federation.type(keys=["upc"]) class Product: upc: str name: typing.Optional[str] price: typing.Optional[int] weight: typing.Optional[int] @strawberry.federation.type(extend=True) class Query: @strawberry.field def top_products(self, info, first: int) -> typing.List[Product]: return [] expected_representation = """ type Product @key(fields: "upc") { upc: String! name: String price: Int weight: Int } extend type Query { topProducts(first: Int!): [Product!]! } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent( expected_representation).strip()
def export_schema(schema: str): try: schema_symbol = import_module_symbol(schema, default_symbol_name="schema") except (ImportError, AttributeError) as exc: message = str(exc) raise click.BadArgumentUsage(message) if not isinstance(schema_symbol, Schema): message = "The `schema` must be an instance of strawberry.Schema" raise click.BadArgumentUsage(message) print(print_schema(schema_symbol))
def test_optional(): @strawberry.type class Query: s: Optional[str] expected_type = """ type Query { s: String } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_printer_with_camel_case_off(): @strawberry.type class Query: hello_world: str expected_type = """ type Query { hello_world: String! } """ schema = strawberry.Schema( query=Query, config=StrawberryConfig(auto_camel_case=False) ) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_print_simple_directive(): @strawberry.schema_directive(locations=[Location.FIELD_DEFINITION]) class Sensitive: reason: str @strawberry.type class Query: first_name: str = strawberry.field(directives=[Sensitive(reason="GDPR")]) expected_type = """ type Query { firstName: String! @sensitive(reason: "GDPR") } """ schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_type).strip()
def test_lazy_enum(): @strawberry.type class Query: a: strawberry.LazyType["LazyEnum", "tests.schema.test_lazy_types.test_lazy_enums"] expected = """ enum LazyEnum { BREAD } type Query { a: LazyEnum! } """ schema = strawberry.Schema(Query) assert print_schema(schema) == textwrap.dedent(expected).strip()
def test_forward_reference(): @strawberry.type class MyType: id: strawberry.ID @strawberry.type class Query: me: MyType expected_representation = """ type MyType { id: ID! } type Query { me: MyType! } """ schema = strawberry.Schema(Query) assert print_schema(schema) == textwrap.dedent(expected_representation).strip()