# ruff: noqa: E402
import pytest

graphene = pytest.importorskip("graphene")

from responder.ext.graphql import GraphQLView


@pytest.fixture
def schema():
    class Query(graphene.ObjectType):
        hello = graphene.String(name=graphene.String(default_value="stranger"))

        def resolve_hello(self, info, name):
            return f"Hello {name}"

    return graphene.Schema(query=Query)


def test_graphql_schema_query_querying(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.get("http://;/?q={ hello }", headers={"Accept": "json"})
    assert r.status_code == 200
    assert r.json() == {"data": {"hello": "Hello stranger"}}


def test_graphql_schema_json_query(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.post("http://;/", json={"query": "{ hello }"})
    assert r.status_code < 300
    assert r.json() == {"data": {"hello": "Hello stranger"}}


def test_graphiql(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.get("http://;/", headers={"Accept": "text/html"})
    assert r.status_code < 300
    assert "GraphiQL" in r.text


def test_graphql_shorthand(api, schema):
    """Test the api.graphql() shorthand method."""
    api.graphql("/gql", schema=schema)
    r = api.requests.post("http://;/gql", json={"query": "{ hello }"})
    assert r.status_code < 300
    assert r.json() == {"data": {"hello": "Hello stranger"}}


def test_graphql_missing_query_key(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.post("http://;/", json={"not_query": "foo"})
    assert r.status_code == 400
    assert "errors" in r.json()


def test_graphql_query_param(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.get("http://;/?query={ hello }", headers={"Accept": "json"})
    assert r.json() == {"data": {"hello": "Hello stranger"}}


def test_graphql_error_response(api, schema):
    api.add_route("/", GraphQLView(schema=schema, api=api))
    r = api.requests.post("http://;/", json={"query": "{ nonexistent }"})
    assert "errors" in r.json()
