graphene-federation3
Federation support for graphene
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)
@keydecorator (entity support) # to perform Queries across service boundaries- You can use multiple
@keyper 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)
- You can use multiple
- 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
@providesas well as field on a base type that provides. Check example bellow:
import graphene from graphene_federation3 import provides @provides class ArticleThatProvideAuthorAge(graphene.ObjectType): id = Int(required=True) text = String(required=True) author = provides(Field(User), fields='age')
- Base class should be decorated with
import graphene
from graphene_federation3 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_federation3 import build_schema
class Query(graphene.ObjectType):
...
pass
schema = build_schema(Query) # add _service{sdl} field in Query
import graphene
from graphene_federation3 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
@keyor@extendis added to_Entityunion __resolve_referencemethod 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, seeentity.get_entity_queryfor 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:
- 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 buildbeforemake test
Also, you can read about how we've come to federation at Preply here
Metadata
Release files for graphene-federation3 0.3.0.1652187672
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| graphene-federation3-0.3.0.1652187672.tar.gz | 12.1 kB | Details |
Release files / graphene-federation3-0.3.0.1652187672.tar.gz
| Download URL | graphene-federation3-0.3.0.1652187672.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
906da52de1f6d4055f611f929fdd5dced59c7583126d46c94e9034f65bdbeb81
|
|
BLAKE2b-256 checksum How to use checksums |
0287cec3499582082e92dbd8d495664ad48f8fd55a6b309189b8de4e418ea77e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|