Welcome to the final lesson of our course! Here, we'll learn how to test GraphQL APIs, ensuring that your server is robust and reliable. Testing is crucial for maintaining the stability and functionality of your API as it evolves.
Let's define a simple schema involving books. This schema includes the Book
type, a Query
for retrieving books, and a Mutation
for adding a book.
TypeScript1const typeDefs = gql` 2 type Book { 3 id: ID! 4 title: String! 5 author: String! 6 } 7 8 type Query { 9 books: [Book] 10 } 11 12 type Mutation { 13 addBook(title: String!, author: String!): Book 14 } 15`;
Here's a brief explanation:
- Book: Represents a book with
id
,title
, andauthor
fields. - Query
books
: Fetches a list of books. - Mutation
addBook
: Adds a new book.
Let's implement the books
query and see how to test it. We'll use a simple array to store our books.
TypeScript1const books = [ 2 { id: '1', title: 'The Hobbit', author: 'J.R.R. Tolkien' }, 3 { id: '2', title: 'Harry Potter', author: 'J.K. Rowling' } 4]; 5 6const resolvers = { 7 Query: { 8 books: () => books 9 } 10};
Now, we'll use createTestClient
from apollo-server-testing
to test our books
query. This utility allows us to create an isolated instance of our Apollo Server for testing purposes. By creating a test client, we can programmatically run queries and mutations against our API without needing a running server instance.
Here's how it works:
-
Setup Apollo Server:
TypeScript1import { ApolloServer, gql } from 'apollo-server'; 2import { createTestClient } from 'apollo-server-testing'; 3 4const server = new ApolloServer({ typeDefs, resolvers }); 5const { query } = createTestClient(server as any);
-
Write and Execute the Test Query:
TypeScript1const queryBooks = async () => { 2 const res = await query({ 3 query: gql` 4 query { 5 books { 6 id 7 title 8 author 9 } 10 } 11 ` 12 }); 13 console.log(res.data); 14}; 15 16queryBooks();
When you run this code, createTestClient
sets up the server and allows you to send the books
query, receiving the results directly in your code for inspection. The expected output is:
JSON1{ 2 "data": { 3 "books": [ 4 { 5 "id": "1", 6 "title": "The Hobbit", 7 "author": "J.R.R. Tolkien" 8 }, 9 { 10 "id": "2", 11 "title": "Harry Potter", 12 "author": "J.K. Rowling" 13 } 14 ] 15 } 16}
This example demonstrates how to implement and test a simple query, ensuring your resolver functions as expected.
Next, let's implement the addBook
mutation and learn how to test it.
First, we define the resolver for the addBook
mutation.
TypeScript1const resolvers = { 2 Query: { 3 books: () => books 4 }, 5 Mutation: { 6 addBook: (_: any, { title, author }: { title: string, author: string }) => { 7 const newBook = { id: String(books.length + 1), title, author }; 8 books.push(newBook); 9 return newBook; 10 } 11 } 12};
To test this mutation, we use createTestClient
from the apollo-server-testing
package. Here's how to set up and test the addBook
mutation:
-
Create the Test Client:
TypeScript1import { ApolloServer, gql } from 'apollo-server'; 2import { createTestClient } from 'apollo-server-testing'; 3 4const server = new ApolloServer({ typeDefs, resolvers }); 5const { mutate } = createTestClient(server as any);
- This code sets up an isolated Apollo Server instance with your schema (
typeDefs
) and resolvers. createTestClient
is used to get methods likemutate
to perform mutations without needing a running server instance.
- This code sets up an isolated Apollo Server instance with your schema (
-
Write and Execute the Test Mutation:
TypeScript1const addBook = async () => { 2 const res = await mutate({ 3 mutation: gql` 4 mutation { 5 addBook(title: "1984", author: "George Orwell") { 6 id 7 title 8 author 9 } 10 } 11 ` 12 }); 13 console.log(res.data); 14}; 15 16addBook();
- This function sends the
addBook
mutation to the server and logs the response. - The mutation adds a new book with title "1984" and author "George Orwell".
- This function sends the
When you run this code, the test client executes the addBook
mutation, and you should see the following output:
JSON1{ 2 "data": { 3 "addBook": { 4 "id": "3", 5 "title": "1984", 6 "author": "George Orwell" 7 } 8 } 9}
This section demonstrates how to test a mutation by setting up a test client with Apollo Server and executing the mutation to ensure it performs correctly.
To sum up, in this lesson, you learned how to:
- Set up a basic GraphQL environment.
- Define a simple GraphQL schema.
- Implement and test queries and mutations using Apollo Server and
apollo-server-testing
.
These testing techniques are essential for ensuring that your GraphQL API remains robust as it scales.
Congratulations on completing the Comprehensive Intro to GraphQL in TypeScript course path! Now, you have a strong foundation in both creating and testing GraphQL APIs. Make sure to apply these skills in your future projects and dive deeper into more advanced topics as you grow your expertise. Happy coding!