strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

`export-schema` not working in Django projects

Open atiberghien opened this issue 3 years ago • 7 comments

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.
Fund with Polar

atiberghien avatar Jan 17 '22 15:01 atiberghien

Hi @atiberghien, thanks for the bug report, I'll edit the issue so that it is clear we need a management command for django 😊

patrick91 avatar Jan 17 '22 16:01 patrick91

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 👍

HelmutKarsten avatar May 20 '22 10:05 HelmutKarsten

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 👍

@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 avatar Sep 21 '22 14:09 fescobar

@fescobar can you try with import strawberry.django at the top of your module?

patrick91 avatar Sep 21 '22 15:09 patrick91

@fescobar can you try with import strawberry.django at the top of your module?

It's working now, thank you for your quick response.

fescobar avatar Sep 21 '22 15:09 fescobar

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?

bradleyoesch avatar Aug 29 '23 16:08 bradleyoesch