Federation implementation for graphene

graphene, gql, federation
pip install graphene-federation==0.0.6



Federation support for graphene

Build: CircleCI

Federation specs implementation on top of Python graphene lib

Based on discussion:

Supports now:

  • sdl (_service fields) # make possible to add schema in federation (as is)
  • @key decorator (entity support) # to perform Queries across service boundaries
    • You can use multiple @key per each ObjectType
        class User(ObjectType):
            id = Int(required=True)
            email = String()
            def __resolve_reference(self, info, **kwargs):
                if is not None:
                    return User(, email=f'name_{}')
                return User(id=123,              
  • extend # extend remote types
  • external # mark field as external
  • requires # mark that field resolver requires other fields to be pre-fetched

Todo implement:

  • @provides
import graphene
from graphene_federation import build_schema, key

@key(fields='id')  # mark File as Entity and add in EntityUnion
class File(graphene.ObjectType):
    id = graphene.Int(required=True)
    name = graphene.String()

    def resolve_id(self, info, **kwargs):
        return 1

    def resolve_name(self, info, **kwargs):

    def __resolve_reference(self, info, **kwargs):  #
        return get_file_by_id(
import graphene
from graphene_federation import build_schema

class Query(graphene.ObjectType):

schema = build_schema(Query)  # add _service{sdl} field in Query
import graphene
from graphene_federation import external, extend

class Message(graphene.ObjectType):
    id = external(graphene.Int(required=True))

    def resolve_id(self, **kwargs):
        return 1


  • Each type which is decorated with @key or @extend is added to _Entity union
  • __resolve_reference method can be defined for each type that is an entity. This method is called whenever an entity is requested as part of the fulfilling a query plan. If not explicitly defined, default resolver is used. Default resolver just creates instance of type with passed fieldset as kwargs, see entity.get_entity_query for more details
  • You should define __resolve_reference, if you need to extract object before passing it to fields resolvers (example: FileNode)
  • You should not define __resolve_reference, if fileds resolvers need only data passed in fieldset (example: FunnyText)
  • read more in official documentation

Known issues:

  1. decorators will not work properly
  • on fields with capitalised letters with auto_camelcase=True, for example: my_ABC_field = String()
  • on fields with custom names for example some_field = String(name='another_name')

For more details see examples

Or better check integration_tests

Also cool example of integration with Mongoengine

For contribution:

Run tests:

  • make test
  • if you've changed Dockerfile or requirements run make build before make test

Also, you can read about how we've come to federation at Preply here