Bird
0
0

You have a GraphQL interface Vehicle implemented by Car and Bike. How would you write a query to get the make for all vehicles, and additionally get numDoors only for cars using inline fragments?

hard📝 Application Q8 of 15
GraphQL - Queries
You have a GraphQL interface Vehicle implemented by Car and Bike. How would you write a query to get the make for all vehicles, and additionally get numDoors only for cars using inline fragments?
A{ vehicles { make ... on Vehicle { numDoors } } }
B{ vehicles { make ... Car { numDoors } } }
C{ vehicles { make ... on Car { numDoors } } }
D{ vehicles { make ... on Bike { numDoors } } }
Step-by-Step Solution
Solution:
  1. Step 1: Query common fields outside fragments

    'make' is common to all vehicles, so it goes directly inside 'vehicles'.
  2. Step 2: Use inline fragment for Car-specific field

    Use '... on Car { numDoors }' to get 'numDoors' only for cars.
  3. Final Answer:

    { vehicles { make ... on Car { numDoors } } } -> Option C
  4. Quick Check:

    Inline fragment targets specific type fields [OK]
Quick Trick: Put common fields outside, type-specific inside inline fragments [OK]
Common Mistakes:
  • Omitting 'on' keyword
  • Using wrong type in inline fragment
  • Querying type-specific fields outside fragments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GraphQL Quizzes