0
0
GraphQLquery~30 mins

Input validation patterns in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Input Validation Patterns in GraphQL
📖 Scenario: You are building a simple GraphQL API for a user registration system. You need to ensure that the inputs for creating a user are valid before saving them to the database.
🎯 Goal: Build a GraphQL schema with input validation patterns for user registration, including checks for username length, email format, and password strength.
📋 What You'll Learn
Create a GraphQL input type called CreateUserInput with fields username, email, and password.
Add validation rules for username to be at least 3 characters long.
Add validation rules for email to match a basic email pattern.
Add validation rules for password to be at least 8 characters long.
Create a mutation createUser that accepts CreateUserInput and returns a User type.
💡 Why This Matters
🌍 Real World
Input validation in GraphQL APIs is essential to prevent invalid or harmful data from entering your system, improving security and data quality.
💼 Career
Many backend developer roles require designing GraphQL schemas with proper input validation to build robust APIs.
Progress0 / 4 steps
1
Define the User type and CreateUserInput input type
Create a GraphQL User type with fields id (ID!), username (String!), email (String!). Then create an input type called CreateUserInput with fields username (String!), email (String!), and password (String!).
GraphQL
Need a hint?

Define the User type and CreateUserInput input type with the exact fields and types.

2
Add validation directives for username and email
Add validation directives to CreateUserInput: for username, add a directive to enforce minimum length of 3 characters. For email, add a directive to enforce a basic email pattern using a regex.
GraphQL
Need a hint?

Use directives like @length(min: 3) and @pattern(regex: "...") on the input fields.

3
Add validation directive for password length
Add a validation directive to the password field in CreateUserInput to enforce a minimum length of 8 characters.
GraphQL
Need a hint?

Add @length(min: 8) directive to the password field.

4
Create the createUser mutation
Add a mutation called createUser that accepts an argument input of type CreateUserInput! and returns a User!.
GraphQL
Need a hint?

Define the Mutation type with the createUser mutation accepting CreateUserInput! and returning User!.