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}
def test_same_type_resolver_maps_are_merged_into_executable_schema():
type_defs = """
type Query {
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}
def test_directive_can_have_optional_argument():
type_defs = """
directive @test(arg: String) on FIELD_DEFINITION
type Query {
hello: String @test
}
"""
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": None}
def test_custom_resolver_is_called_with_arguments_passed_with_query():
type_defs = """
type Query {
test(returnValue: Int!): Int
}
"""
query = ResolverMap("Query")
@query.field("test")
def resolve_test(*_, returnValue): # pylint: disable=unused-variable
assert returnValue == 4
return "42"
schema = make_executable_schema(type_defs, query)
result = graphql_sync(schema, "{ test(returnValue: 4) }")
assert result.errors is None
assert result.data == {"test": 42}
def test_custom_resolver_is_called_with_input_type_value_as_dict():
type_defs = """
type Query {
test(data: TestInput): Int
}
input TestInput {
value: Int
}
"""
query = ResolverMap("Query")
@query.field("test")
def resolve_test(*_, data): # pylint: disable=unused-variable
assert data == {"value": 4}
return "42"
schema = make_executable_schema(type_defs, query)
result = graphql_sync(schema, "{ test(data: { value: 4 }) }")
assert result.errors is None
assert result.data == {"test": 42}
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}