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

Feature: Add setting DEFAULT_PK_FIELD_NAME #446

Merged
merged 14 commits into from
May 26, 2024

Conversation

noamsto
Copy link
Contributor

@noamsto noamsto commented Dec 25, 2023

This PR was inspired by #159.
I took a different approach which is less granular but easier to implement.
If a fine-grain control is needed it can be implemented in the future.

Description

Add a DEFAULT_PK_FIELD_NAME setting which default to "pk".
This should affect all builtin Django CRUDs, and is backward compatible.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Signed-off-by: Noam Stolero <nstolero@barracuda.com>
Signed-off-by: Noam Stolero <nstolero@barracuda.com>
@noamsto
Copy link
Contributor Author

noamsto commented Dec 25, 2023

@bellini666
What do you think of this?
I know #159 is abandoned and this is a partial solution, but I know I will use it at least :).

I can add more tests if you think are needed.

Also, need your help regarding this: https://github.com/strawberry-graphql/strawberry-graphql-django/pull/446/files#diff-87c07c1c9f6db39e2dacd257d14f3123d74b65d0a1db4029364d69f4a707013cR55

@noamsto noamsto changed the title Query set key attr Feature: Add setting DEFAULT_PK_FIELD_NAME Dec 25, 2023
Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noamsto I think this is mostly fine, and I commented on the issue you requested my attention, although I'm not sure it is the correct option in there

strawberry_django/filters.py Outdated Show resolved Hide resolved
strawberry_django/mutations/fields.py Outdated Show resolved Hide resolved
@@ -28,6 +30,7 @@
)

_T = TypeVar("_T")
settings = strawberry_django_settings()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking,
Maybe it's redundant here?
Anyway key_attr is propagating to DjangoMutationCUD and there it will be handled for Update and Delete right?

strawberry_django/filters.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Dec 26, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (f4f77a3) 87.73% compared to head (f1575b6) 87.71%.
Report is 1 commits behind head on main.

Files Patch % Lines
strawberry_django/mutations/fields.py 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #446      +/-   ##
==========================================
- Coverage   87.73%   87.71%   -0.02%     
==========================================
  Files          37       37              
  Lines        3130     3143      +13     
==========================================
+ Hits         2746     2757      +11     
- Misses        384      386       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

noamsto and others added 4 commits December 31, 2023 15:24
Signed-off-by: Noam Stolero <nstolero@barracuda.com>
Signed-off-by: Noam Stolero <nstolero@barracuda.com>
Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! :)

I think the only thing missing is is the DjangomodelFilterInput

I was thinking about it and probably what we want to do it to create it dynamically if DEFAULT_PK_FIELD_NAME != "pk" like I suggested at the point where it is being used (it is only used in one place) and save it in a global variable for further usage.

What do you think?

@noamsto
Copy link
Contributor Author

noamsto commented Jan 7, 2024

Looking good! :)

I think the only thing missing is is the DjangomodelFilterInput

I was thinking about it and probably what we want to do it to create it dynamically if DEFAULT_PK_FIELD_NAME != "pk" like I suggested at the point where it is being used (it is only used in one place) and save it in a global variable for further usage.

What do you think?

I don't mind doing that, though the usage is in a different file; defined in: strawberry_django/filters.py, used in: strawberry_django/fields/types.py it's a bit confusing no?

It is also used in test_types.py wouldn't it break the test?

@noamsto
Copy link
Contributor Author

noamsto commented Jan 8, 2024

@bellini666 ,
I see there are plenty of places which are declaring pk
I feel like there maybe much more work here than I initially thought.

I need to revisit all the occurrences and see what are necessary changes:
Files with 'pk':

  • permissions.py
  • /mutations/resolvers.py
  • realy.py
  • /mutations/types.py
  • optimizer.py

If you think there are places which I mentioned that irrelevant please tell.

I added Mutation test and might add others as well as I progress. It might take me a while.

@patrick91 patrick91 marked this pull request as ready for review May 22, 2024 22:42
@patrick91 patrick91 requested a review from bellini666 May 22, 2024 22:42
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @noamsto - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 5 issues found
  • 🟢 Complexity: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

strawberry_django/filters.py Show resolved Hide resolved
strawberry_django/filters.py Show resolved Hide resolved
strawberry_django/filters.py Show resolved Hide resolved
strawberry_django/filters.py Show resolved Hide resolved
strawberry_django/mutations/fields.py Show resolved Hide resolved
tests/filters/test_filters.py Show resolved Hide resolved
tests/filters/test_filters.py Show resolved Hide resolved
tests/filters/test_filters.py Show resolved Hide resolved
tests/test_queries.py Show resolved Hide resolved
tests/test_queries.py Show resolved Hide resolved
@bellini666 bellini666 merged commit 971231b into strawberry-graphql:main May 26, 2024
50 checks passed
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

Successfully merging this pull request may close these issues.

Possibility to rename pk to id or something else
4 participants