Bird
Raised Fist0
GraphQLquery~20 mins

Integration tests with test server in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
GraphQL Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this GraphQL query in the test server?

Given a test server with a simple schema for books and authors, what will be the output of the following query?

{ books { title author { name } } }
GraphQL
query { books { title author { name } } }
ASyntaxError: Unexpected token in query
B[{"title": "1984"}, {"title": "Brave New World"}]
C[{"title": "1984", "author": null}, {"title": "Brave New World", "author": null}]
D[{"title": "1984", "author": {"name": "George Orwell"}}, {"title": "Brave New World", "author": {"name": "Aldous Huxley"}}]
Attempts:
2 left
💡 Hint

Remember the test server has mock data for books and their authors.

📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this GraphQL mutation?

Consider this mutation to add a new book:

mutation { addBook(title: "Dune", authorId: 3) { id title } }

Which option below will cause a syntax error when run on the test server?

Amutation { addBook(title: "Dune", authorId: 3) { id title } }
Bmutation { addBook(title: "Dune", authorId: 3) { id, title } }
Cmutation { addBook(title: "Dune" authorId: 3) { id title } }
D} } eltit di { )3 :dIrohtua ,"enuD" :eltit(kooBdda { noitatum
Attempts:
2 left
💡 Hint

Check the commas between arguments carefully.

optimization
advanced
2:00remaining
How to optimize this GraphQL query to reduce data fetched in integration tests?

This query fetches all fields of books and authors:

{ books { id title author { id name bio } } }

Which option optimizes the query to fetch only necessary fields for a test that checks book titles and author names?

A{ books { title author { name } } }
B{ books { id title author { id name bio } } }
C{ books { title author { bio } } }
D{ books { id author { id name } } }
Attempts:
2 left
💡 Hint

Fetch only fields needed for the test to reduce data size.

🔧 Debug
advanced
2:00remaining
Why does this integration test fail with a null author?

Test query:

{ books { title author { name } } }

Test server returns author as null for some books. What is the most likely cause?

AThe query syntax is incorrect causing author field to be null.
BThe test server's mock data has books without linked authors.
CThe test server does not support nested queries.
DThe client query is missing the author field.
Attempts:
2 left
💡 Hint

Check the mock data completeness in the test server.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using a test server for integration tests in GraphQL?

Choose the best explanation for why integration tests use a test server instead of the real backend.

AIt isolates tests from real backend changes and allows controlled data for predictable results.
BIt runs faster because it uses the real backend with caching enabled.
CIt avoids writing queries by generating data automatically.
DIt replaces the need for unit tests by testing everything at once.
Attempts:
2 left
💡 Hint

Think about test reliability and control over data.

Practice

(1/5)
1. What is the main purpose of using a test server in GraphQL integration tests?
easy
A. To speed up the production server
B. To run queries and mutations safely without affecting real data
C. To replace the database permanently
D. To generate random data automatically

Solution

  1. Step 1: Understand the role of a test server

    A test server is a safe environment that mimics the real server but does not affect actual data.
  2. Step 2: Identify the purpose in integration tests

    Integration tests use the test server to check if queries and mutations work together correctly without risk.
  3. Final Answer:

    To run queries and mutations safely without affecting real data -> Option B
  4. Quick Check:

    Test server = safe testing environment [OK]
Hint: Test server isolates tests from real data changes [OK]
Common Mistakes:
  • Thinking test server speeds up production
  • Confusing test server with permanent database replacement
  • Assuming test server auto-generates data
2. Which of the following is the correct way to start a test server for GraphQL integration tests using Apollo Server?
easy
A. const server = ApolloServer(); server.startServer();
B. const server = ApolloServer(typeDefs, resolvers); server.run();
C. const server = new ApolloServer({ typeDefs, resolvers }); await server.listen();
D. const server = new ApolloServer({ typeDefs, resolvers }); await server.start();

Solution

  1. Step 1: Recall Apollo Server setup

    Apollo Server requires creating an instance with typeDefs and resolvers, then calling start() before listen().
  2. Step 2: Identify correct method to start server

    The correct method to start the server is await server.start(); before running listen().
  3. Final Answer:

    const server = new ApolloServer({ typeDefs, resolvers }); await server.start(); -> Option D
  4. Quick Check:

    Use server.start() before listen() [OK]
Hint: Remember to call await server.start() before listen() [OK]
Common Mistakes:
  • Calling listen() without starting server
  • Using incorrect constructor syntax
  • Assuming server.run() or startServer() exist
3. Given this test code snippet for a GraphQL query on a test server:
const result = await server.executeOperation({ query: `query { user(id: 1) { name } }` }); console.log(result.data.user.name);
What will be printed if the user with id 1 has the name "Alice"?
medium
A. undefined
B. null
C. "Alice"
D. Error: user not found

Solution

  1. Step 1: Understand executeOperation result

    executeOperation returns an object with data containing the query result if successful.
  2. Step 2: Check the query and expected data

    The query requests user with id 1 and its name. If user exists with name "Alice", result.data.user.name will be "Alice".
  3. Final Answer:

    "Alice" -> Option C
  4. Quick Check:

    Query result matches user name "Alice" [OK]
Hint: executeOperation returns data object with query results [OK]
Common Mistakes:
  • Expecting undefined if user exists
  • Confusing null with undefined
  • Assuming error thrown instead of null result
4. You wrote this test code but it throws an error:
const result = await server.executeOperation({ query: `mutation { addUser(name: "Bob") { id } }` });
What is the most likely cause?
medium
A. The mutation name is incorrect or not defined in schema
B. The query should be a GET request, not mutation
C. executeOperation cannot run mutations
D. Missing await keyword before server.executeOperation

Solution

  1. Step 1: Check mutation usage in test

    executeOperation supports mutations, so that is not the issue.
  2. Step 2: Verify mutation name and schema

    If mutation name addUser is not defined in the schema, the server throws an error.
  3. Final Answer:

    The mutation name is incorrect or not defined in schema -> Option A
  4. Quick Check:

    Mutation must exist in schema to run [OK]
Hint: Check mutation name matches schema exactly [OK]
Common Mistakes:
  • Thinking executeOperation can't run mutations
  • Forgetting to await executeOperation
  • Confusing query and mutation types
5. You want to write an integration test that checks if a mutation correctly adds a user and then a query fetches that user. Which sequence correctly tests this on a GraphQL test server?
hard
A. Run mutation with executeOperation, then run query with executeOperation, check query result matches mutation data
B. Run query first, then mutation, then check mutation result
C. Run mutation and query in parallel without waiting, then check results
D. Only run mutation; query testing is unnecessary in integration tests

Solution

  1. Step 1: Understand integration test flow

    Integration tests verify that mutations affect data and queries reflect those changes.
  2. Step 2: Correct test sequence

    First run mutation to add user, then query to fetch user, then compare results to confirm correctness.
  3. Final Answer:

    Run mutation with executeOperation, then run query with executeOperation, check query result matches mutation data -> Option A
  4. Quick Check:

    Mutation then query to verify changes [OK]
Hint: Test mutation first, then query to confirm data change [OK]
Common Mistakes:
  • Running query before mutation
  • Running mutation and query in parallel without order
  • Skipping query test after mutation