-
Notifications
You must be signed in to change notification settings - Fork 766
Add setting for including foreignkey IDs #213
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 setting for including foreignkey IDs #213
Conversation
Does this seem like a feature you'd like to include in this repo? If so, I'll add some tests and documentation. If not, I'll just implement it for our project's graphql wrappers and close this PR. |
2 similar comments
I'm actually wondering if this would be nicer as a global setting? It's still a bit tedious to add that line to every DjangoObjectType Meta. |
bdc7189
to
f93251b
Compare
6d2e60e
to
377399d
Compare
@jkimbo @dopeboy @danpalmer |
This is a cool feature but it feels like a use case that isn't general enough to add a whole setting for. Could this not just be done with resolver methods? I'd like to tidy up some of the PRs, so if we still want to work on this let me know or I will close this in 1 week. |
Problem
By default, relations are only exposed as complex nested structures. So in order to get a related ID, you have to perform the join to the related field. Sometimes, only the id is needed at first, and the related object might be fetched by id later on an as-needed basis.
Solution
Add a new setting,
INCLUDE_FOREIGNKEY_IDS
that tells graphene-django to addgraphene.ID
fields for foreign keys.Utilize this new option in
construct_fields
to look forForeignKey
fields and add them along with their existing relational field.Current Behavior
Current Workaround
You can currently achieve the same behavior by simply adding the id attribute to your
DjangoObjectType
class explicitly.Usage after this PR
Turn on the setting
Then DjangoObjectTypes are simple
And then you can simply do:
And avoid making a join when you don't have to.
While the workaround isn't difficult, in a large project with lots of models and relations, it can be a lot to add. In our case, we want these IDs present on all models, so it makes sense to provide a more convenient approach.