strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

`strawberry export-schema` does not work with django project

Open K0Te opened this issue 1 year ago • 2 comments

Hi, I've tried running strawberry export-schema with my django project and it doesn't seem to work.

Describe the Bug

root@8f7852265221:/opt/app# strawberry export-schema zephyr.apps.currencies.schema
Traceback (most recent call last):
  File "/usr/local/bin/strawberry", line 8, in <module>
    sys.exit(run())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/strawberry/cli/commands/export_schema.py", line 21, in export_schema
    schema_symbol = load_schema(schema, app_dir)
  File "/usr/local/lib/python3.10/site-packages/strawberry/cli/utils/__init__.py", line 13, in load_schema
    schema_symbol = import_module_symbol(schema, default_symbol_name="schema")
  File "/usr/local/lib/python3.10/site-packages/strawberry/utils/importer.py", line 15, in import_module_symbol
    module = importlib.import_module(module_name)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/opt/app/./zephyr/apps/currencies/schema.py", line 12, in <module>
    from zephyr.apps.currencies.models import Currency as CurrencyModel
  File "/opt/app/./zephyr/apps/currencies/models.py", line 8, in <module>
    class Currency(Model):
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

System Information

Latest release - 0.4.0

strawberry-django-plus @ file:///root/.cache/pypoetry/artifacts/1d/f4/ab/e366ea0441b0105cd632d496e5d1fa2ecefcdc6e4bc0885b1e1e99f438/strawberry_django_plus-1.18.0-py3-none-any.whl
strawberry-graphql @ file:///root/.cache/pypoetry/artifacts/0c/29/03/bbbe577f7dc75c14bb6d712abe93e5befec5f12c24f3309d2c676c65c3/strawberry_graphql-0.117.1-py3-none-any.whl
strawberry-graphql-django @ file:///root/.cache/pypoetry/artifacts/fc/32/3a/11761a440dfbf08c126435a89e9e7d53f90148a9748dc7a5d49f83ce29/strawberry_graphql_django-0.4.0-py3-none-any.whl

K0Te avatar Aug 09 '22 17:08 K0Te

The issue here is that django needs to be set up to be able to generate the schema.

You can probably copy the management command I have in strawberry-django-plus: https://github.com/blb-ventures/strawberry-django-plus/blob/main/strawberry_django_plus/management/commands/export_schema.py

Then you generate it with manage.py export_schema

bellini666 avatar Aug 09 '22 20:08 bellini666

@bellini666 Thank you! I already have strawberry-django-plus as dependency, I've just added it to INSTALLED_APPS and now manage.py export_schema works. This should probably be a part of strawberry-graphql-django, as all documentation seems to recommend strawberry export-schema command.

K0Te avatar Aug 09 '22 22:08 K0Te

Looks like the conversation is moving to #280

pcraciunoiu avatar Jul 07 '23 23:07 pcraciunoiu