Coder Social home page Coder Social logo

Comments (5)

bellini666 avatar bellini666 commented on July 30, 2024

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.

moritz89 avatar moritz89 commented on July 30, 2024

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.

bellini666 avatar bellini666 commented on July 30, 2024

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

moritz89 avatar moritz89 commented on July 30, 2024

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.

moritz89 avatar moritz89 commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.