0
0
GraphQLquery~20 mins

Inline fragments in GraphQL - Practice Problems & Coding Challenges

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