0
0
GraphQLquery~10 mins

Subgraph definition in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subgraph definition
Start Subgraph Definition
Define Types and Fields
Add @key Directive for Entity
Add @external Directive for Shared Fields
Add @requires or @provides for Field Dependencies
Export Subgraph Schema
Subgraph Ready for Federation
This flow shows how to define a GraphQL subgraph schema step-by-step for federation.
Execution Sample
GraphQL
type Product @key(fields: "id") {
  id: ID!
  name: String
  price: Float
  weight: Float @external
}
Defines a Product type with a key and marks weight as external to indicate it's owned by another subgraph.
Execution Table
StepActionSchema PartDirective AppliedEffect
1Define Product typetype Product { id, name, price, weight }NoneBasic type with fields
2Add @key directivetype Product @key(fields: "id")@key(fields: "id")Marks id as entity key
3Mark weight as externalweight: Float @external@externalIndicates weight is owned by another subgraph
4Complete subgraph schemaFull schema with directivesAll appliedSubgraph ready for federation
💡 All necessary directives applied, subgraph schema is valid and ready
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Product TypeundefinedDefined with fieldsAdded @key directiveMarked weight @externalFinal subgraph type definition
DirectivesNoneNone@key(fields: "id")@key + @externalAll directives applied
Key Moments - 3 Insights
Why do we add the @key directive to a type?
The @key directive marks which field(s) uniquely identify an entity across subgraphs, as shown in execution_table step 2.
What does the @external directive mean on a field?
It means the field is owned by another subgraph and is referenced here, as shown in execution_table step 3.
Can a subgraph define a type without any directives?
Yes, but it won't participate in federation properly. Directives like @key are needed for entity resolution, as shown in steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the @key directive added?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Refer to the 'Directive Applied' column in execution_table row for Step 2.
According to variable_tracker, what is the state of the Product Type after Step 3?
AAdded @key directive only
BMarked weight @external
CDefined with fields only
DNo directives applied
💡 Hint
Check the 'Product Type' row under 'After Step 3' in variable_tracker.
If we omit the @external directive on weight, what effect would it have?
ASchema becomes invalid
BWeight is ignored in federation
CWeight is treated as owned by this subgraph
DNo effect
💡 Hint
Recall the meaning of @external from key_moments and execution_table step 3.
Concept Snapshot
Subgraph Definition in GraphQL:
- Define types with fields
- Use @key(fields: "id") to mark entity keys
- Use @external on fields owned by other subgraphs
- Optionally use @requires/@provides for dependencies
- Export schema for federation
Full Transcript
Defining a subgraph in GraphQL involves creating types with fields and adding special directives. The @key directive marks the unique identifier for entities, enabling federation to recognize them. The @external directive marks fields that belong to other subgraphs but are referenced here. This process ensures the subgraph schema is ready to be combined with others in a federated graph. The execution table shows each step applying these directives, and the variable tracker shows how the type and directives evolve. Understanding these directives is key to building federated GraphQL services.