[FIXED] Using Dataloader at request level (graphene + tornado-graphql)

Issue

I'm trying to integrate GraphQL to my web service which was written in tornado (python). By using dataloaders, I can speed up my request and avoid sending multiple queries to my database. But the problem is I can't find any examples or the definition that equal the "context" variable at request level to store the GraphQLView. I found an example written in sanic refer to this link. Are there any definition in "tornado" that equal to "context" (get_context) in sanic ??? Or any examples to resolve the attributes like that:

class Bandwidth(ObjectType):
    class Meta:
        interfaces = (Service, )
    min_inbits_value = Field(Point)
    max_inbits_value = Field(Point)
    def resolve_min_inbits_value(context, resolve_info, arg1, arg2):

Solution

Finaly, I can access and modify the context at request level, I want to share how to I do it. I can wrapper the context propery in TornadoGraphQLHandler, but I need to parse the raw query:

from graphene_tornado import tornado_graphql_handler
from graphql import parse
class TornadoGraphQLHandler(tornado_graphql_handler.TornadoGraphQLHandler):
    @property
    def context(self):
        data = self.parse_body()
        query, variables, operation_name, id = self.get_graphql_params(self.request, data)
        try:
            document = parse(query)
            args = dict()
            for member in document.definitions[0].selection_set.selections[0].arguments:
                args[member.name.value] = member.value.value
            return <dataloaders with the arguments in request here>
        except:
            return self.request

By this way, I can access the dataloader by "info.context" in graphene in the next level.



Answered By - Kien.N
Answer Checked By - Mary Flores (Python.Easybugfix Volunteer)

Related Posts

Post a Comment