0
0
GraphQLquery~30 mins

Integration tests with test server in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Integration Tests with Test Server
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
Integration tests help developers verify that their GraphQL APIs work correctly before deploying to production. This reduces bugs and improves reliability.
💼 Career
Many software engineering roles require writing tests for APIs. Knowing how to set up test servers and write integration tests is a valuable skill for backend and full-stack developers.
Progress0 / 4 steps
1
Create a simple GraphQL schema with a Book type

Create a GraphQL schema string called typeDefs that defines a Book type with fields id (ID!), title (String!), and author (String!). Also define a Query type with a field book that takes an id argument of type ID! and returns a Book.

GraphQL
Need a hint?

Use backticks to create a multi-line string. Define the Book type and Query type exactly as described.

2
Create a resolver for the book query

Create a constant called resolvers that defines a Query resolver with a book function. This function takes parent, args, and returns a book object with id, title, and author fields. Return the book with id equal to args.id, title as "Test Book", and author as "John Doe".

GraphQL
Need a hint?

Define the resolvers object with a Query field. The book resolver returns a fixed book object using args.id.

3
Create the test server using ApolloServer

Create a constant called server that is a new ApolloServer instance. Pass typeDefs and resolvers as options to the constructor.

GraphQL
Need a hint?

Import ApolloServer and create a new instance with the schema and resolvers.

4
Write an integration test to query the book by ID

Write an async function called testBookQuery. Inside, call server.executeOperation with a query string that requests book(id: "1") and asks for id, title, and author. Await the result and assign it to a variable called response. Then check that response.data.book.id equals "1", response.data.book.title equals "Test Book", and response.data.book.author equals "John Doe".

GraphQL
Need a hint?

Use server.executeOperation with a query string. Check the response data fields for expected values.