Bird
Raised Fist0
GraphQLquery~20 mins

Inline fragments in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Inline Fragments 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 inline fragments?

Given this GraphQL schema with types Book and Magazine implementing Publication, and this query:

{
  publications {
    title
    ... on Book {
      author
    }
    ... on Magazine {
      issue
    }
  }
}

Assuming the data has one Book titled "GraphQL Guide" by "Alice" and one Magazine titled "Tech Monthly" with issue 42, what is the query result?

A[{"title": "GraphQL Guide", "issue": 42}, {"title": "Tech Monthly", "author": "Alice"}]
B[{"title": "GraphQL Guide"}, {"title": "Tech Monthly"}]
C[{"title": "GraphQL Guide", "author": "Alice"}, {"title": "Tech Monthly", "issue": 42}]
D[{"author": "Alice"}, {"issue": 42}]
Attempts:
2 left
💡 Hint

Inline fragments allow you to select fields specific to a type when querying an interface or union.

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

Choose the best reason why inline fragments are used in GraphQL queries.

ATo paginate large lists of data efficiently.
BTo sort query results on the server side.
CTo define reusable fragments that can be used across multiple queries.
DTo conditionally select fields based on the runtime type of an object implementing an interface or union.
Attempts:
2 left
💡 Hint

Think about how GraphQL handles interfaces and unions with different possible types.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this GraphQL query using inline fragments

Which option contains a syntax error in the use of inline fragments?

{
  search(term: "GraphQL") {
    ... on Book {
      title
      author
    }
    ... Magazine {
      title
      issue
    }
  }
}
AThe fragment on <code>Book</code> should be named with a fragment name.
BThe fragment on <code>Magazine</code> is missing the keyword <code>on</code>.
CInline fragments cannot be used inside queries.
DThe <code>search</code> field cannot have inline fragments.
Attempts:
2 left
💡 Hint

Check the syntax for inline fragments carefully.

❓ optimization
advanced
1:30remaining
How do inline fragments improve query efficiency in GraphQL?

Which option best explains how inline fragments can optimize GraphQL queries?

AThey allow fetching only the fields relevant to the actual type, reducing unnecessary data transfer.
BThey automatically cache query results on the client side.
CThey enable batch loading of related objects in one request.
DThey compress the query string to reduce network size.
Attempts:
2 left
💡 Hint

Think about how selecting fields conditionally affects the data returned.

🔧 Debug
expert
2:30remaining
What error does this GraphQL query raise?

Given this query:

{
  items {
    ... on Product {
      id
      price
    }
    ... on Service {
      id
      duration
    }
    ... on Product {
      name
    }
  }
}

Assuming items returns a list of Product and Service types, what error will this query cause?

ANo error; the query runs successfully.
BSyntax error due to missing fragment names.
CRuntime error because <code>duration</code> is not a field of <code>Service</code>.
DDuplicate fragment type error because <code>Product</code> inline fragment is repeated.
Attempts:
2 left
💡 Hint

Inline fragments on the same type are allowed; GraphQL merges their field selections.

Practice

(1/5)
1. What is the main purpose of using inline fragments in a GraphQL query?
easy
A. To select fields conditionally based on the object type in a mixed list
B. To create new types in the schema
C. To update data on the server
D. To paginate query results

Solution

  1. Step 1: Understand inline fragments usage

    Inline fragments allow querying different fields depending on the type of an object in a list or union.
  2. Step 2: Identify the purpose in queries

    This lets you get specific fields for each type without separate queries.
  3. Final Answer:

    To select fields conditionally based on the object type in a mixed list -> Option A
  4. Quick Check:

    Inline fragments = conditional fields by type [OK]
Hint: Inline fragments pick fields by type inside mixed lists [OK]
Common Mistakes:
  • Thinking inline fragments create new types
  • Confusing inline fragments with mutations
  • Using inline fragments for pagination
2. Which of the following is the correct syntax for an inline fragment in GraphQL?
easy
A. ... on { TypeName field1 field2 }
B. ... TypeName { field1 field2 }
C. ... TypeName on { field1 field2 }
D. ... on TypeName { field1 field2 }

Solution

  1. Step 1: Recall inline fragment syntax

    The correct syntax uses three dots, then on, then the type name, followed by fields in braces.
  2. Step 2: Match options to syntax

    ... on TypeName { field1 field2 } matches this exactly: ... on TypeName { field1 field2 }.
  3. Final Answer:

    ... on TypeName { field1 field2 } -> Option D
  4. Quick Check:

    Inline fragment syntax = '... on TypeName { fields }' [OK]
Hint: Remember: three dots + 'on' + type name + braces [OK]
Common Mistakes:
  • Omitting the 'on' keyword
  • Placing type name inside braces incorrectly
  • Using wrong order of keywords
3. Given this GraphQL query snippet:
{
  searchItems {
    id
    ... on Book {
      author
    }
    ... on Movie {
      director
    }
  }
}

What fields will be returned for an item of type Movie?
medium
A. id and director
B. id and author
C. only director
D. only id

Solution

  1. Step 1: Identify fields outside and inside fragments

    The field id is always returned for all items. For Movie type, the inline fragment selects director.
  2. Step 2: Combine fields for Movie type

    So, the result for a Movie item includes id and director.
  3. Final Answer:

    id and director -> Option A
  4. Quick Check:

    Movie fields = id + director [OK]
Hint: Fields outside fragments always return; inside depends on type [OK]
Common Mistakes:
  • Assuming author field returns for Movie
  • Ignoring fields outside fragments
  • Thinking only fragment fields return
4. Consider this GraphQL query:
{
  items {
    ... on Book {
      title
      author
    }
    ... on Movie {
      title
      director
    }
  }
}

What is the error in this query?
medium
A. Cannot query both Book and Movie in the same query
B. Missing closing brace for the root field
C. Fields inside fragments must be unique across fragments
D. Inline fragments cannot be used without a type condition

Solution

  1. Step 1: Check query braces

    The query opens braces for items and two inline fragments but does not close the items field or the root query braces.
  2. Step 2: Identify missing closing braces

    Proper GraphQL syntax requires matching closing braces for all opened braces.
  3. Final Answer:

    Missing closing brace for the root field -> Option B
  4. Quick Check:

    All braces must close properly [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
  • Forgetting to close braces
  • Thinking inline fragments need unique fields
  • Believing you cannot query multiple types
5. You have a GraphQL schema with a union type SearchResult = Book | Movie | Author. You want to query a list of searchResults and get the title for Book, director for Movie, and name for Author. Which query correctly uses inline fragments to achieve this?
hard
A. { searchResults { ... { title director name } } }
B. { searchResults { title director name } }
C. { searchResults { ... on Book { title } ... on Movie { director } ... on Author { name } } }
D. { searchResults { ... on SearchResult { title director name } } }

Solution

  1. Step 1: Understand union type querying

    For a union, you must use inline fragments with ... on TypeName to get fields specific to each type.
  2. Step 2: Evaluate each option

    { searchResults { ... on Book { title } ... on Movie { director } ... on Author { name } } } correctly uses inline fragments for each type. { searchResults { title director name } } tries to get all fields directly, which fails. { searchResults { ... { title director name } } } has invalid syntax. { searchResults { ... on SearchResult { title director name } } } incorrectly uses the union name as a type condition.
  3. Final Answer:

    { searchResults { ... on Book { title } ... on Movie { director } ... on Author { name } } } -> Option C
  4. Quick Check:

    Use inline fragments per type in unions [OK]
Hint: Use '... on TypeName' for each union member [OK]
Common Mistakes:
  • Querying all fields without fragments on unions
  • Using union name as type condition
  • Incorrect fragment syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard