Input validation helps make sure the data sent to a GraphQL server is correct and safe. It stops errors and bad data from causing problems.
Input validation patterns in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
type Mutation {
createUser(input: CreateUserInput!): User
}
input CreateUserInput {
username: String!
email: String!
age: Int
}
# Validation happens in resolver or with custom scalars/directivesGraphQL schema defines the shape of input but does not do all validation automatically.
Use custom scalars or directives for advanced validation like patterns or ranges.
input CreateUserInput {
username: String!
email: String!
age: Int
}scalar Email
input CreateUserInput {
username: String!
email: Email!
}directive @length(min: Int, max: Int) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
input CreateUserInput {
username: String! @length(min: 3, max: 15)
}This mutation creates a user after checking the input meets simple rules like username length and email format.
type Mutation {
createUser(input: CreateUserInput!): User
}
input CreateUserInput {
username: String!
email: String!
age: Int
}
type User {
id: ID!
username: String!
email: String!
age: Int
}
# Resolver example (pseudocode):
# if input.username.length < 3 then throw error
# if input.email does not match email pattern then throw error
# else create user and return
# Mutation example:
mutation {
createUser(input: {username: "Sam", email: "sam@example.com", age: 25}) {
id
username
email
age
}
}GraphQL schema types ensure basic type safety but do not replace full validation.
Use custom scalars or validation libraries in resolvers for better input checks.
Clear error messages help users fix their input mistakes quickly.
Input validation keeps data safe and correct before saving or processing.
GraphQL schema defines input shapes but advanced checks need custom code or scalars.
Use directives, custom scalars, or resolver logic to enforce rules like length or format.
Practice
Solution
Step 1: Understand input validation role
Input validation checks data before it is saved or used to avoid errors or security issues.Step 2: Identify main goal in GraphQL
GraphQL uses input validation to keep data safe and correct before processing.Final Answer:
To ensure data is safe and correct before processing -> Option AQuick Check:
Input validation = data safety and correctness [OK]
- Confusing validation with performance optimization
- Thinking validation auto-generates UI
- Assuming validation stores data
Solution
Step 1: Review GraphQL schema capabilities
GraphQL schemas can define custom scalar types to validate formats like email or date.Step 2: Eliminate invalid options
SQL queries, CSS, and HTML are unrelated to schema validation.Final Answer:
Using custom scalar types for specific formats -> Option DQuick Check:
Custom scalars = validation in schema [OK]
- Confusing schema with UI styling
- Trying to embed SQL in schema
- Using HTML tags in schema definitions
input UserInput {
username: String!
}
resolver createUser(input: UserInput) {
if (input.username.length === 0) {
throw new Error('Username cannot be empty');
}
return saveUser(input);
}Solution
Step 1: Analyze resolver input check
The resolver checks if username length is zero and throws an error if true.Step 2: Understand effect of empty string input
Empty string triggers the error, so user is not saved.Final Answer:
The resolver throws an error and user is not saved -> Option BQuick Check:
Empty username triggers error in resolver [OK]
- Assuming schema rejects empty string automatically
- Thinking empty username is saved
- Ignoring resolver error handling
input ProductInput {
price: Float!
}
resolver addProduct(input: ProductInput) {
if (input.price < 0) {
return 'Price must be positive';
}
saveProduct(input);
return 'Product added';
}Solution
Step 1: Check error handling in resolver
The resolver returns a string on error instead of throwing an error, which may not stop execution properly.Step 2: Understand proper error signaling
Throwing an error is standard to halt processing and signal failure in GraphQL.Final Answer:
Returning a string error instead of throwing an error -> Option AQuick Check:
Errors should be thrown, not returned as strings [OK]
- Returning error messages instead of throwing
- Confusing Float and Int types
- Ignoring zero price validation
Solution
Step 1: Understand validation needs
Email must be valid format and lowercase before saving or processing.Step 2: Choose best GraphQL validation method
Custom scalar can enforce format; resolver can transform input to lowercase.Step 3: Evaluate other options
Relying only on String misses validation; directives alone can't transform; database validation is late.Final Answer:
Use a custom scalar for email format and transform input to lowercase in resolver -> Option CQuick Check:
Custom scalar + resolver transform = best validation [OK]
- Skipping format validation
- Expecting directives to transform input
- Validating only after saving data
