We use required fields with non-null (!) to make sure some data is always given. This helps keep data complete and reliable.
Required fields with non-null (!) in GraphQL
Introduction
Syntax
GraphQL
type TypeName {
fieldName: FieldType!
}The exclamation mark ! after the type means the field is required and cannot be null.
If you omit !, the field can be empty or missing.
Examples
id and name must always have values. email can be empty.GraphQL
type User {
id: ID!
name: String!
email: String
}name and price are required fields. description is optional.GraphQL
type Product {
name: String!
price: Float!
description: String
}Sample Program
This defines a Book type where title and author must always be provided. publishedYear is optional.
GraphQL
type Book {
title: String!
author: String!
publishedYear: Int
}Important Notes
Using ! helps catch errors early by forcing required data.
Be careful not to mark too many fields as required, or it can make data input hard.
Summary
Adding ! after a field type makes it required and non-null.
Required fields help keep your data complete and trustworthy.
Optional fields can be left empty or missing.
Practice
1. What does adding
! after a field type in GraphQL schema mean?easy
Solution
Step 1: Understand the role of
In GraphQL, adding!in GraphQL!after a type means the field is non-nullable, so it must have a value.Step 2: Interpret the meaning for data completeness
This means the field is required and cannot be left empty or null, ensuring data completeness.Final Answer:
The field is required and cannot be null -> Option AQuick Check:
Non-null!means required = C [OK]
Hint: Remember:
! means must have a value, no null allowed [OK]Common Mistakes:
- Thinking
!means optional - Confusing
!with list type - Assuming
!marks deprecated fields
2. Which of the following is the correct syntax to declare a required string field named
name in GraphQL?easy
Solution
Step 1: Recall GraphQL syntax for required fields
In GraphQL, to make a field required, you add!after the type, not before the type or after the field name.Step 2: Match the correct syntax
The correct syntax isname: String!meaning the fieldnameis a non-null string.Final Answer:
name: String!-> Option DQuick Check:
Required field syntax = type + ! after type [OK]
Hint: Put
! right after the type, never before field name [OK]Common Mistakes:
- Placing
!before the type - Adding
!after the field name - Omitting
!for required fields
3. Given this GraphQL type definition:
Which of these queries will cause an error if
type User {
id: ID!
email: String!
phone: String
}Which of these queries will cause an error if
email is missing in the data?medium
Solution
Step 1: Identify required fields in the type
The fieldsidandemailare marked with!, so they must not be null or missing.Step 2: Analyze queries requesting
If the data is missingemailemail, any query requesting it will cause an error because it's required.Final Answer:
Query requestingidandemail-> Option CQuick Check:
Requesting missing required field causes error = A [OK]
Hint: Queries asking for missing required fields cause errors [OK]
Common Mistakes:
- Assuming optional fields cause errors if missing
- Ignoring
!meaning required - Thinking missing required fields return null
4. Consider this GraphQL schema snippet:
Which change fixes the error when a query returns null for
type Product {
id: ID!
name: String!
description: String
}Which change fixes the error when a query returns null for
name?medium
Solution
Step 1: Understand the error cause
The error occurs becausenameis required (String!) but the query returns null, violating the non-null rule.Step 2: Fix by making
Removingnameoptional!fromnameallows it to be null, fixing the error.Final Answer:
Remove!fromnamefield -> Option BQuick Check:
Null returned for required field? Remove![OK]
Hint: If null causes error, remove
! to allow optional [OK]Common Mistakes:
- Adding
!to optional fields instead of removing - Changing unrelated fields
- Renaming fields without fixing null issue
5. You have this GraphQL input type:
You want to ensure
input NewUserInput {
username: String!
email: String!
phone: String
}You want to ensure
phone is also required but keep it optional for some mutations. How can you design this?hard
Solution
Step 1: Understand requirement for conditional field requirement
You wantphonerequired in some cases but optional in others, so a single input type cannot express both.Step 2: Use multiple input types for flexibility
Creating two input types--one withphone: String!and one withphone: String--lets you choose which to use per mutation.Final Answer:
Create two input types: one withphone: String!and one withphone: String-> Option AQuick Check:
Use multiple input types for conditional required fields [OK]
Hint: Use separate input types to toggle required fields flexibly [OK]
Common Mistakes:
- Trying to toggle
!dynamically in one input type - Removing required from always required fields
- Making all fields optional losing data integrity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
