Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
GraphQL Schema Testing Basics
📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to make sure your schema is correct before adding real data.
🎯 Goal: Create a GraphQL schema with a Book type and a Query type. Then write a test query to check the schema structure.
📋 What You'll Learn
Define a Book type with fields id (ID!), title (String!), and author (String!)
Define a Query type with a field books that returns a list of Book
Write a test query to fetch id and title of all books
Ensure the schema is syntactically valid and the test query matches the schema
💡 Why This Matters
🌍 Real World
GraphQL schemas define how clients can request data from APIs in a clear and structured way.
💼 Career
Understanding schema design and testing is essential for backend developers working with GraphQL APIs.
Progress0 / 4 steps
1
Define the Book type
Create a GraphQL type called Book with these exact fields: id of type ID!, title of type String!, and author of type String!.
GraphQL
Hint
Use the type keyword followed by the type name and curly braces to define fields.
2
Add the Query type
Add a GraphQL Query type with a field called books that returns a list of Book (use square brackets).
GraphQL
Hint
The Query type defines entry points for fetching data. Use square brackets to indicate a list.
3
Write a test query
Write a GraphQL query named GetBooks that fetches the id and title fields from the books query.
GraphQL
Hint
Use the query keyword followed by the query name and select the fields inside curly braces.
4
Complete the schema with a simple resolver placeholder
Add a placeholder resolver function for books that returns an empty array. Use JavaScript syntax for the resolver object with a Query field containing books as a function returning [].
GraphQL
Hint
Resolvers connect schema fields to data. Here, return an empty array to keep it simple.
Practice
(1/5)
1. What is the main purpose of schema testing in GraphQL?
easy
A. To check the database connection
B. To test the speed of GraphQL queries
C. To verify that the GraphQL schema matches the expected structure and types
D. To validate user authentication tokens
Solution
Step 1: Understand schema testing purpose
Schema testing ensures the GraphQL schema is correct and matches the design.
Step 2: Compare options to purpose
Only verifying schema structure and types matches schema testing's goal.
Final Answer:
To verify that the GraphQL schema matches the expected structure and types -> Option C
Quick Check:
Schema testing = verify schema structure [OK]
Hint: Schema testing checks schema structure, not performance or auth [OK]
But the test fails. What is the most likely cause?
medium
A. The User type does not have an email field defined
B. The getFields() method is not valid on schema types
C. The test syntax is incorrect and should use hasProperty
D. The schema variable is undefined
Solution
Step 1: Understand what getFields() returns
The getFields() method returns an object of fields defined on the type.
Step 2: Analyze test failure reason
If test fails checking for 'email', likely the User type lacks that field in schema definition.
Final Answer:
The User type does not have an email field defined -> Option A
Quick Check:
Missing field causes test failure [OK]
Hint: Test fails if field is missing in schema type [OK]
Common Mistakes:
Assuming method getFields() is invalid
Confusing test assertion method names
Not checking if schema variable is defined
5. You want to write a schema test to ensure the Post type has a field comments that returns a list of Comment types. Which test code correctly verifies this?
hard
A. expect(schema.getType('Post').getFields().comments.type.toString()).toBe('[Comment!]!')
B. expect(schema.getType('Post').getFields().comments.type.ofType.name).toBe('Comment')
C. expect(schema.getType('Post').getFields().comments.type.toString()).toBe('[Comment]')
D. expect(schema.getType('Post').getFields().comments.type.name).toBe('Comment')
Solution
Step 1: Understand GraphQL list and non-null syntax
A list of Comment types with non-null items and non-null list is represented as [Comment!]!.
Step 2: Match test code to expected type string
expect(schema.getType('Post').getFields().comments.type.toString()).toBe('[Comment!]!') checks the full type string including non-null markers, correctly verifying the list of non-null Comments.
Final Answer:
expect(schema.getType('Post').getFields().comments.type.toString()).toBe('[Comment!]!') -> Option A
Quick Check:
List of non-null Comments = '[Comment!]!' [OK]
Hint: Use toString() to check full list and non-null type syntax [OK]
Common Mistakes:
Checking only inner type name without list brackets