Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_directive_can_have_required_argument():
type_defs = """
directive @test(arg: String) on FIELD_DEFINITION
type Query {
hello: String @test(arg: "OK!")
}
"""
query = QueryType()
query.set_field("hello", lambda *_: "hello world")
schema = make_executable_schema(
type_defs, [query], directives={"test": ReturnValueDirective}
)
result = graphql_sync(schema, "{ hello }")
assert result.errors is None
assert result.data == {"hello": "OK!"}
hello: String
test(data: Int): Boolean
}
"""
query = QueryType()
query.set_field("hello", lambda *_: "World!")
extending_query = QueryType()
@extending_query.field("test")
def resolve_test(*_, data): # pylint: disable=unused-variable
assert data == 4
return True
schema = make_executable_schema(type_defs, [query, extending_query])
result = graphql_sync(schema, "{ hello test(data: 4) }")
assert result.errors is None
assert result.data == {"hello": "World!", "test": True}
raise ValueError()
formatted_date = ast.value
parsed_datetime = datetime.strptime(formatted_date, "%Y-%m-%d")
return parsed_datetime.date()
scalar_with_default_parser = ScalarType("ScalarWithDefaultParser")
@scalar_with_default_parser.value_parser
def parse_value_from_default_literal_parser(value):
return type(value).__name__
schema = make_executable_schema(
type_defs, [query, datereadonly, dateinput, scalar_with_default_parser]
)
def test_attempt_bind_scalar_to_undefined_type_raises_error():
schema = build_schema(type_defs)
scalar = ScalarType("Test")
with pytest.raises(ValueError):
scalar.bind_to_schema(schema)
def test_attempt_bind_scalar_to_invalid_schema_type_raises_error():
schema = build_schema(type_defs)
scalar = ScalarType("Query")
with pytest.raises(ValueError):
scalar.bind_to_schema(schema)
type Query {
user: User
}
type User {
username: String
}
"""
query = QueryType()
query.set_field("user", lambda *_: Mock(first_name="Joe"))
user = ObjectType("User")
user.set_alias("username", "first_name")
schema = make_executable_schema(type_defs, [query, user])
result = graphql_sync(schema, "{ user { username } }")
assert result.errors is None
assert result.data == {"user": {"username": "Joe"}}
def test_int_enum_arg_is_transformed_to_internal_value():
query = QueryType()
query.set_field("testEnum", lambda *_, value: value == PyIntEnum.NEWHOPE)
schema = make_executable_schema([enum_definition, enum_param], [query, int_enum])
result = graphql_sync(schema, "{ testEnum(value: NEWHOPE) }")
assert result.data["testEnum"] is True
def test_query_errors_if_interface_didnt_resolve_the_type(
query_with_invalid_result, interface
):
schema = make_executable_schema(type_defs, [query_with_invalid_result, interface])
result = graphql_sync(schema, test_query)
assert result.data == {"result": None}
def test_successful_enum_value_passed_as_argument():
query = QueryType()
query.set_field("testEnum", lambda *_, value: True)
schema = make_executable_schema([enum_definition, enum_param], query)
result = graphql_sync(schema, "{ testEnum(value: %s) }" % "NEWHOPE")
assert result.errors is None, result.errors
def test_serialize_custom_scalar():
type_defs = """
scalar Date
type Query {
test: Date
}
"""
resolvers = {
"Query": {"test": lambda *_: date.today()},
"Date": {"serializer": lambda date: date.strftime("%Y-%m-%d")},
}
schema = make_executable_schema(type_defs, resolvers)
result = graphql(schema, "{ test }")
assert result.errors is None
assert result.data == {"test": date.today().strftime("%Y-%m-%d")}
def schema():
return make_executable_schema(type_defs)
def test_start_simple_server_from_inheriting_type_respects_inheritance(
type_defs, resolvers, make_server
):
class CustomGraphQL(GraphQL):
pass
schema = make_executable_schema(type_defs, resolvers)
start_simple_server(schema, server_class=CustomGraphQL)
make_server.assert_called_once()
called_with_args = make_server.call_args[0]
assert isinstance(called_with_args[2], CustomGraphQL)