strawberry
strawberry copied to clipboard
`export-schema` not working in Django projects
When running strawberry export-schema core.schema:schema in a django project, we get this error:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
To fix this we should create a management command (so that the django settings are properly configured before exporting the schema).
The management command should have the same API as export-schema :)
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
Hi @atiberghien, thanks for the bug report, I'll edit the issue so that it is clear we need a management command for django 😊
For those who need a quick work around while #1658 is not released:
add this to a management/commmands/ directory of your choice
see the django documentation for more information.
from django.core.management import BaseCommand
from strawberry.printer import print_schema
from config.schema import schema # import your schema here
class Command(BaseCommand):
help = 'Exports the strawberry graphql schema'
def handle(self, *args, **options):
print(print_schema(schema))
call your file export_schema.py
after this you can use it like so: python manage.py export_schema
or python manage.py export_schema > schema.graphql to save it to a file 👍
For those who need a quick work around while #1658 is not released:
add this to a
management/commmands/directory of your choice see the django documentation for more information.from django.core.management import BaseCommand from strawberry.printer import print_schema from config.schema import schema # import your schema here class Command(BaseCommand): help = 'Exports the strawberry graphql schema' def handle(self, *args, **options): print(print_schema(schema))call your file
export_schema.pyafter this you can use it like so:
python manage.py export_schemaorpython manage.py export_schema > schema.graphqlto save it to a file 👍
@HelmutKarsten
I'm trying to use your workaround to generate my schema, but in my case, I'm using @strawberry.django.type
@strawberry.django.type(MyModel)
class My Entity:
id: auto
When I tried to export using that command appears error:
@strawberry.django.type(MyModel)
AttributeError: module 'strawberry' has no attribute 'django'
Do you know how can I fix this?
@fescobar can you try with import strawberry.django at the top of your module?
@fescobar can you try with
import strawberry.djangoat the top of your module?
It's working now, thank you for your quick response.
Looking at this as a good first issue. I see https://github.com/strawberry-graphql/strawberry/pull/1658 with a draft and https://github.com/strawberry-graphql/strawberry-graphql-django/pull/299 already added it to strawberry-graphql-django. Should this and https://github.com/strawberry-graphql/strawberry/pull/1658 be closed?
Otherwise should we give some message if django.core.exceptions.ImproperlyConfigured is thrown to use the management command instead?