Bird
Raised Fist0
GraphQLquery~20 mins

Shared types across subgraphs in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Federation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the purpose of shared types in GraphQL subgraphs?

In a GraphQL federated architecture, why do we use shared types across subgraphs?

ATo prevent any subgraph from accessing types defined in other subgraphs.
BTo duplicate type definitions in each subgraph for faster query execution.
CTo allow multiple subgraphs to reference the same type and ensure consistent data representation.
DTo enforce that each subgraph has completely unique types with no overlap.
Attempts:
2 left
💡 Hint

Think about how multiple teams might work on different parts of a graph but need to share common data structures.

query_result
intermediate
2:00remaining
What is the output of this federated schema query?

Given two subgraphs sharing a User type with a @key directive, what will this query return?

query {
  user(id: "123") {
    id
    name
    email
  }
}
GraphQL
Subgraph A defines User with fields id, name and @key(fields: "id").
Subgraph B extends User with field email.
The gateway composes the schema and runs the query above.
A{ "errors": [ { "message": "Field 'email' not found on type 'User'" } ] }
B{ "data": { "user": { "id": "123", "name": "Alice", "email": "alice@example.com" } } }
C{ "data": { "user": null } }
D{ "data": { "user": { "id": "123", "name": "Alice" } } }
Attempts:
2 left
💡 Hint

Remember that the gateway merges fields from all subgraphs that share the same key.

📝 Syntax
advanced
2:00remaining
Which subgraph schema snippet correctly shares a type with a @key directive?

Identify the valid GraphQL schema snippet that correctly defines a shared Product type with a @key directive for federation.

A
type Product @key(fields: "upc") {
  upc: String!
  name: String
  price: Float
}
B
type Product @key(fields: upc) {
  upc: String!
  name: String
  price: Float
}
C
type Product @key(fields: "upc") {
  upc: String
  name: String
  price: Float
}
D
type Product @key(fields: "upc") {
  upc: Int!
  name: String
  price: Float
}
Attempts:
2 left
💡 Hint

Check the syntax for the @key directive and the type of the key field.

optimization
advanced
2:00remaining
How to optimize resolving shared types across subgraphs?

When multiple subgraphs share a type, what is an effective way to optimize query performance at the gateway?

AFetch all fields from all subgraphs regardless of query selection to cache data.
BDuplicate all shared types in each subgraph to reduce gateway overhead.
CDisable schema stitching to speed up query execution.
DUse entity resolvers to fetch only required fields from each subgraph and avoid over-fetching.
Attempts:
2 left
💡 Hint

Think about how to minimize data transfer and processing by fetching only what is needed.

🔧 Debug
expert
2:30remaining
Why does this federated query fail with a 'Cannot find key fields' error?

Given this subgraph schema snippet:

extend type Review @key(fields: "id") {
  id: ID!
  body: String
}

And this query:

query {
  review(id: "r1") {
    id
    body
  }
}

Why does the gateway return an error: Cannot find key fields for type 'Review'?

ABecause the base <code>Review</code> type is not defined in any subgraph, only extended.
BBecause the <code>@key</code> directive is missing the correct field name.
CBecause the <code>id</code> field is not marked as non-nullable.
DBecause the query uses <code>review</code> instead of <code>reviews</code>.
Attempts:
2 left
💡 Hint

Consider what it means to extend a type without defining it first.

Practice

(1/5)
1. What is the main purpose of using @key in shared types across GraphQL subgraphs?
easy
A. To mark fields that uniquely identify an entity across subgraphs
B. To define a field as optional in the schema
C. To specify the data type of a field
D. To mark a field as deprecated

Solution

  1. Step 1: Understand the role of @key

    The @key directive marks fields that uniquely identify an entity across subgraphs, enabling them to share the same type.
  2. Step 2: Differentiate from other directives

    Other directives like @external or @deprecated serve different purposes, not unique identification.
  3. Final Answer:

    To mark fields that uniquely identify an entity across subgraphs -> Option A
  4. Quick Check:

    @key marks unique identifiers [OK]
Hint: Remember: @key means unique ID for shared types [OK]
Common Mistakes:
  • Confusing @key with @external
  • Thinking @key marks optional fields
  • Assuming @key defines data types
2. Which of the following is the correct way to mark a field as coming from another subgraph in a shared type?
easy
A. Use @key directive on the field
B. Use @provides directive on the field
C. Use @requires directive on the field
D. Use @external directive on the field

Solution

  1. Step 1: Identify the directive for external fields

    The @external directive marks fields that are owned by another subgraph but referenced in the current one.
  2. Step 2: Differentiate from other directives

    @key marks unique identifiers, @requires and @provides relate to field dependencies, not external ownership.
  3. Final Answer:

    Use @external directive on the field -> Option D
  4. Quick Check:

    @external marks fields from other subgraphs [OK]
Hint: External fields use @external directive [OK]
Common Mistakes:
  • Using @key instead of @external
  • Confusing @requires with @external
  • Not marking external fields at all
3. Given the following subgraph schema snippet:
type Product @key(fields: "id") {
  id: ID!
  name: String
  price: Float @external
}

Which statement is true about the price field?
medium
A. It is defined and owned by this subgraph
B. It is a unique identifier for Product
C. It is defined in another subgraph and referenced here
D. It is deprecated and should not be used

Solution

  1. Step 1: Analyze the @external directive on price

    The @external directive means price is not owned here but comes from another subgraph.
  2. Step 2: Understand the role of @key on id

    The id field is the unique identifier, so price is not an ID.
  3. Final Answer:

    It is defined in another subgraph and referenced here -> Option C
  4. Quick Check:

    @external means field is from another subgraph [OK]
Hint: Fields with @external come from other subgraphs [OK]
Common Mistakes:
  • Thinking @external means field is owned here
  • Confusing @key with @external
  • Assuming @external means deprecated
4. Consider this subgraph type definition:
type User @key(fields: "userId") {
  userId: ID!
  email: String @external
  name: String
}

Which statement is true about the email field?
medium
A. It is defined in another subgraph and referenced here
B. The @key directive must include email field
C. The userId field cannot be used as a key
D. The name field must be marked @external

Solution

  1. Step 1: Analyze the @external directive on email

    The @external directive means email is defined in another subgraph and referenced here.
  2. Step 2: Differentiate from other fields

    userId is the @key field provided locally, name is owned locally (no directive).
  3. Final Answer:

    It is defined in another subgraph and referenced here -> Option A
  4. Quick Check:

    @external means field from another subgraph [OK]
Hint: @external means field from another subgraph [OK]
Common Mistakes:
  • Thinking @external means owned locally
  • Believing @key must include all fields
  • Assuming all fields need @external
5. You have two subgraphs sharing a Book type. Subgraph A defines:
type Book @key(fields: "isbn") {
  isbn: ID!
  title: String
}

Subgraph B defines:
extend type Book @key(fields: "isbn") {
  isbn: ID! @external
  author: String
}

Which statement best describes how these shared types work together?
hard
A. Both subgraphs own isbn, causing a conflict
B. Subgraph A owns isbn and title, Subgraph B extends Book using isbn as key and adds author
C. Subgraph B owns isbn and author, Subgraph A only references isbn
D. Subgraph B cannot extend Book without redefining title

Solution

  1. Step 1: Identify ownership of fields

    Subgraph A defines Book with isbn and title, so it owns these fields.
  2. Step 2: Understand extension in Subgraph B

    Subgraph B extends Book, marking isbn as @external (owned by A) and adds author.
  3. Step 3: Confirm no conflicts

    Using @key with the same field isbn allows both subgraphs to share the type without conflict.
  4. Final Answer:

    Subgraph A owns isbn and title, Subgraph B extends Book using isbn as key and adds author -> Option B
  5. Quick Check:

    Extension uses @external keys to share types [OK]
Hint: Extension uses @external keys to share types [OK]
Common Mistakes:
  • Thinking both subgraphs own the same key field
  • Believing extension requires redefining all fields
  • Assuming conflicts occur with shared keys