📋JSON Templates

GraphQL Response Format

Standard GraphQL response structure with data and errors.

Explanation

GraphQL responses always include data field, and optionally errors for partial failures.

Examples

Successful Response
Output
{
  "data": {
    "user": {
      "id": "usr_123",
      "name": "John Doe",
      "email": "john@example.com"
    }
  }
}
Response with Errors
Output
{
  "data": {
    "user": null
  },
  "errors": [
    {
      "message": "User not found",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "USER_NOT_FOUND"
      }
    }
  ]
}

Code Examples

TypeScript
interface GraphQLResponse<T = any> {
  data?: T;
  errors?: Array<{
    message: string;
    locations?: Array<{ line: number; column: number }>;
    path?: Array<string | number>;
    extensions?: Record<string, any>;
  }>;
}

// Handle response
function handleResponse<T>(response: GraphQLResponse<T>) {
  if (response.errors) {
    console.error('GraphQL errors:', response.errors);
    // Still might have partial data
  }
  
  return response.data;
}

Try it Now

💡 Tips

  • Responses can have both data and errors
  • Null data field indicates complete failure
  • Check errors array even if data exists
  • Use extensions for error codes
  • Path shows where error occurred in query

⚠️ Common Pitfalls

  • Don't assume data is present - check for errors
  • Partial data with errors is valid
  • HTTP status is usually 200 even with errors