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_custom_root_value_is_passed_to_subscription_resolvers(schema):
app = GraphQL(schema, root_value={"test": "TEST-ROOT"})
client = TestClient(app)
with client.websocket_connect("/", "graphql-ws") as ws:
ws.send_json({"type": GQL_CONNECTION_INIT})
ws.send_json(
{
"type": GQL_START,
"id": "test1",
"payload": {"query": "subscription { testRoot }"},
}
)
response = ws.receive_json()
assert response["type"] == GQL_CONNECTION_ACK
response = ws.receive_json()
assert response["type"] == GQL_DATA
assert response["payload"] == {"data": {"testRoot": "TEST-ROOT"}}
def test_extension_from_option_are_passed_to_query_executor(schema):
app = GraphQL(schema, extensions=[CustomExtension])
client = TestClient(app)
response = client.post("/", json={"query": '{ hello(name: "BOB") }'})
assert response.json() == {"data": {"hello": "hello, bob!"}}
def test_custom_logger_is_used_to_log_subscription_resolver_error(schema, mocker):
logging_mock = mocker.patch("ariadne.logger.logging")
app = GraphQL(schema, logger="custom")
client = TestClient(app)
with client.websocket_connect("/", "graphql-ws") as ws:
ws.send_json({"type": GQL_CONNECTION_INIT})
ws.send_json(
{
"type": GQL_START,
"id": "test1",
"payload": {"query": "subscription { resolverError }"},
}
)
response = ws.receive_json()
assert response["type"] == GQL_CONNECTION_ACK
response = ws.receive_json()
assert response["type"] == GQL_DATA
logging_mock.getLogger.assert_called_once_with("custom")
def test_custom_logger_is_used_to_log_query_error(schema, mocker):
logging_mock = mocker.patch("ariadne.logger.logging")
app = GraphQL(schema, logger="custom")
execute_failing_query(app)
logging_mock.getLogger.assert_called_once_with("custom")
def test_playground_html_is_served_on_get_request(schema, snapshot):
app = GraphQL(schema)
client = TestClient(app)
response = client.get("/")
assert response.status_code == 200
snapshot.assert_match(response.text)
def test_custom_root_value_is_passed_to_query_resolvers(schema):
app = GraphQL(schema, root_value={"test": "TEST-ROOT"})
client = TestClient(app)
response = client.post("/", json={"query": "{ testRoot }"})
assert response.json() == {"data": {"testRoot": "TEST-ROOT"}}
def test_custom_root_value_function_is_called_with_context_value(schema):
get_root_value = Mock(return_value=True)
app = GraphQL(
schema, context_value={"test": "TEST-CONTEXT"}, root_value=get_root_value
)
client = TestClient(app)
client.post("/", json={"query": "{ status }"})
get_root_value.assert_called_once_with({"test": "TEST-CONTEXT"}, ANY)
def test_custom_root_value_function_is_called_by_subscription(schema):
get_root_value = Mock(return_value=True)
app = GraphQL(schema, root_value=get_root_value)
client = TestClient(app)
with client.websocket_connect("/", "graphql-ws") as ws:
ws.send_json({"type": GQL_CONNECTION_INIT})
ws.send_json(
{
"type": GQL_START,
"id": "test1",
"payload": {"query": "subscription { ping }"},
}
)
response = ws.receive_json()
assert response["type"] == GQL_CONNECTION_ACK
response = ws.receive_json()
assert response["type"] == GQL_DATA
get_root_value.assert_called_once()
@query.field("hello") # Query.hello
def resolve_hello(*_):
return "Hello DjangoCon!"
@mutation.field("add") # Mutation.add
def resolve_add(*_, a: int, b: int):
return a + b
# Create an executable GraphQL schema
schema = make_executable_schema(type_defs, [query, mutation])
# Create the ASGI app
app = GraphQL(schema, debug=True)
@query.field("books") # Query.books
def resolve_books(*_):
return [
{"title": "The Color of Magic", "year": 1983},
{"title": "The Light Fantastic", "year": 1986},
{"title": "Equal Rites", "year": 1987},
]
# Create an executable GraphQL schema
schema = make_executable_schema(type_defs, [query])
# Create the ASGI app
app = GraphQL(schema, debug=True)