Bird
Raised Fist0
GraphQLquery~20 mins

useQuery hook 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
🎖️
useQuery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this useQuery hook return?

Given the following React component using useQuery from Apollo Client, what will be the value of data after the query completes successfully?

GraphQL
import { useQuery, gql } from '@apollo/client';

const GET_USERS = gql`
  query GetUsers {
    users {
      id
      name
    }
  }
`;

function UsersList() {
  const { loading, error, data } = useQuery(GET_USERS);
  if (loading) return 'Loading...';
  if (error) return `Error! ${error.message}`;
  return data;
}
Aundefined
B{"users":[{"id":"1","name":"Alice"},{"id":"2","name":"Bob"}]}
Cnull
D{"users":[]}
Attempts:
2 left
💡 Hint

Think about what the data object contains after a successful query.

📝 Syntax
intermediate
2:00remaining
Identify the correct syntax in this useQuery usage

Which option contains correct syntax when using useQuery?

GraphQL
const { loading, error, data } = useQuery(GET_POSTS, { variables: { id: 1 } });
Aconst { loading, error, data } = useQuery(GET_POSTS, variables: { id: 1 });
Bconst { loading, error, data } = useQuery(GET_POSTS, { variables: { id = 1 } });
Cconst { loading, error, data } = useQuery(GET_POSTS, { variables: id: 1 });
Dconst { loading, error, data } = useQuery(GET_POSTS, { variables: { id: 1 } });
Attempts:
2 left
💡 Hint

Check the syntax of the options object passed to useQuery.

optimization
advanced
2:00remaining
How to prevent unnecessary re-fetching with useQuery?

You want to avoid re-fetching data when the component re-renders but the variables haven't changed. Which option correctly achieves this?

AUse <code>useQuery(GET_DATA, { fetchPolicy: 'no-cache' })</code>
BUse <code>useQuery(GET_DATA, { fetchPolicy: 'network-only' })</code>
CUse <code>useQuery(GET_DATA, { fetchPolicy: 'cache-first' })</code>
DUse <code>useQuery(GET_DATA, { fetchPolicy: 'cache-and-network' })</code>
Attempts:
2 left
💡 Hint

Think about which fetch policy uses cached data first to avoid network calls.

🔧 Debug
advanced
2:00remaining
Why does this useQuery hook cause an infinite loop?

Consider this React component:

function MyComponent() {
  const { data } = useQuery(GET_ITEMS, { variables: { filter: getFilter() } });
  return <div>{data?.items.length}</div>;
}

function getFilter() {
  return { active: true };
}

Why does this cause an infinite loop of queries?

ABecause <code>getFilter()</code> returns a new object every render, changing variables and triggering re-fetch.
BBecause <code>data</code> is undefined initially causing re-render.
CBecause <code>useQuery</code> does not accept functions in variables.
DBecause <code>filter</code> is missing a required field.
Attempts:
2 left
💡 Hint

Think about how React compares objects in dependencies.

🧠 Conceptual
expert
2:00remaining
What is the role of the 'loading' property in useQuery?

In the useQuery hook, what does the loading property indicate?

A<code>loading</code> is true while the query is in progress and false once it completes or errors.
B<code>loading</code> is true only if the query has an error.
C<code>loading</code> is true after the query completes successfully.
D<code>loading</code> is always false unless manually set.
Attempts:
2 left
💡 Hint

Consider the lifecycle of a query request.

Practice

(1/5)
1. What does the useQuery hook in GraphQL primarily do inside a React component?
easy
A. Fetches data from a GraphQL server and provides loading and error states
B. Updates data on the GraphQL server
C. Deletes data from the GraphQL server
D. Creates a new GraphQL schema

Solution

  1. Step 1: Understand the purpose of useQuery

    The useQuery hook is designed to run a GraphQL query and fetch data.
  2. Step 2: Recognize the states it provides

    It also provides loading and error states to manage UI feedback during data fetching.
  3. Final Answer:

    Fetches data from a GraphQL server and provides loading and error states -> Option A
  4. Quick Check:

    useQuery fetches data = A [OK]
Hint: useQuery fetches data and manages loading/error states [OK]
Common Mistakes:
  • Confusing useQuery with mutation hooks
  • Thinking useQuery updates or deletes data
  • Assuming useQuery creates schemas
2. Which of the following is the correct syntax to use the useQuery hook with a query named GET_USERS?
easy
A. useQuery(GET_USERS).then(response => ...);
B. const { data, loading, error } = useQuery(GET_USERS);
C. const data = useQuery(GET_USERS);
D. const { data, error } = useQuery(GET_USERS, loading);

Solution

  1. Step 1: Recall the useQuery return structure

    The useQuery hook returns an object with data, loading, and error properties.
  2. Step 2: Match the correct destructuring syntax

    const { data, loading, error } = useQuery(GET_USERS); correctly destructures these properties from the hook call.
  3. Final Answer:

    const { data, loading, error } = useQuery(GET_USERS); -> Option B
  4. Quick Check:

    Destructure data, loading, error = C [OK]
Hint: Destructure data, loading, error from useQuery call [OK]
Common Mistakes:
  • Not destructuring loading state
  • Using promises with useQuery (it's a hook)
  • Passing loading as argument
3. Given this code snippet:
const { data, loading, error } = useQuery(GET_POSTS);
if (loading) return 'Loading...';
if (error) return 'Error!';
return data.posts.length;

What will be the output if data.posts contains 5 posts?
medium
A. 5
B. 'Error!'
C. undefined
D. 'Loading...'

Solution

  1. Step 1: Check loading and error conditions

    The code returns 'Loading...' if loading is true and 'Error!' if error exists. Since data.posts has 5 posts, loading is false and error is null.
  2. Step 2: Return the length of posts array

    With no loading or error, the code returns data.posts.length, which is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    data.posts.length = 5 [OK]
Hint: Check loading/error first, then return data length [OK]
Common Mistakes:
  • Ignoring loading state and expecting data immediately
  • Confusing error with data
  • Returning undefined if data is not checked
4. Identify the error in this code using useQuery:
const { data, loading, error } = useQuery(GET_COMMENTS);
if (loading) return ;
if (error) return

Error occurred

;
return data.comments.map(c => <p>{c.text}</p>);
medium
A. The map function should use curly braces instead of parentheses
B. The component returns JSX before checking loading
C. The query name GET_COMMENTS is not imported
D. No error; code is correct

Solution

  1. Step 1: Check for common mistakes in useQuery usage

    The code uses GET_COMMENTS but does not show it being imported or defined, which is required.
  2. Step 2: Verify JSX and map usage

    The JSX and map syntax are correct; returning JSX conditionally is valid.
  3. Final Answer:

    The query name GET_COMMENTS is not imported -> Option C
  4. Quick Check:

    Missing query import = A [OK]
Hint: Always import your GraphQL queries before useQuery [OK]
Common Mistakes:
  • Forgetting to import the query
  • Returning JSX before loading check
  • Incorrect map syntax for JSX
5. You want to fetch a list of users but only display them after the data is fully loaded. Which pattern correctly uses useQuery to achieve this?
hard
A. const { data } = useQuery(GET_USERS); if (!loading) return data.users.map(u => u.name); else return 'Loading...';
B. const { loading } = useQuery(GET_USERS); if (loading) return data.users; else return 'Loading...';
C. const { data } = useQuery(GET_USERS); return data.users.map(u => u.name);
D. const { data, loading } = useQuery(GET_USERS); if (loading) return 'Loading...'; return data.users.map(u => u.name);

Solution

  1. Step 1: Understand loading state usage

    We must show 'Loading...' while data is loading, so check if loading is true first.
  2. Step 2: Return user list only after loading is false

    const { data, loading } = useQuery(GET_USERS); if (loading) return 'Loading...'; return data.users.map(u => u.name); correctly returns 'Loading...' if loading is true, else maps over data.users.
  3. Final Answer:

    const { data, loading } = useQuery(GET_USERS); if (loading) return 'Loading...'; return data.users.map(u => u.name); -> Option D
  4. Quick Check:

    Check loading first, then return data = B [OK]
Hint: Always check loading before accessing data [OK]
Common Mistakes:
  • Accessing data before loading is false
  • Returning data when loading is true
  • Ignoring loading state completely