📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to make sure that certain important fields like title and author of a book are always provided and never left empty.
🎯 Goal: Create a GraphQL schema that defines a Book type with required fields using the non-null ! symbol. Then write a query to fetch books ensuring the required fields are always returned.
📋 What You'll Learn
Define a Book type with title and author as required fields using !
Create a query type Query with a field books that returns a list of Book
Write a sample query to fetch title and author from books
💡 Why This Matters
🌍 Real World
GraphQL APIs often require certain fields to always be present to avoid errors and ensure data integrity, such as user names, product titles, or book authors.
💼 Career
Understanding how to use non-null fields in GraphQL is essential for backend developers and API designers to create robust and reliable APIs.
Progress0 / 4 steps
1
Define the Book type with required fields
Create a GraphQL type called Book with two fields: title and author. Both fields must be of type String! to make them required (non-null).
GraphQL
Hint
Use String! to mark a field as required (non-null).
2
Add the Query type with books field
Add a Query type with a field called books that returns a list of Book objects. Use [Book!]! to indicate the list and its items are required.
GraphQL
Hint
Use [Book!]! to say the list and each book inside it cannot be null.
3
Write a sample query to fetch required fields
Write a GraphQL query named GetBooks that fetches the title and author fields from the books query.
GraphQL
Hint
Inside the books field, select both title and author.
4
Complete the schema with required fields and query
Combine the Book type and Query type with the books field into a complete GraphQL schema. Ensure all required fields use ! and the query is ready to fetch title and author.
GraphQL
Hint
Make sure the schema and query are complete and consistent with required fields.
Practice
(1/5)
1. What does adding ! after a field type in GraphQL schema mean?
easy
A. The field is required and cannot be null
B. The field is optional and can be null
C. The field is deprecated
D. The field is a list type
Solution
Step 1: Understand the role of ! in GraphQL
In GraphQL, adding ! 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 A
Quick 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
A. name: String
B. name!: String
C. name: !String
D. name: String!
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 is name: String! meaning the field name is a non-null string.
Final Answer:
name: String! -> Option D
Quick 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:
type User {
id: ID!
email: String!
phone: String
}
Which of these queries will cause an error if email is missing in the data?
medium
A. Query requesting only phone
B. Query requesting id and phone only
C. Query requesting id and email
D. Query requesting only id
Solution
Step 1: Identify required fields in the type
The fields id and email are marked with !, so they must not be null or missing.
Step 2: Analyze queries requesting email
If the data is missing email, any query requesting it will cause an error because it's required.
Final Answer:
Query requesting id and email -> Option C
Quick 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:
type Product {
id: ID!
name: String!
description: String
}
Which change fixes the error when a query returns null for name?
medium
A. Add ! to description field
B. Remove ! from name field
C. Change id type to ID without !
D. Rename name field to title
Solution
Step 1: Understand the error cause
The error occurs because name is required (String!) but the query returns null, violating the non-null rule.
Step 2: Fix by making name optional
Removing ! from name allows it to be null, fixing the error.
Final Answer:
Remove ! from name field -> Option B
Quick Check:
Null returned for required field? Remove ! [OK]
Hint: If null causes error, remove ! to allow optional [OK]