# GraphQL error: Cannot return null for non-nullable field

- **ID:** `api/graphql-non-null-field-returned-null`
- **Domain:** api
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 82%

## Root Cause

A resolver returned null for a field marked as non-nullable (!) in the GraphQL schema, causing the parent field to also become null (null propagation).

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| graphql-js 16.8.0 | active | — | — |
| Apollo Server 4.9.0 | active | — | — |
| GraphQL Ruby 2.3.0 | active | — | — |
| HotChocolate 13.8.0 | active | — | — |

## Workarounds

1. **Identify the failing field from the error path (e.g., 'user.profile.name') and fix the resolver to return a valid value. Example: ensure the database query joins the required table or adds a fallback like `name: user.name || 'Unknown'`.** (90% success)
   ```
   Identify the failing field from the error path (e.g., 'user.profile.name') and fix the resolver to return a valid value. Example: ensure the database query joins the required table or adds a fallback like `name: user.name || 'Unknown'`.
   ```
2. **Temporarily make the field nullable in the schema (change `!` to `?` in SDL) to prevent null propagation while debugging the resolver.** (75% success)
   ```
   Temporarily make the field nullable in the schema (change `!` to `?` in SDL) to prevent null propagation while debugging the resolver.
   ```
3. **Add error handling in the resolver to log the null case and return a sensible default. For example, in JavaScript: `const name = user?.profile?.name ?? 'Anonymous';`.** (85% success)
   ```
   Add error handling in the resolver to log the null case and return a sensible default. For example, in JavaScript: `const name = user?.profile?.name ?? 'Anonymous';`.
   ```

## Dead Ends

- **** — Clients may not handle null gracefully, leading to runtime errors or missing data. (70% fail)
- **** — Default values can hide data integrity problems and produce incorrect results. (60% fail)
- **** — Returning null from a non-null field still triggers the same error, as GraphQL validates at the schema level. (80% fail)
