0
0
GraphQLquery~30 mins

Subgraph definition in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a GraphQL Subgraph Definition
📖 Scenario: You are building a modular GraphQL API for a bookstore. Each part of the API is a subgraph that handles specific data. Your task is to define a subgraph schema for the Books part of the API.
🎯 Goal: Define a GraphQL subgraph schema for books with types and directives that enable it to work as a subgraph in a federated GraphQL architecture.
📋 What You'll Learn
Create a type Book with fields id (ID!), title (String!), and author (String!)
Add the @key(fields: "id") directive to Book to specify the primary key
Add the @extends directive to Book if extending from another subgraph (optional for this project)
Include the @external directive on fields that are resolved by other subgraphs (optional for this project)
Define the Query type with a field books returning a list of Book
💡 Why This Matters
🌍 Real World
GraphQL subgraphs allow teams to build modular APIs that can be composed into a single federated graph, improving scalability and collaboration.
💼 Career
Understanding subgraph definitions is essential for backend developers working with GraphQL federation in modern microservice architectures.
Progress0 / 4 steps
1
Define the Book type with fields
Create a GraphQL type called Book with fields id of type ID!, title of type String!, and author of type String!.
GraphQL
Need a hint?

Use the type keyword to define a GraphQL type. Fields have names and types separated by a colon.

2
Add the @key directive to Book
Add the @key(fields: "id") directive to the Book type to specify that id is the primary key for this subgraph.
GraphQL
Need a hint?

The @key directive is used to mark the primary key field(s) for federation.

3
Define the Query type with books field
Create a Query type with a field called books that returns a list of Book objects. Use square brackets [] to indicate a list.
GraphQL
Need a hint?

The Query type is the entry point for queries. Use square brackets to indicate a list of items.

4
Add the @extends and @external directives (optional)
Add the @extends directive to the Book type and the @external directive to the author field to indicate that author is resolved by another subgraph.
GraphQL
Need a hint?

The @extends directive marks a type as extending another subgraph's type. The @external directive marks fields resolved elsewhere.