Comments (5)
Hey @moritz89 ,
Yeah, more documentation is never bad :)
Probably add that under a new section called "Examples" or "Recipes"?
Btw, regarding the example itself, I think it is even easier if you register your type in the field_type_map, then you can just let auto
do what you expect. e.g.
from strawberry_django.fields.types import field_type_map
@strawberry.type(name="Money")
class MoneyType:
@strawberry.field
def amount(self, root: Money) -> Decimal:
return root.amount
@strawberry.field
def currency_code(self, root: Money) -> CurrencyCode:
return root.currency
field_type_map[MoneyField] = MoneyType
Not sure how that type works (never used if before), but if what the field returns contains everything that is needed, this should work.
Just a suggestion :)
from strawberry-django.
That looks neat with the field_type_map
. However, where would you insert the only
keyword? Guess that hidden requirement breaks it when using the optimized queries?
from strawberry-django.
@moritz89 that would probably require an adjustment...
Wondering if we should have an integration for that lib? We do have optional integrations for some, such as django-guardian
, django-choices-field
, etc
from strawberry-django.
AFAIK, django-money is the most popular library to save money fields, so it would be useful for a 'broad' user base. There are a few unknowns so I'd first just add the docs and have a look at the integration support when time permits / more interest arises.
from strawberry-django.
I've had to change the class to the following to work with mutation outputs by adding a custom constructor and field methods. Is this a good approach or is there a better one? Without the constructor it only worked with GQL queries (see above).
# utils.py ---------------------------------------------------------
from moneyed import CURRENCIES_BY_ISO, Money
CurrencyCode = strawberry.enum(
Enum("CurrencyCode", {i.code: i.code for i in CURRENCIES_BY_ISO.values()})
)
@strawberry.type(name="Money")
class MoneyType:
def __init__(self, money: Money):
self.money = money
@strawberry.field
def amount(self) -> Decimal:
return self.money.amount
@strawberry.field
def currency_code(self) -> CurrencyCode:
return self.money.currency.code
# mutations.py ---------------------------------------------------------
from moneyed import Money
from utils import MoneyType
@strawberry.type
class OpenSubscriptionSession:
secure_data: str
secure_key: str
total_price: MoneyType
@strawberry.type
class Mutation:
@input_mutation
def open_subscription_session(self, info: Info, plan: str) -> OpenSubscriptionSession:
...
total_price: Money = prices.total_price()
session = OpenSubscriptionSession(
secure_data=json.dumps(store_data),
secure_key="",
total_price=MoneyType(total_price),
)
return session
from strawberry-django.
Related Issues (20)
- Ability to use custom StrawberryDjangoField class for relay connections and nodes HOT 1
- Slow startup when defining many filters HOT 5
- N+1 in some resolvers when enabling `only` extension HOT 2
- The right way to protect and filter information HOT 5
- Thanks! HOT 1
- The get_queryset method is called twice when using relay connections HOT 1
- Custom ordering methods doesn't work when value are passed by variables HOT 1
- Bug: "AttributeError 'list' object has no attribute '_result_cache'" when using pagination and optimizer
- Lazy loaded connections don't show filters or ordering
- `DjangoOptimizerExtension` can't hand `order_with_respect_to` tuples with using `OrderedModel`
- can't use `strawberry.Parent` with `root` as it's name 😊
- Global ID mapping does not work with manual id assignment on type in relay django HOT 3
- Raising django.core.exceptions.ValidationError in strawberry_django.mutations.fields.DjangoUpdateMutation/DjangoCreateMutation/DjangoDeleteMutation.resolver fails HOT 5
- `mutations.delete` and `mutations.update` do not support both `filters` _and_ `handle_django_errors` HOT 2
- When the optional file field is used, an error will be reported when there are empty files in the query set HOT 1
- Query Optimizer Annotate not working when used in nested query HOT 11
- Broken interfaces with optimiser extension HOT 4
- feat: MPTT relations compatibility
- LSP: warn on `fields="__all__"` and add code action to expose all fields
- prefetch_related and filtering in custom resolver HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strawberry-django.