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
Shared Types Across Subgraphs in GraphQL
📖 Scenario: You are building a GraphQL API for an online bookstore. The API is split into two subgraphs: Books and Authors. Both subgraphs need to share a common type called Author to link books with their authors.This project will guide you through creating shared types across these subgraphs to enable smooth data fetching and type consistency.
🎯 Goal: Create two GraphQL subgraphs named Books and Authors. Define a shared Author type that both subgraphs use. Implement the necessary schema directives and type extensions to share the Author type properly.
📋 What You'll Learn
Create a Books subgraph schema with a Book type referencing Author
Create an Authors subgraph schema defining the Author type
Use the @key directive on the shared Author type for federation
Extend the Author type in the Books subgraph to reference it
Ensure the schemas are compatible for Apollo Federation
💡 Why This Matters
🌍 Real World
Large applications often split their GraphQL APIs into smaller subgraphs for better modularity and team collaboration. Sharing types across these subgraphs allows consistent data representation and smooth integration.
💼 Career
Understanding how to share types and use Apollo Federation directives is essential for backend developers working with GraphQL in microservices or modular API architectures.
Progress0 / 4 steps
1
Create the Authors subgraph schema with the shared Author type
Create a GraphQL schema string called authorsSchema that defines a type Author with fields id: ID! and name: String!. Add the @key(fields: "id") directive to the Author type to mark it as a federated entity.
GraphQL
Hint
Use the @key directive on the Author type with fields: "id" to enable federation.
2
Create the Books subgraph schema with Book type and extend Author
Create a GraphQL schema string called booksSchema that defines a type Book with fields id: ID!, title: String!, and author: Author!. Extend the Author type with @key(fields: "id") directive, and add the id: ID! @external field inside the extension.
GraphQL
Hint
Remember to use extend type Author with @key and mark the id field as @external in the Books subgraph.
3
Add a resolver placeholder for the author field in Book type
Create a JavaScript object called booksResolvers with a Book field resolver for author. The resolver should accept book as the first argument and return an object with __typename: "Author" and id equal to book.authorId.
GraphQL
Hint
The resolver returns a reference to the shared Author type using __typename and the id field.
4
Complete the federation setup by exporting schemas and resolvers
Export the authorsSchema, booksSchema, and booksResolvers variables using export statements so they can be used to build the federated GraphQL gateway.
GraphQL
Hint
Use a single export statement listing all three variables.
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
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.
Step 2: Differentiate from other directives
Other directives like @external or @deprecated serve different purposes, not unique identification.
Final Answer:
To mark fields that uniquely identify an entity across subgraphs -> Option A
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
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.
Step 2: Differentiate from other directives
@key marks unique identifiers, @requires and @provides relate to field dependencies, not external ownership.
Final Answer:
Use @external directive on the field -> Option D
Quick Check:
@external marks fields from other subgraphs [OK]
Hint: External fields use @external directive [OK]