Docsx-graphql ReferenceSupergraph Entity Design

Supergraph Entity Design

The x-graphql-supergraph-name extension enables JSON Schemas to explicitly declare their role in a federated GraphQL supergraph, identifying base entity subgraphs that own type definitions and serve as query roots.


Problem Statement

In Apollo Federation, base/owner subgraphs define the primary entity type with @key, and extending subgraphs use @extends to add fields. Federation validators must otherwise infer ownership from directives alone — which becomes ambiguous when multiple schemas have @key directives, or when API gateways need explicit routing information.


Schema Extension Properties

PropertyTypeRequiredDescription
x-graphql-supergraph-namestringYesUnique subgraph identifier (e.g., "users-service")
x-graphql-supergraph-typeenumYesRole: base-entity | entity-extending | utility
x-graphql-supergraph-entitystringYesEntity type name (e.g., "User"). All schemas for the same entity must match.
x-graphql-supergraph-query-rootbooleanNoIf true, this subgraph handles root Query fields for this entity
x-graphql-supergraph-versionstringNoSchema version for this subgraph (e.g., "1.0.0")

Usage Examples

Base Entity Subgraph (Owner)

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User Entity - Base",
  "x-graphql-supergraph-name": "users-service",
  "x-graphql-supergraph-type": "base-entity",
  "x-graphql-supergraph-entity": "User",
  "x-graphql-supergraph-query-root": true,
  "type": "object",
  "x-graphql-type-name": "User",
  "x-graphql-directives": [
    { "name": "key", "arguments": { "fields": "\"user_id\"" } }
  ],
  "properties": {
    "user_id": { "type": "string", "x-graphql-type": "ID!" }
  }
}

Extending Entity Subgraph

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User Status - Extending",
  "x-graphql-supergraph-name": "user-status-service",
  "x-graphql-supergraph-type": "entity-extending",
  "x-graphql-supergraph-entity": "User",
  "x-graphql-supergraph-query-root": false,
  "type": "object",
  "x-graphql-type-name": "User",
  "x-graphql-directives": [
    { "name": "extends" },
    { "name": "key", "arguments": { "fields": "\"user_id\"" } }
  ],
  "properties": {
    "user_id": {
      "type": "string",
      "x-graphql-type": "ID!",
      "x-graphql-directives": [{ "name": "external" }]
    },
    "status": { "type": "string", "enum": ["ACTIVE", "INACTIVE"] }
  }
}

Utility Subgraph (Non-entity)

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Common Scalars",
  "x-graphql-supergraph-name": "shared-types",
  "x-graphql-supergraph-type": "utility",
  "x-graphql-supergraph-entity": null
}

Validation Rules

Base Entity Subgraphs

  1. Must have x-graphql-supergraph-type: "base-entity"
  2. Must have @key directive without @extends
  3. Must define Query type with entity resolver (when query-root: true)
  4. Entity type name must match x-graphql-supergraph-entity

Extending Entity Subgraphs

  1. Must have x-graphql-supergraph-type: "entity-extending"
  2. Must have @extends directive
  3. Must reference existing entity via x-graphql-supergraph-entity
  4. Must use @external for fields from the owner
  5. Must repeat the owner’s @key fields

Utility Subgraphs

  1. x-graphql-supergraph-entity should be null or omitted
  2. No @extends or @key required

Composition Algorithm

1. Identify all schemas with same x-graphql-supergraph-entity
2. Find base entity (x-graphql-supergraph-type: "base-entity")
3. Validate base entity has @key
4. Validate all extenders use @extends with matching @key fields
5. Route Query.entity() to base entity subgraph
6. Generate federation SDL with proper @external markers

Benefits

BenefitDescription
Explicit ownershipClear which subgraph owns each federated entity type
Better compositionValidators understand entity relationships and dependencies
Gateway routingAPI gateways know exactly which subgraph to query for each entity
Self-documentingMetadata makes federation design explicit and discoverable
Early error detectionCatch composition errors before runtime
Backward compatibleSchemas without this metadata fall back to @key/@extends inference