Skip to content

graphene-mongo not compatible with graphql-server due to GraphQLResolveInfo.context handling #198

Closed
@leonardwellthy

Description

@leonardwellthy

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

Specify a different default context when instantiating graphql-server's GraphQLView.

from collections import UserDict
from flask import Flask, request
from graphql_server.flask import GraphQLView

app = Flask(__name__)
app.add_url_rule(
    "/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True,
                                              context=UserDict())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions