-
-
Notifications
You must be signed in to change notification settings - Fork 508
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
Add automatic type generation for Django models #137
Add automatic type generation for Django models #137
Conversation
Codecov Report
@@ Coverage Diff @@
## master #137 +/- ##
==========================================
- Coverage 90.65% 90.04% -0.61%
==========================================
Files 23 22 -1
Lines 492 432 -60
==========================================
- Hits 446 389 -57
+ Misses 46 43 -3 |
Awesome! I'm going to take a look at this pretty soon, thank you so much for this! I think we might have to spend a bit of time on choices, which seems to be a pain point in graphene (but this can be done, and probably should, in another PR) |
Thanks for adding the Here's a preview of the changelog: Added automatic GraphQL type generation for Django models. Usage: # models.py
from django.db import models
class Todo(models.Model):
name = models.CharField(max_length=250)
done = models.BooleanField(default=False)
# types.py
from todo.models import Todo
from strawberry.contrib.django.type import model_type
@model_type(model=Todo, fields=['id', 'name', 'done'])
class TodoType:
pass
# OR
@model_type
class TodoType:
class Meta:
model = Todo
fields = ['id', 'name', 'done']
# Generated type:
type TodoType {
id: ID!
name: String!
done: Boolean!
} |
@patrick91 No worries! Happy to contribute :) Can you expand a little on what needs to be done as far as choices are concerned? |
@jaydenwindle I think it might be useful to convert django choices to enums, but we need to investigate this a bit more (might not be worth the troubles) |
@patrick91 Got it, that makes sense. Auto conversion to enums would definitely be useful. I'll log an issue for adding choices support and we can brainstorm there. |
from strawberry.graphql import execute | ||
|
||
|
||
@pytest.mark.asyncio |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to test this with async?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe so, since it uses async
/await
. Otherwise pytest skips it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have graphql sync we can use ;) https://github.com/strawberry-graphql/strawberry/blob/master/tests/test_schema.py#L9
Co-Authored-By: Patrick Arminio <patrick.arminio@gmail.com>
Co-Authored-By: Patrick Arminio <patrick.arminio@gmail.com>
…rry into feature/model_types
…all django extra in dev, renamed django test model so pytest doesn't try to use it as a test
Would be great to see this merged. Whats needed to push this forward? Anything I can do to help? |
@sarendsen hey there! we are currently focusing on making strawberry a bit more stable, so we parked this for a bit. The last time I was thinking about this I wasn't sure about the API.
This looks inline with what strawberry does, but I think we need to find a way to handle additional fields, ie:
And then there's also to talk about providing data for editing/querying data. Would like to discuss these? We have a discord channel, I think it might be easier to chat/talk there :) https://discord.gg/3uQ2PaY |
Hi guys, just posed asking about this on another issue but found this one later. I see that there is already some work on adding support for Django models. I'd like to contribute to this. Do you think you can bring me up to speed? Does this support QuerySets? I think this is not in the Docs, as before coming here I did not find anything. I think a lot of docs are also missing in general. Happy to contribute writing docs too. Let me know how I can be helpful, Best |
@Rocamonde we put this on hold for the time being, as we are finishing up working on strawberry and getting it stable first. I saw you joined the discord channel, maybe we can discuss the next steps there. I'm finishing this PR #352, it should unblock this :) |
I'll close this for now, we have a discusson on pydantic here: #444 That will probably shape how we handle the django integration too in future :) |
This PR adds automatic GraphQL type generation for Django models.
Usage: