Graphene Federation

raw JSON →
3.3.0 verified Fri May 01 auth: no python

Federation implementation for Graphene. Adds support for Apollo Federation (v1 and v2) to GraphQL schemas built with graphene. Current version 3.3.0 supports Federation v2.11 directives. Active development.

pip install graphene-federation
error ImportError: cannot import name 'enable_federation_2' from 'graphene_federation'
cause enable_federation_2 was removed in v3.2.0.
fix
Use federation_version parameter: schema = build_schema(query=Query, federation_version=2)
error NameError: name 'extend' is not defined
cause The @extend decorator was removed in v3.2.0.
fix
Replace @extend with @extends and ensure @key is present.
error TypeError: build_schema() got an unexpected keyword argument 'federation_2'
cause Old code still using enable_federation_2 pattern but upgrade to v3.2.0 removed it.
fix
Use build_schema(query=Query, federation_version=2) instead.
error AttributeError: module 'graphene_federation' has no attribute 'cost'
cause Using older version (<3.3.0) of graphene-federation that does not include 'cost' directive.
fix
Upgrade to v3.3.0: pip install --upgrade graphene-federation
breaking In v3.2.0, the @extend directive was removed. Replace with @extends + @key for V1, or just @key for V2.
fix Replace @extend decorator with @extends (and @key if not already present).
deprecated enable_federation_2() function is deprecated. Use federation_version parameter in build_schema() instead.
fix Remove enable_federation_2() call and pass federation_version=2 to build_schema().
gotcha Federation v2 requires all types to have @key directive. Missing @key on an entity type leads to runtime errors in Apollo Gateway.
fix Add @key to all entity types using '@key('id')' or other field combinations.
gotcha When using Federation v2 with @shareable, ensure that fields marked @shareable are resolved consistently across subgraphs or expect composition errors.
fix Define resolvers returning identical values for @shareable fields or avoid marking them as shareable if they differ.

Basic graphene federation schema using @extends and @key.

import graphene
from graphene_federation import build_schema, key, extends

@key('id')
@extends
class Product(graphene.ObjectType):
    id = graphene.ID(required=True)
    name = graphene.String()

class Query(graphene.ObjectType):
    product = graphene.Field(Product, id=graphene.ID(required=True))

    def resolve_product(self, info, id):
        return Product(id=id)

schema = build_schema(query=Query)
print(schema)