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

wip: Initial work for first class pydantic usage #3268

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

thejaminator
Copy link
Contributor

@thejaminator thejaminator commented Dec 2, 2023

Initial work that allows you to use pydantic classes out of the box, without converting it into a strawberry dataclass type.

Start of #2181

What works

  • Basic usage of just using the pydantic type as an input / output

What doesn't work (Alot of things, don't use this yet)

  • Directives
  • Strawberry fields
  • Resolvers

What is tested so far

  • test_basic.py tests.

Two issues I encountered while implementing

Description

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).

)
)

return fields
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the code itself is quite simple for now!

# Or just store them in a dict rather than on the model itself.
@pytest.mark.xfail(reason="Need to fix this")
def test_both_output_and_input_type():
@first_class(name="WorkInput", is_input=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

marked failing things in test_basic.py with xfail

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One problem is when your pydantic type needs to be both a Input and Output type. Now you can't mark them with the same decorator - because that will override the existing __strawberry_definition__.

@botberry
Copy link
Member

botberry commented Dec 2, 2023

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to James Chua for the PR 👏

Get it here 👉 https://beta.strawberry.rocks/release/(next)

1 similar comment
@botberry
Copy link
Member

botberry commented Dec 2, 2023

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to James Chua for the PR 👏

Get it here 👉 https://beta.strawberry.rocks/release/(next)

Copy link

codecov bot commented Dec 2, 2023

Codecov Report

Merging #3268 (b4f838e) into main (c0d00aa) will decrease coverage by 0.20%.
The diff coverage is 87.48%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3268      +/-   ##
==========================================
- Coverage   96.60%   96.40%   -0.20%     
==========================================
  Files         482      486       +4     
  Lines       29954    30633     +679     
  Branches     3694     3783      +89     
==========================================
+ Hits        28937    29533     +596     
- Misses        833      915      +82     
- Partials      184      185       +1     

Copy link

codspeed-hq bot commented Dec 2, 2023

CodSpeed Performance Report

Merging #3268 will not alter performance

Comparing thejaminator:first-class-init (b4f838e) with main (c0d00aa)

Summary

✅ 11 untouched benchmarks

class Query:
@strawberry.field
def user(self) -> User:
return User(id=1, name="Patrick")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is how you use the api @patrick91 .

  1. decorate it
  2. Return it in your Query class

@thejaminator thejaminator changed the title Initial work for first class pydantic usage wip: Initial work for first class pydantic usage Dec 2, 2023
reason="""No way to manually handle errors
the validation goes boom in convert_argument, not in the create_user resolver"""
)
def test_mutation_with_validation_and_error_type():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

issue 2: no way to manually handle validation errors for now

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.

None yet

2 participants