How to Validate GraphQL Mutation Values Before Saving a Record
A useful starting point for GraphQL mutation input validation is a concrete question: which values may the server save? OWASP guidance recommends validating input against an allowlist, using specific types, and defining mutation input schemas. We will turn these recommendations into a contract for one operation and testable acceptance criteria. The example is entirely fictional: a lesson service using synthetic records under your control.
The Trust Boundary: From Input Values to a Saved Record
In this exercise, the trust boundary lies between the values received by the server and the data it is permitted to save. For the fictional updateLesson operation, we will set this requirement: the server validates both editable fields before saving. If either value is invalid, the operation must be rejected and the entire record must remain unchanged. This is a condition of our contract that needs to be tested, rather than a guarantee that automatically follows from OWASP guidance.
The deliverable is a short document specifying allowed values, rejection rules, and the expected state of the record. Developers can use it to implement the behavior, while product owners can use clear examples to assess whether it meets the acceptance criteria.
A Contract for One Mutation
Suppose updateLesson changes a lesson’s category and duration. In this example, both fields are required: the category accepts only THEORY or PRACTICE, and the duration must be an integer from 15 to 120 minutes, inclusive. The category names and numerical limits are invented for this exercise; they are not prescribed requirements.
Define an input schema with an enumeration for categories and a specific integer type for duration. OWASP recommends specific GraphQL types, including scalars and enumerations, and custom validators for complex checks. Declaring a numeric type alone is insufficient to satisfy our contract’s acceptance criteria: the 15–120 range must be expressed separately in validation and verified through tests. Range validation also aligns with OWASP’s general REST guidance; that guidance does not describe GraphQL semantics.
- Category: THEORY or PRACTICE only.
- Duration: an integer, with a minimum of 15 and a maximum of 120 minutes.
- Rejection: a clear error and no changes to any part of the record.
Positive Checks: What Actually Gets Saved
The following are original editorial tests, not an official OWASP procedure. Run them in your own test environment using synthetic records. Before each case, prepare a known initial state and retain it for comparison.
First, submit an allowed category and a duration of 60 minutes. After the request, read the saved record separately: both fields must exactly match the submitted values. Then repeat the check for 15 and 120 minutes. This confirms that both boundaries are included in the allowed range. A successful HTTP response alone is insufficient: the criterion here is the values actually saved.
Negative Checks Without Partial Saves
To test the category, submit UNKNOWN with a valid duration. Capture the entire synthetic record before the request, then read it separately afterward. Acceptance requires both outcomes: the request is rejected, and the record exactly matches its initial state. An error message alone does not confirm that nothing was saved.
Next, test 14 and 121 minutes separately. In each request, also propose changing the category to the other allowed value, for example from THEORY to PRACTICE. Expect rejection and an exact match with the record’s state before the request. If the category changes despite the duration being rejected, the implementation fails the example’s requirement to prevent partial saves. Begin each case from a prepared state so that earlier requests do not interfere with the comparison.
Errors as Part of the Contract
OWASP recommends rejecting invalid input without revealing excessive information about the API and its validation checks. Document your server’s specific error format and include it in the acceptance criteria. For this example, it is enough to say that the category is invalid or the duration is outside the allowed limits, without internal technical details.
Check that responses to UNKNOWN, 14, and 121 contain no stack traces or information about the implementation’s internal structure. OWASP’s REST guidance also recommends withholding these details from clients. However, do not treat REST HTTP status codes as universal rules for GraphQL: verify the agreed response for the specific operation and check the record’s state separately.
What to Fix First
The first priority is to agree on the contract and bring the implementation into line with it: define the input schema, category enumeration, and duration range validation. Every validation check in our example must finish before saving. If a negative test reveals a change to even one field, fix the save path and repeat that case, reading the result separately.
The next priority is to establish the positive and negative checks, then review the error content. Acceptance is complete when 60, 15, and 120 are saved with allowed categories; UNKNOWN, 14, and 121 are rejected without changing the record; and responses contain no internal technical details. Keep the initial and final values alongside the request results so the conclusion can be verified.
Testing Limits and the Next Step
These tests check only the input values of one mutation under the specified conditions. They do not cover authorization, the order of business process steps, GraphQL resource budgets, CSRF, or file handling. Passing the examples is not a statistical assessment of security and does not guarantee the security of the entire product.
Start with one operation and obtain an observable result: allowed values are saved, prohibited values are rejected, and the record remains unchanged after rejection. If you need help with this testing, you can request a free website or web-product audit, providing the mutation contract and test results from your own synthetic data.