graphene-mongo not compatible with graphql-server due to GraphQLResolveInfo.context handling
Error description
Running the tutorial application at https://graphene-mongo.readthedocs.io/en/latest/tutorial.html with graphql-server[flask] instead of Flask-GraphQL installed results in the following error being returned
{
"errors": [
{
"message": "'dict' object has no attribute 'queryset'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"allEmployees"
]
}
],
"data": {
"allEmployees": null
}
}
Underlying Cause
graphql-server passes its GraphQLResolveInfo.context as a dict rather than an object with attribute getters and setters, which leads to an Exception whenever graphene-mongo attempts to set a queryset attribute on it
graphene-django and the old Flask-GraphQL pass a Request object as the GraphQLResolveInfo.context and graphene-mongo defaults the context to a graphene Context object if no context is specified
Workaround
Subclass graphql-server's GraphQLView to create a context that allows a queryset attribute to be set
class DictWithQuerySet(dict):
def __init__(self, *args, **kwargs):
super(DictWithQuerySet, self).__init__(*args, **kwargs)
self.queryset = None
class MyGraphQLView(GraphQLView):
def get_context(self):
context = super().get_context()
return DictWithQuerySet(context)