graphql-python / graphql-python/graphene
Subclass built-in scalars without creating new GraphQL type
- Vorherrschende Sprache
- Python
- Sterne
- 8.2k
- Forks
- 818
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
**Is your feature request related to a problem? Please describe.**
I'd like to be able to customise serialization logic for some scalars, e.g. a `NormalizedDecimal` that inherits from `Decimal` but calls `.normalize()` on the value during serialization. Unfortunately, this results in the GraphQL schema containing a new `NormalizedDecimal` scalar that clients don't know how to deserialize.
**Describe the solution you'd like**
I'd like the type to still be that of the superclass in the schema, e.g. `Decimal` in the above example.
**Describe alternatives you've considered**
I've tried overriding `Scalar.get_type()` to return the type of the superclass, but then the subclass doesn't function as intended, e.g. the custom serialization logic doesn't get invoked. I've also tried setting `Meta.name` to that of the superclass, but that also results in the custom serialization logic not being invoked.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Beginne damit, das Verhalten von Scalar.get_type() und den Einfluss von Meta.name auf die Schemagenerierung nachzuverfolgen, und verwende dabei das Beispiel NormalizedDecimal aus dem Issue. Die Aufgabe ist abgeschlossen, wenn eine Scalar-Unterklasse ihre benutzerdefinierte Serialisierungslogik beibehalten kann, während das Schema den Scalar-Typ der Oberklasse offenlegt; füge gezielte Tests für beide Verhaltensweisen hinzu.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- graphql, python
- Bereich
- api, backend-api-design
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100