Skip to content

Commit

Permalink
Port a better schema printer from GraphQL Core 3.x (#9389)
Browse files Browse the repository at this point in the history
This exports more information including descriptions and makes the
schema file useful for client implementation and for linting.
  • Loading branch information
patrys committed Mar 22, 2022
1 parent 76cf81e commit bd0f5b4
Show file tree
Hide file tree
Showing 21 changed files with 14,466 additions and 6,311 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
hooks:
- id: trailing-whitespace
exclude: ^templates/templated_email/compiled/
exclude_types: [svg]
exclude_types: [svg,graphql]
- id: end-of-file-fixer
exclude: ^templates/templated_email/compiled/
exclude_types: [svg]
Expand Down
15 changes: 0 additions & 15 deletions saleor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
from graphql.utils import schema_printer

from .celeryconf import app as celery_app

__all__ = ["celery_app"]
__version__ = "dev"


def patched_print_object(type):
interfaces = type.interfaces
implemented_interfaces = (
" implements {}".format(" & ".join(i.name for i in interfaces))
if interfaces
else ""
)

return ("type {}{} {{\n" "{}\n" "}}").format(
type.name, implemented_interfaces, schema_printer._print_fields(type)
)
3 changes: 2 additions & 1 deletion saleor/graphql/core/federation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from graphql import GraphQLArgument, GraphQLError, GraphQLField, GraphQLList

from ...channel import ChannelContext
from ...schema_printer import print_schema
from .entities import federated_entities


Expand Down Expand Up @@ -134,7 +135,7 @@ def resolve_entities(_, info, *, representations):

def create_service_sdl_resolver(schema):
# Render schema to string
federated_schema_sdl = str(schema)
federated_schema_sdl = print_schema(schema)

# Remove "schema { ... }"
schema_start = federated_schema_sdl.find("schema {")
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/core/types/sort_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ChannelSortInputObjectType(SortInputObjectType):
channel = graphene.Argument(
graphene.String,
description=(
"Specifies the channel in which to sort the data. "
"Specifies the channel in which to sort the data."
f"{DEPRECATED_IN_3X_INPUT} Use root-level channel argument instead."
),
)
Expand Down
4 changes: 2 additions & 2 deletions saleor/graphql/discount/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def remove_catalogues_from_node(cls, node, input):

class VoucherInput(graphene.InputObjectType):
type = VoucherTypeEnum(
description=("Voucher type: PRODUCT, CATEGORY SHIPPING or ENTIRE_ORDER.")
description="Voucher type: PRODUCT, CATEGORY SHIPPING or ENTIRE_ORDER."
)
name = graphene.String(description="Voucher name.")
code = graphene.String(description="Code to use the voucher.")
Expand Down Expand Up @@ -290,7 +290,7 @@ class Arguments:
id = graphene.ID(required=True, description="ID of a voucher.")
input = CatalogueInput(
required=True,
description=("Fields required to modify catalogue IDs of voucher."),
description="Fields required to modify catalogue IDs of voucher.",
)

class Meta:
Expand Down
3 changes: 2 additions & 1 deletion saleor/graphql/management/commands/get_graphql_schema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.core.management.base import BaseCommand

from ...api import schema
from ...schema_printer import print_schema


class Command(BaseCommand):
help = "Writes SDL for GraphQL API schema to stdout"

def handle(self, *args, **options):
self.stdout.write(str(schema))
self.stdout.write(print_schema(schema))
2 changes: 1 addition & 1 deletion saleor/graphql/meta/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class Arguments:
)
input = graphene.List(
graphene.NonNull(MetadataInput),
description=("Fields required to update the object's metadata."),
description="Fields required to update the object's metadata.",
required=True,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExternalNotificationTriggerInput(graphene.InputObjectType):
class ExternalNotificationTrigger(BaseMutation):
class Arguments:
input = ExternalNotificationTriggerInput(
required=True, description="Input for External Notification Trigger. "
required=True, description="Input for External Notification Trigger."
)
plugin_id = graphene.String(description="The ID of notification plugin.")
channel = graphene.String(
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/order/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class Fulfillment(ModelObjectType):
warehouse = graphene.Field(
Warehouse,
required=False,
description=("Warehouse from fulfillment was fulfilled."),
description="Warehouse from fulfillment was fulfilled.",
)

class Meta:
Expand Down
4 changes: 2 additions & 2 deletions saleor/graphql/payment/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class CreditCard(graphene.ObjectType):
description="Last 4 digits of the card number.", required=True
)
exp_month = graphene.Int(
description=("Two-digit number representing the card’s expiration month."),
description="Two-digit number representing the card’s expiration month.",
required=False,
)
exp_year = graphene.Int(
description=("Four-digit number representing the card’s expiration year."),
description="Four-digit number representing the card’s expiration year.",
required=False,
)

Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/product/bulk_mutations/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class ProductVariantBulkCreateInput(ProductVariantInput):
)
stocks = graphene.List(
graphene.NonNull(StockInput),
description=("Stocks of a product available for sale."),
description="Stocks of a product available for sale.",
required=False,
)
channel_listings = graphene.List(
Expand Down
4 changes: 2 additions & 2 deletions saleor/graphql/product/mutations/digital_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class DigitalContentDelete(BaseMutation):

class Arguments:
variant_id = graphene.ID(
description=("ID of a product variant with digital content to remove."),
description="ID of a product variant with digital content to remove.",
required=True,
)

Expand Down Expand Up @@ -161,7 +161,7 @@ class DigitalContentUpdate(BaseMutation):

class Arguments:
variant_id = graphene.ID(
description=("ID of a product variant with digital content to update."),
description="ID of a product variant with digital content to update.",
required=True,
)
input = DigitalContentInput(
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/product/mutations/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ class ProductVariantCreateInput(ProductVariantInput):
)
stocks = graphene.List(
graphene.NonNull(StockInput),
description=("Stocks of a product available for sale."),
description="Stocks of a product available for sale.",
required=False,
)

Expand Down
4 changes: 1 addition & 3 deletions saleor/graphql/product/types/digital_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class DigitalContentUrl(ModelObjectType):
created = graphene.DateTime(required=True)
download_num = graphene.Int(required=True)
url = graphene.String(description="URL for digital content.")
token = graphene.Field(
UUID, description=("UUID of digital content."), required=True
)
token = graphene.Field(UUID, description="UUID of digital content.", required=True)

class Meta:
model = models.DigitalContentUrl
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/product/types/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ class Product(ChannelContextTypeWithMetadata, ModelObjectType):
resolver=ChannelContextType.resolve_translation,
)
available_for_purchase = graphene.Date(
description="Date when product is available for purchase. "
description="Date when product is available for purchase."
)
is_available_for_purchase = graphene.Boolean(
description="Whether the product is available for purchase."
Expand Down

0 comments on commit bd0f5b4

Please sign in to comment.