Modern QA2026Error Handling in GraphQL — tiles
Log inJoin
43 / 65 · 14 API Testing Fundamentals · GraphQL Testing← prev⊞ allnext →☰ Read as one page

6.4Error Handling in GraphQL

GraphQL returns HTTP 200 even for many types of errors. The errors are in the response body:

def test_graphql_validation_error(api, base_url):
    """Invalid query syntax should return errors array."""
    r = requests.post(f"{base_url}/graphql",
        json={"query": "{ invalid syntax here }"},
        headers=api.headers)

    data = r.json()
    assert "errors" in data
    assert len(data["errors"]) > 0
    assert "message" in data["errors"][0]

def test_graphql_not_found(api, base_url):
    """Querying a non-existent resource."""
    query = """
    query { user(id: "nonexistent") { name, email } }
    """
    r = requests.post(f"{base_url}/graphql",
        json={"query": query},
        headers=api.headers)

    data = r.json()
    # Implementation-dependent: either errors array or null data
    assert data["data"]["user"] is None or "errors" in data

def test_graphql_partial_errors(api, base_url):
    """GraphQL can return partial data with errors."""
    query = """
    query {
        user(id: "123") { name }
        nonExistentField { data }
    }
    """
    r = requests.post(f"{base_url}/graphql",
        json={"query": query},
        headers=api.headers)

    data = r.json()
    # May have both data and errors
    if "errors" in data:
        for error in data["errors"]:
            assert "message" in error