05 / 05

What are fragments?

Fragments let you construct sets of fields, and then include them in queries where needed.

Operation Name:
  1. 1

    Fragments are the primary unit of composition in GraphQL.

  2. 2

    Fragments allow for the reuse of common repeated selections of fields, reducing duplicated text in the document.

if we wanted to fetch some common information about mutual friends as well as friends of some user:
Difficulty: 5/10
Topics: reusability, query composition, type safety

Scenario Questions

0-2 years experience
  1. 1

    You need to fetch a user's id, name, and email in two different components. How would you use a GraphQL fragment to avoid repeating the field selections?

  2. 2

    If you accidentally include a field in a fragment that doesn't exist on the target type, what error does the server return and how would you fix it?

  3. 3

    Given a simple query that repeats address fields, rewrite it using a named fragment for the address.

2-5 years experience
  1. 1

    Your team is adding a new UI widget that shares many fields with an existing product list query. Walk me through how you'd refactor the queries using fragments, and what trade‑offs you consider regarding cache normalization.

  2. 2

    During a code review you notice a fragment is being spread on a type that doesn't implement the required fields, causing a runtime error. How would you debug and resolve the issue?

  3. 3

    Explain how using fragments can affect the size of the response payload and what strategies you might employ if the fragment includes optional fields that are often null.

5-8 years experience
  1. 1

    At scale, our GraphQL gateway aggregates data from multiple micro‑services. How would you design a fragment strategy to ensure consistent field selection across services while minimizing duplication and supporting versioning?

  2. 2

    We observed that certain fragments cause N+1 queries in resolvers because they trigger additional data fetching. How would you identify and mitigate this performance problem?

  3. 3

    Discuss the implications of using inline fragments versus named fragments in a schema that evolves frequently, especially regarding client code generation and backward compatibility.

8+ years experience
  1. 1

    Our organization is migrating from a monolithic GraphQL server to a federated architecture. What considerations would you make for fragment definitions to avoid breaking existing clients, and how would you coordinate fragment ownership across teams?

  2. 2

    When introducing a shared UI component library that consumes GraphQL fragments, how would you establish governance, versioning, and deprecation policies to keep fragment contracts stable across many product lines?

  3. 3

    If a critical fragment becomes a bottleneck due to large nested selections, how would you redesign the schema or fragment usage to improve performance while preserving the developer experience?

Follow-up Questions

  • Can you walk me through a concrete fragment you wrote recently?
  • What are the main advantages and potential downsides of using fragments versus repeating fields?
  • How do fragments interact with GraphQL caching or client code generation?