graphql-python / graphql-server

This is the core package for using GraphQL in a custom server easily
MIT License
120 stars 72 forks source link

New version of graphql-core breaks package #29

Closed ms42Q closed 4 years ago

ms42Q commented 4 years ago

Description

I am using Flask-GraphQL and since graphql-core:3 was released I get an import error.

  File "app.py", line 3, in <module>
    from flask_graphql import GraphQLView
  File "/usr/local/lib/python3.7/site-packages/flask_graphql/__init__.py", line 1, in <module>
    from .blueprint import GraphQL
  File "/usr/local/lib/python3.7/site-packages/flask_graphql/blueprint.py", line 5, in <module>
    from .graphqlview import GraphQLView
  File "/usr/local/lib/python3.7/site-packages/flask_graphql/graphqlview.py", line 7, in <module>
    from graphql_server import (HttpQueryError, default_format_error,
  File "/usr/local/lib/python3.7/site-packages/graphql_server/__init__.py", line 5, in <module>
    from graphql import get_default_backend
ImportError: cannot import name 'get_default_backend' from 'graphql' (/usr/local/lib/python3.7/site-packages/graphql/__init__.py)

See:

https://raw.githubusercontent.com/graphql-python/graphql-server-core/7ff9553602cf33b5b137eaa6e8bff62dd1377043/graphql_server/__init__.py

Suggested change

Pin dependency to graphql-core==2.2.1 or even better upgrade to newer version of graphql-core.

Workaround

In my case pinning +graphql-core==2.2.1 worked.

ms42Q commented 4 years ago

See https://github.com/graphql-python/flask-graphql/issues/66

Cito commented 4 years ago

This is already fixed in master, but it needs to be released on PyPI - see #28.

Cito commented 4 years ago

This has now been released as v1.1.2. Let me know if there are still any problems.

pgzmnk commented 2 years ago

Pinning graphql-core to <3 might have seemed a quick fix 2 years ago.

The package is now at version 3.2.0 which significantly enhances async support.

What needs to happen to upgrade graphql-server to support graphql-core >3 ?

Cito commented 2 years ago

@pgzmnk v3.0.0b4 already supports graphql-core >= 3.1. What we need is people reporting whether there are still issues with this beta version or whether it works nicely, and when everything looks ok, somebody who would cut a final release. I am currently working on graphql-core 3.2, but would be willing to test and help after I have released that.

Cito commented 2 years ago

@jkimbo what do you think?

pgzmnk commented 2 years ago

@Cito This is great to hear.

Here's the scenario:

I'm upgrading a graphene-sqlalchemy Flask API to graphene==3. As you suggested, I installed graphql-server==3.0.0b4.

Python 3.9.7

pip freeze:

graphene==3.0
graphene-sqlalchemy==3.0.0b1
graphql-core==3.1.3
graphql-relay==3.1.0
graphql-server==3.0.0b4
graphql-server-core==2.0.0

app.py

import os

from flask import Flask
from flask_cors import CORS
from graphql_server.flask import GraphQLView
# from flask_graphql import GraphQLView # produces same error

import src.models
from src.extensions import db
from src.schema import schema

app = Flask(__name__)

app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_URI")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
CORS(app)

app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view(
        'graphql',
        schema=schema,
        graphiql=True
    )
)

db.init_app(app)

if __name__ == '__main__':
    app.run()

Log:

Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/demo_graphene/src/app.py", line 6, in <module>
    from graphql_server.flask import GraphQLView
  File "/demo_graphene/.venv/lib/python3.9/site-packages/graphql_server/__init__.py", line 15, in <module>
    from graphql import get_default_backend
ImportError: cannot import name 'get_default_backend' from 'graphql' (/demo_graphene/.venv/lib/python3.9/site-packages/graphql/__init__.py)

There seems to be an outdated import statement.

Relevant discussion: https://github.com/graphql-python/graphene/issues/1086

Cito commented 2 years ago

This looks like you have actually intstalled a very old (1.1.3 or so) version of graphql-server, 3.0.0b4 should not throw that error. Are you sure you ran the pip freeze with that demo_graphene/.venv activated?

pgzmnk commented 2 years ago

This is the observed output with a fresh .venv.

(.venv3) p@Ps-MacBook-Pro demo_graphene % pip freeze | grep graph
graphene==3.0
graphene-sqlalchemy==3.0.0b1
graphql-core==3.1.2
graphql-relay==3.1.0
graphql-server==3.0.0b4
graphql-server-core==2.0.0
(.venv3) p@Ps-MacBook-Pro demo_graphene % python3 -m src.app     
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/demo_graphene/src/app.py", line 5, in <module>
    from flask_graphql import GraphQLView
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/__init__.py", line 1, in <module>
    from .blueprint import GraphQL
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/blueprint.py", line 5, in <module>
    from .graphqlview import GraphQLView
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/graphqlview.py", line 7, in <module>
    from graphql_server import (HttpQueryError, default_format_error,
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/graphql_server/__init__.py", line 18, in <module>
    from graphql import get_default_backend
ImportError: cannot import name 'get_default_backend' from 'graphql' (/demo_graphene/.venv3/lib/python3.9/site-packages/graphql/__init__.py)

Uninstalling then reinstalling graphql-server then produces this different log, for the same pip freeze.

(.venv3) p@Ps-MacBook-Pro demo_graphene % pip freeze | grep graph            
graphene==3.0
graphene-sqlalchemy==3.0.0b1
graphql-core==3.1.2
graphql-relay==3.1.0
graphql-server==3.0.0b4
graphql-server-core==2.0.0
(.venv3) p@Ps-MacBook-Pro demo_graphene % python3 -m src.app                 
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/demo_graphene/src/app.py", line 5, in <module>
    from flask_graphql import GraphQLView
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/__init__.py", line 1, in <module>
    from .blueprint import GraphQL
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/blueprint.py", line 5, in <module>
    from .graphqlview import GraphQLView
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_graphql/graphqlview.py", line 7, in <module>
    from graphql_server import (HttpQueryError, default_format_error,
ImportError: cannot import name 'default_format_error' from 'graphql_server' (/demo_graphene/.venv3/lib/python3.9/site-packages/graphql_server/__init__.py)
pgzmnk commented 2 years ago

Setting format_error=None to temporarily overcome this issue results in:

Traceback (most recent call last):
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask_cors/extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/flask/views.py", line 83, in view
    self = view.view_class(*class_args, **class_kwargs)  # type: ignore
  File "/demo_graphene/.venv3/lib/python3.9/site-packages/graphql_server/flask/graphqlview.py", line 58, in __init__
    assert isinstance(
AssertionError: A Schema is required to be provided to GraphQLView.

I will look into an alternative to Flask's app.

Cito commented 2 years ago

In the second installation it looks like you have flask_graphql installed. I think that was only needed with v2 and is incompatible with v3. Flask support is included already in graphql-server v3. In you lust dump it looks right again, but maybe the schema is somehow not passed properly, or mabye it's not a v3 schema.

pgzmnk commented 2 years ago

Thank you @Cito

Setting schema to schema.graphql_schema resolved the last issue. Successfully upgraded to graphene 3.0.

app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view(
        'graphql',
        schema=schema.graphql_schema,
        graphiql=True
    )
)

Reference: https://issueexplorer.com/issue/graphql-python/graphene/1127#926804

Cito commented 2 years ago

Glad to hear @pgzmnk .

I have just commited 476edf370099df050289f9c0b8d70007e7dc8ecc that allows passing a Graphene schema as well.