Reference resolving for federation support
Hello, I am currently working on a project where I use the Apollo Federation Gateway and graphene-federation to connect multiple Microservices. For each external Type I need to add the following resolver to the type.
def __resolve_reference(self, info, **kwargs):
return MY_MODEL.objects.get(id=self.id)
Couldn't we just add something like the following to the MongoEngineObjectType?
@classmethod
def __resolve_reference(cls, info):
return cls._meta.model.objects.get(id=self.id)
The only problem is that we need to resolve the reference by the Types key which isn't alwasy the id (just in my case). It could be any property of the model.
Thanks!
@fabianriewe : thanks for late reply ..., it's okay to plug your idea to MongoengineObjectType, but before it, I have 2 quick questions need your feedback
-
there is something like as below, does it not fit your case? https://github.com/graphql-python/graphene-mongo/blob/9a894cc6aa49bfab064e7fb995e293b0048359e4/graphene_mongo/types.py#L209-L211
-
second, it maybe not a
@classmethodsince we needself, so it probably bedef __resolve_reference(self, info): return self._meta.model.objects.get(pk=self.id)
make sense?
The get_node function would fit the usecase. But it's required to call it '__resolve_reference' due to the Apollo Federation spec (https://www.apollographql.com/docs/apollo-server/federation/implementing-services/#gatsby-focus-wrapper). And yes we need to remove the @classmethod.
@fabianriewe : I have added it on branch feat-add-resolve_reference, would you please verify it fits your need or not? Thanks.
Hey! I looked deeper into this function and found out that we can improve it by using the following:
def __resolve_reference(self, info, **kwargs):
# construct filter for keys
keys = {}
for key in self._keys:
keys[key] = self.__getattribute__(key)
return self._meta.model.objects.get(**keys)
By using this the query is much more generic and doesn't rely on an key("id")