0
0
GraphQLquery~20 mins

Fragments for reusable selections in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fragment Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this GraphQL query using fragments?

Given the following GraphQL schema and query, what will be the result of the query?

fragment userFields on User {
  id
  name
}

query {
  users {
    ...userFields
    email
  }
}
GraphQL
fragment userFields on User {
  id
  name
}

query {
  users {
    ...userFields
    email
  }
}
A[{"id": "1", "name": "Alice", "email": "alice@example.com"}, {"id": "2", "name": "Bob", "email": "bob@example.com"}]
B[{"id": "1", "name": "Alice"}, {"id": "2", "name": "Bob"}]
C[{"email": "alice@example.com"}, {"email": "bob@example.com"}]
DSyntaxError: Fragment 'userFields' is not defined
Attempts:
2 left
💡 Hint

Fragments allow you to reuse fields. The query includes the fragment and adds the email field.

🧠 Conceptual
intermediate
1:30remaining
Why use fragments in GraphQL queries?

What is the main benefit of using fragments in GraphQL queries?

ATo speed up the server response time by caching fragments
BTo automatically generate mutations from queries
CTo encrypt sensitive data in the query
DTo reuse sets of fields across multiple queries or parts of a query
Attempts:
2 left
💡 Hint

Think about how fragments help avoid repeating the same fields.

📝 Syntax
advanced
2:30remaining
Identify the syntax error in this fragment usage

Which option correctly fixes the syntax error in this GraphQL query?

fragment userFields on User {
  id
  name
}

query {
  users {
    ...userFields
    email
  }
  ...userFields
}
GraphQL
fragment userFields on User {
  id
  name
}

query {
  users {
    ...userFields
    email
  }
  ...userFields
}
ARemove the spread outside the users field; fragments can only be spread inside fields of matching type
BChange 'fragment userFields on User' to 'fragment userFields on Query'
CAdd parentheses after ...userFields like ...userFields()
DMove the fragment definition inside the query block
Attempts:
2 left
💡 Hint

Fragments must be spread only inside fields of the correct type.

optimization
advanced
2:00remaining
How do fragments improve query performance?

Which statement best describes how using fragments can improve GraphQL query performance?

AFragments automatically batch multiple queries into one request
BFragments reduce the size of the query sent over the network by avoiding repeated fields
CFragments enable the server to cache and reuse parsed query parts internally
DFragments compress the response data to reduce bandwidth
Attempts:
2 left
💡 Hint

Think about how the server processes queries with fragments.

🔧 Debug
expert
3:00remaining
Why does this fragment cause a runtime error?

Given this fragment and query, why does the query fail at runtime?

fragment postFields on Post {
  id
  title
  author {
    id
    name
  }
}

query {
  posts {
    ...postFields
    author {
      email
    }
  }
}
GraphQL
fragment postFields on Post {
  id
  title
  author {
    id
    name
  }
}

query {
  posts {
    ...postFields
    author {
      email
    }
  }
}
AThe fragment 'postFields' is missing the 'email' field for author, causing a null pointer error
BThe query tries to select 'author' fields twice with conflicting selections, causing a merge conflict error
CFragments cannot contain nested fields, so 'author' inside the fragment is invalid
DThe query is missing a required argument for 'posts', causing a validation error
Attempts:
2 left
💡 Hint

Consider how GraphQL merges fields with the same name but different selections.