Skip to content

Commit

Permalink
Tighten the schema types for output fields (#9605)
Browse files Browse the repository at this point in the history
Mark more fields as never returning null to make it easier to work with
automatically generated types in storefronts and apps.
  • Loading branch information
patrys committed Apr 26, 2022
1 parent ce6a8dc commit 81418cb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 70 deletions.
34 changes: 17 additions & 17 deletions saleor/graphql/account/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,23 +456,23 @@ class ChoiceValue(graphene.ObjectType):


class AddressValidationData(graphene.ObjectType):
country_code = graphene.String()
country_name = graphene.String()
address_format = graphene.String()
address_latin_format = graphene.String()
allowed_fields = NonNullList(graphene.String)
required_fields = NonNullList(graphene.String)
upper_fields = NonNullList(graphene.String)
country_area_type = graphene.String()
country_area_choices = NonNullList(ChoiceValue)
city_type = graphene.String()
city_choices = NonNullList(ChoiceValue)
city_area_type = graphene.String()
city_area_choices = NonNullList(ChoiceValue)
postal_code_type = graphene.String()
postal_code_matchers = NonNullList(graphene.String)
postal_code_examples = NonNullList(graphene.String)
postal_code_prefix = graphene.String()
country_code = graphene.String(required=True)
country_name = graphene.String(required=True)
address_format = graphene.String(required=True)
address_latin_format = graphene.String(required=True)
allowed_fields = NonNullList(graphene.String, required=True)
required_fields = NonNullList(graphene.String, required=True)
upper_fields = NonNullList(graphene.String, required=True)
country_area_type = graphene.String(required=True)
country_area_choices = NonNullList(ChoiceValue, required=True)
city_type = graphene.String(required=True)
city_choices = NonNullList(ChoiceValue, required=True)
city_area_type = graphene.String(required=True)
city_area_choices = NonNullList(ChoiceValue, required=True)
postal_code_type = graphene.String(required=True)
postal_code_matchers = NonNullList(graphene.String, required=True)
postal_code_examples = NonNullList(graphene.String, required=True)
postal_code_prefix = graphene.String(required=True)


class StaffNotificationRecipient(graphene.ObjectType):
Expand Down
16 changes: 12 additions & 4 deletions saleor/graphql/checkout/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class CheckoutLine(ModelObjectType):
total_price = graphene.Field(
TaxedMoney,
description="The sum of the checkout line price, taxes and discounts.",
required=True,
)
requires_shipping = graphene.Boolean(
description="Indicates whether the item need to be delivered."
description="Indicates whether the item need to be delivered.",
required=True,
)

class Meta:
Expand Down Expand Up @@ -222,12 +224,14 @@ class Checkout(ModelObjectType):
)
email = graphene.String(description="Email of a customer.", required=False)
gift_cards = NonNullList(
GiftCard, description="List of gift cards associated with this checkout."
GiftCard,
description="List of gift cards associated with this checkout.",
required=True,
)
is_shipping_required = graphene.Boolean(
description="Returns True, if checkout requires shipping.", required=True
)
quantity = graphene.Int(required=True, description="The number of items purchased.")
quantity = graphene.Int(description="The number of items purchased.", required=True)
stock_reservation_expires = graphene.DateTime(
description=(
f"{ADDED_IN_31} Date when oldest stock reservation for this checkout "
Expand All @@ -240,10 +244,12 @@ class Checkout(ModelObjectType):
"A list of checkout lines, each containing information about "
"an item in the checkout."
),
required=True,
)
shipping_price = graphene.Field(
TaxedMoney,
description="The price of the shipping, with all the taxes included.",
required=True,
)
shipping_method = graphene.Field(
ShippingMethod,
Expand All @@ -262,6 +268,7 @@ class Checkout(ModelObjectType):
subtotal_price = graphene.Field(
TaxedMoney,
description="The price of the checkout before shipping, with taxes included.",
required=True,
)
token = graphene.Field(UUID, description="The checkout's token.", required=True)
total_price = graphene.Field(
Expand All @@ -270,9 +277,10 @@ class Checkout(ModelObjectType):
"The sum of the the checkout line prices, with all the taxes,"
"shipping costs, and discounts included."
),
required=True,
)
language_code = graphene.Field(
LanguageCodeEnum, required=True, description="Checkout language code."
LanguageCodeEnum, description="Checkout language code.", required=True
)

class Meta:
Expand Down
40 changes: 26 additions & 14 deletions saleor/graphql/order/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,30 +697,34 @@ class Order(ModelObjectType):
)
available_shipping_methods = NonNullList(
ShippingMethod,
required=False,
description="Shipping methods that can be used with this order.",
required=False,
deprecation_reason="Use `shippingMethods`, this field will be removed in 4.0",
)
shipping_methods = NonNullList(
ShippingMethod, description="Shipping methods related to this order."
ShippingMethod,
description="Shipping methods related to this order.",
required=True,
)
available_collection_points = NonNullList(
Warehouse,
required=True,
description=(
f"{ADDED_IN_31} Collection points that can be used for this order. "
f"{PREVIEW_FEATURE}"
),
required=True,
)
invoices = NonNullList(
Invoice,
required=False,
description=(
"List of order invoices. Requires one of the following permissions: "
f"{OrderPermissions.MANAGE_ORDERS}, {AuthorizationFilters.OWNER}."
),
required=True,
)
number = graphene.String(
description="User-friendly number of an order.", required=True
)
number = graphene.String(description="User-friendly number of an order.")
original = graphene.ID(
description="The ID of the order that was the base for this order."
)
Expand All @@ -734,7 +738,9 @@ class Order(ModelObjectType):
payment_status_display = graphene.String(
description="User-friendly payment status.", required=True
)
payments = NonNullList(Payment, description="List of payments for the order.")
payments = NonNullList(
Payment, description="List of payments for the order.", required=True
)
total = graphene.Field(
TaxedMoney, description="Total amount of the order.", required=True
)
Expand All @@ -760,18 +766,22 @@ class Order(ModelObjectType):
deprecation_reason=(f"{DEPRECATED_IN_3X_FIELD} Use `id` instead."),
)
voucher = graphene.Field(Voucher)
gift_cards = NonNullList(GiftCard, description="List of user gift cards.")
gift_cards = NonNullList(
GiftCard, description="List of user gift cards.", required=True
)
display_gross_prices = graphene.Boolean(required=True)
customerNote = graphene.Boolean(required=True)
customer_note = graphene.String(required=True)
weight = graphene.Field(Weight)
weight = graphene.Field(Weight, required=True)
redirect_url = graphene.String()
subtotal = graphene.Field(
TaxedMoney,
description="The sum of line prices not including shipping.",
required=True,
)
status_display = graphene.String(description="User-friendly order status.")
status_display = graphene.String(
description="User-friendly order status.", required=True
)
can_finalize = graphene.Boolean(
description=(
"Informs whether a draft order can be finalized"
Expand All @@ -789,19 +799,20 @@ class Order(ModelObjectType):
NonNullList(OrderEvent),
description="List of events associated with the order.",
permissions=[OrderPermissions.MANAGE_ORDERS],
required=True,
)
total_balance = graphene.Field(
Money,
description="The difference between the paid and the order total amount.",
required=True,
)
user_email = graphene.String(
required=False,
description=(
"Email address of the customer. Requires the following permissions to "
f"access the full data: {OrderPermissions.MANAGE_ORDERS}, "
f"{AuthorizationFilters.OWNER}"
),
required=False,
)
is_shipping_required = graphene.Boolean(
description="Returns True, if order requires shipping.", required=True
Expand Down Expand Up @@ -847,7 +858,7 @@ class Order(ModelObjectType):
discounts = NonNullList(
"saleor.graphql.discount.types.OrderDiscount",
description="List of all discounts assigned to the order.",
required=False,
required=True,
)
errors = NonNullList(
OrderError,
Expand Down Expand Up @@ -1278,17 +1289,18 @@ def wrap_voucher_with_channel_context(data):
return Promise.all([voucher, channel]).then(wrap_voucher_with_channel_context)

@staticmethod
def resolve_language_code_enum(root, _info, **_kwargs):
def resolve_language_code_enum(root: models.Order, _info, **_kwargs):
return LanguageCodeEnum[str_to_enum(root.language_code)]

@staticmethod
def resolve_original(root, info, **_kwargs):
def resolve_original(root: models.Order, _info, **_kwargs):
if not root.original_id:
return None
return graphene.Node.to_global_id("Order", root.original_id)

@staticmethod
@traced_resolver
def resolve_errors(root, info, **_kwargs):
def resolve_errors(root: models.Order, info, **_kwargs):
if root.status == OrderStatus.DRAFT:
country = get_order_country(root)
try:
Expand Down

0 comments on commit 81418cb

Please sign in to comment.