Skip to main content

Federation implementation for graphene

Project description

graphene-federation

Federation support for graphene

Build: CircleCI

Federation specs implementation on top of Python graphene lib https://www.apollographql.com/docs/apollo-server/federation/federation-spec/

Based on discussion: https://github.com/graphql-python/graphene/issues/953#issuecomment-508481652

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
        @key('id')
        @key('email')
        class User(ObjectType):
            id = Int(required=True)
            email = String()
        
            def __resolve_reference(self, info, **kwargs):
                if self.id is not None:
                    return User(id=self.id, email=f'name_{self.id}@gmail.com')
                return User(id=123, email=self.email)              
    
  • extend # extend remote types
  • external # mark field as external
  • requires # mark that field resolver requires other fields to be pre-fetched
  • provides # to annotate the expected returned fieldset from a field on a base type that is guaranteed to be selectable by the gateway.
    • Base class should be decorated with @provides as well as field on a base type that provides. Check example bellow:
        import graphene
        from graphene_federation import provides
        
        @provides
        class ArticleThatProvideAuthorAge(graphene.ObjectType):
            id = Int(required=True)
            text = String(required=True)
            author = provides(Field(User), fields='age')
    
import graphene
from graphene_federation import build_schema, key

@key(fields='id')  # mark File as Entity and add in EntityUnion https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#key
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):
        return self.name

    def __resolve_reference(self, info, **kwargs):  # https://www.apollographql.com/docs/apollo-server/api/apollo-federation/#__resolvereference
        return get_file_by_id(self.id)
import graphene
from graphene_federation import build_schema


class Query(graphene.ObjectType):
    ...
    pass

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

@extend(fields='id')
class Message(graphene.ObjectType):
    id = external(graphene.Int(required=True))

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

__resolve_reference

  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphene-federation-0.2.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

graphene_federation-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file graphene-federation-0.2.0.tar.gz.

File metadata

  • Download URL: graphene-federation-0.2.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.10

File hashes

Hashes for graphene-federation-0.2.0.tar.gz
Algorithm Hash digest
SHA256 659396592835c37b1debd844a8739478194dce59c7a5f44cd91c149abf8d2a42
MD5 97a3b7cd09cacc06f41c5f797c6fb007
BLAKE2b-256 fd4ab3b9d5b1e5f527c7b6b773993316ebc185f96b7cf10d22f0d22d4b6c1caf

See more details on using hashes here.

Provenance

File details

Details for the file graphene_federation-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_federation-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6fa83bfe12c3fbd823da758a5f73f9f87423982b898c841a1a570b7197ac56c
MD5 9dcfffc8dcae29722e2898042ff4e3af
BLAKE2b-256 e10e4d7be022ee2c6614be76111c653c2a235480953434f3c4798ee0f3d28729

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page