0
0
GraphQLquery~20 mins

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

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