0
0
GraphQLquery~20 mins

Input validation patterns in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Input Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this GraphQL query with input validation?

Given the following GraphQL schema snippet that validates an input argument age to be a positive integer, what will be the result of this query?

type Query {
  user(age: Int!): String
}

# Resolver logic:
# if age <= 0, return error "Invalid age"
# else return "User age is <age>"

Query:

{ user(age: -5) }
AError: age must be an integer
BUser age is -5
Cnull
DError: Invalid age
Attempts:
2 left
💡 Hint

Think about how the resolver handles invalid input values.

📝 Syntax
intermediate
2:00remaining
Which input validation directive syntax is correct in GraphQL?

Choose the correct syntax to validate that a string input email matches a simple email pattern using a custom directive @pattern.

Ainput UserInput { email: String! @pattern(regex: "^[^@\s]+@[^@\s]+\.[^@\s]+$") }
Binput UserInput { email: String! @pattern(value: "^[^@\s]+@[^@\s]+\.[^@\s]+$") }
Cinput UserInput { email: String! @validate(regex: "^[^@\s]+@[^@\s]+\.[^@\s]+$") }
Dinput UserInput { email: String! @pattern("^[^@\s]+@[^@\s]+\.[^@\s]+$") }
Attempts:
2 left
💡 Hint

Look for the correct argument name and syntax for directives.

🧠 Conceptual
advanced
2:00remaining
Why is input validation important in GraphQL APIs?

Which of the following is the best reason to implement input validation in GraphQL APIs?

ATo prevent invalid or malicious data from causing errors or security issues
BTo make queries run faster by skipping validation
CTo allow clients to send any data without restrictions
DTo reduce the size of the GraphQL schema
Attempts:
2 left
💡 Hint

Think about security and data integrity.

🔧 Debug
advanced
2:00remaining
Identify the error in this GraphQL input validation resolver

Consider this resolver function for a mutation that validates a username length:

async function createUser(parent, args) {
  if (args.username.length < 3) {
    throw new Error("Username too short");
  }
  // create user logic
  return { id: 1, username: args.username };
}

What error will occur if args.username is null?

ANo error, user created successfully
BError: Username too short
CTypeError: Cannot read property 'length' of null
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint

What happens when you try to access .length of null?

optimization
expert
3:00remaining
How to optimize input validation for nested GraphQL inputs?

You have a mutation that accepts a nested input object with multiple fields requiring validation. Which approach optimizes validation performance and maintainability?

AValidate all fields manually inside the resolver function with nested if statements
BUse reusable custom scalar types and directives for validation on each field
CSkip validation and trust client inputs to reduce server load
DValidate only the top-level input object and ignore nested fields
Attempts:
2 left
💡 Hint

Think about reusability and separation of concerns.