0
0
GraphQLquery~20 mins

useQuery hook in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.