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

[question] How to use generated inner types? #570

Closed
vanyakosmos opened this issue Jan 13, 2019 · 2 comments
Closed

[question] How to use generated inner types? #570

vanyakosmos opened this issue Jan 13, 2019 · 2 comments

Comments

@vanyakosmos
Copy link

vanyakosmos commented Jan 13, 2019

For instance:

I have some django model:

class Foo(models.Model):
    field = models.CharField(max_length=255, choices=(('foo', 'Foo'), ('bar', 'Bar')))

Then I generate object type using DjangoObjectType:

class FooNode(DjangoObjectType):
    class Meta:
        model = Foo

Graphene will create Enum type for that field named FooField (I can see it in docs in graphiql).
But how can I use this type inside my code? How can I extract it from FooNode?

@vanyakosmos
Copy link
Author

vanyakosmos commented Jan 14, 2019

Something like this maybe?

class Post(model.Model):
    status = models.CharField(max_length=255, choices=(('foo', 'Foo'), ('bar', 'Bar')))

class PostNode(DjangoObjectType):
    class Meta:
        model = Post

# ✨🍰✨graphene.Enum
PostStatus = PostNode._meta.fields['status'].type

class Query(graphene.ObjectType):
    stuff_based_on_status = graphene.Int(status=PostStatus)

We need some confirmation that these hacky solutions (such as above or as in #240) are OK, so you could close some issues: #193 #240 #471 (and probably more)

@franzwarning
Copy link

franzwarning commented Jun 22, 2021

@vanyakosmos This saved my day. Perhaps we should document this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants