strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.85k stars 510 forks source link

`export-schema` not working in Django projects #1583

Open atiberghien opened 2 years ago

atiberghien commented 2 years ago

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

Fund with Polar

patrick91 commented 2 years ago

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

HelmutKarsten commented 2 years ago

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 👍

fescobar commented 1 year ago

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?

patrick91 commented 1 year ago

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

fescobar commented 1 year ago

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

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

bradleyoesch commented 10 months ago

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?

ihavemadefire commented 2 months ago

In addition to throwing an error, could this be corrected by adding the workaround as part of the documentation?

I'm working on issue 463 to add the documentation for the export_schema functionality. If we include the workaround in the docs it could clear up confusion from users.