Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues running example #14

Closed
cs01 opened this issue Apr 27, 2019 · 7 comments
Closed

issues running example #14

cs01 opened this issue Apr 27, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@cs01
Copy link

cs01 commented Apr 27, 2019

I am trying to get the example set up in react-example, but am running into some issues.

When running the example tartiflette app, I get:

>> uvicorn server.asgi:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [13587]
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 304, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 311, in serve
    config.load()
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/config.py", line 179, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./server/asgi.py", line 2, in <module>
    from .app import app
  File "./server/app.py", line 3, in <module>
    from tartiflette_starlette import TartifletteApp
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/tartiflette_starlette/__init__.py", line 1, in <module>
    from .app import TartifletteApp
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/tartiflette_starlette/app.py", line 5, in <module>
    from .graphql import GraphQLHandler
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/tartiflette_starlette/graphql.py", line 18, in <module>
    with open(os.path.join(CURDIR, "graphiql.html")) as tpl_file:
FileNotFoundError: [Errno 2] No such file or directory: '/home/csmith/git/react-example/venv/lib/python3.7/site-packages/tartiflette_starlette/graphiql.html'

I add the graphiql.html file by copying and pasting its contents.

Then I get:

>> uvicorn server.asgi:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [13898]
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 304, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 311, in serve
    config.load()
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/config.py", line 179, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./server/asgi.py", line 5, in <module>
    configure(app, settings)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/bocadillo/config.py", line 102, in configure
    plugin(app)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/bocadillo/plugins.py", line 46, in use_providers
    app.on("startup", STORE.enter_session)
AttributeError: 'TartifletteApp' object has no attribute 'on'
INFO: Stopping reloader process [13898]

Output of pip freeze:

aiodine==1.2.5
bocadillo==0.15.0
certifi==2019.3.9
cffi==1.12.3
chardet==3.0.4
Click==7.0
h11==0.8.1
httptools==0.0.13
idna==2.8
Jinja2==2.10.1
lark-parser==0.6.4
MarkupSafe==1.1.1
pkg-resources==0.0.0
pycparser==2.19
python-multipart==0.0.5
pytz==2019.1
requests==2.21.0
six==1.12.0
starlette==0.12.0b3
tartiflette==0.8.9
tartiflette-starlette==0.1.0
typesystem==0.2.2
urllib3==1.24.2
uvicorn==0.7.0
uvloop==0.12.2
websockets==7.0
whitenoise==4.1.2
@florimondmanca
Copy link
Contributor

(For other people reading this, @cs01 is referring to https://github.com/bocadilloproject/react-example which uses Bocadillo.)

For graphiql.html, it seems to be an issue with assets not being packaged correctly. Will investigate!

For the rest, it seems to me you are exposing the naked TartifletteApp to Bocadillo — TartifletteApp isn't a Bocadillo App, and so you're getting the error with .on(), plugin configuration etc.

Did you see this section: Mount onto an ASGI application?

@cs01
Copy link
Author

cs01 commented Apr 27, 2019

The docs say

As such, it [TartifletteApp] can be served on its own using any ASGI web server, or it can be mounted onto another ASGI application.

I was going for the former, which I didn't think required Bocadillo. After I got that working I was going to try to mount it onto a bocadillo app.

This is the code I'm trying to run (copied directly from the README):

# graphql.py
from tartiflette import Resolver

from tartiflette_starlette import TartifletteApp

# Create a Tartiflette resolver for the `hello` field.
@Resolver("Query.hello")
async def resolve_hello(parent, args, context, info):
    name = args["name"]
    return f"Hello, {name}!"

# Define the schema using an SDL string.
# Note: Tartiflette also has support for `.graphql` files.
# See: https://tartiflette.io/docs/api/engine
sdl = """
    type Query {
        hello(name: String): String
    }
"""

app = TartifletteApp(sdl=sdl, path="/graphql")

@florimondmanca
Copy link
Contributor

Hmm. The command you are running is uvicorn server.asgi:app --reload. So, I suppose in asgi.py, configure(app) is called with app being the one you listed above, correct? If so, you can't do that, because configure() only accepts Bocadillo apps.

Instead, you need to "mount" the Tartiflette app onto a Bocadillo app, i.e.:

# app.py
from bocadillo import App
from .graphql import app as graphql_app   # <- graphql.py copy-pasted from README

app = App()
app.mount("/", graphql_app)

(Here, "mounted" should really means "sub-routed", and that depends on how the framework of choice implements this mechanism. Maybe tartiflette-starlette docs should be clearer about this?)

@florimondmanca florimondmanca added the bug Something isn't working label Apr 27, 2019
@cs01
Copy link
Author

cs01 commented Apr 27, 2019

Thanks! Yes I was serving the tartiflette app from app.py (I just overwrote it).

I was actually working on what you just described and moved the tartiflette app to graphql.py.

I'm getting the following. Here is my branch https://github.com/bocadilloproject/react-example/tree/graphql.

>> uvicorn server.asgi:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [14900]
Process SpawnProcess-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 304, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/main.py", line 311, in serve
    config.load()
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/config.py", line 179, in load
    self.loaded_app = import_from_string(self.app)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./server/asgi.py", line 5, in <module>
    configure(app, settings)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/bocadillo/config.py", line 102, in configure
    plugin(app)
  File "/home/csmith/git/react-example/venv/lib/python3.7/site-packages/bocadillo/plugins.py", line 46, in use_providers
    app.on("startup", STORE.enter_session)
AttributeError: 'Starlette' object has no attribute 'on'
INFO: Stopping reloader process [14900]

@cs01
Copy link
Author

cs01 commented Apr 27, 2019

Ah I see what I was doing wrong now. Like you said, the uvicorn command shouldn't have had asgi in it. It works perfectly with uvicorn server.graphql:app. I need to read the instructions more carefully 😄.

I still don't know why the Bocadillo app is reporting AttributeError: 'Starlette' object has no attribute 'on'. Am I doing something wrong there?

@florimondmanca
Copy link
Contributor

Cool! I moved the graphiql.html bug to a separate issue: #15. Will close this. :-)

I still don't know why the Bocadillo app is reporting AttributeError: 'Starlette' object has no attribute 'on'. Am I doing something wrong there?

Because it's not a Bocadillo app. 😉 You created a Starlette app in your app.py script, but you need a Bocadillo App — see the code in my previous answer.

@cs01
Copy link
Author

cs01 commented Apr 27, 2019

Ah yes I just ran it again without the asgi and that one worked too. I was originally trying to run the Bocadillo app and switched back and forth. It's my first time running Starlette so I have a bit of muscle memory for Bocadillo. 😄 thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants