Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add queries for warehouse in API
Mira Szewczykd782d95c1791
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex 195645e88b..9ad319ba74 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -14,6 +14,7 @@ from .product.schema import ProductMutations, ProductQueriesfrom .shipping.schema import ShippingMutations, ShippingQueriesfrom .shop.schema import ShopMutations, ShopQueriesfrom .translations.schema import TranslationQueries+from .warehouse.schema import WarehouseMutations, WarehouseQueriesfrom .webhook.schema import WebhookMutations, WebhookQueriesfrom .wishlist.schema import WishlistMutations@@ -33,6 +34,7 @@ class Query(ShippingQueries,ShopQueries,TranslationQueries,+ WarehouseQueries,WebhookQueries,):pass@@ -52,6 +54,7 @@ class Mutation(ProductMutations,ShippingMutations,ShopMutations,+ WarehouseMutations,WebhookMutations,WishlistMutations,):diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 7a194d13ab..fac2ae67d9 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2078,6 +2078,8 @@ type Mutation {webhookCreate(input: WebhookCreateInput!): WebhookCreatewebhookDelete(id: ID!): WebhookDeletewebhookUpdate(id: ID!, input: WebhookUpdateInput!): WebhookUpdate+ createWarehouse(input: WarehouseCreateInput!): WarehouseCreate+ updateWarehouse(id: ID!, input: WarehouseUpdateInput): WarehouseUpdateauthorizationKeyAdd(input: AuthorizationKeyInput!, keyType: AuthorizationKeyType!): AuthorizationKeyAddauthorizationKeyDelete(keyType: AuthorizationKeyType!): AuthorizationKeyDeletestaffNotificationRecipientCreate(input: StaffNotificationRecipientInput!): StaffNotificationRecipientCreate@@ -3485,6 +3487,8 @@ type Query {webhooks(sortBy: WebhookSortingInput, filter: WebhookFilterInput, before: String, after: String, first: Int, last: Int): WebhookCountableConnectionwebhookEvents: [WebhookEvent]webhookSamplePayload(eventType: WebhookEventTypeEnum!): JSONString+ warehouse(id: ID!): Warehouse+ warehouses(before: String, after: String, first: Int, last: Int): WarehouseCountableConnectiontranslations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnectiontranslation(id: ID!, kind: TranslatableKinds!): TranslatableItemshop: Shop@@ -4523,6 +4527,64 @@ type VoucherUpdate {voucher: Voucher}+type Warehouse implements Node {+ uuid: UUID!+ name: String!+ companyName: String!+ shippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection!+ address: Address!+ email: String!+ id: ID!+}++input WarehouseAddressInput {+ streetAddress1: String!+ streetAddress2: String+ city: String!+ cityArea: String+ postalCode: String+ country: CountryCode!+ countryArea: String+ phone: String+}++type WarehouseCountableConnection {+ pageInfo: PageInfo!+ edges: [WarehouseCountableEdge!]!+ totalCount: Int+}++type WarehouseCountableEdge {+ node: Warehouse!+ cursor: String!+}++type WarehouseCreate {+ errors: [Error!]+ warehouse: Warehouse+}++input WarehouseCreateInput {+ name: String!+ companyName: String+ shippingZonesInput: [ID]+ email: String+ address: WarehouseAddressInput!+}++type WarehouseUpdate {+ errors: [Error!]+ warehouse: Warehouse+}++input WarehouseUpdateInput {+ name: String!+ companyName: String+ shippingZonesInput: [ID]+ email: String+ address: WarehouseAddressInput+}+type Webhook implements Node {name: StringserviceAccount: ServiceAccount!diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.pynew file mode 100644index 0000000000..fa3feca33e--- /dev/null+++ b/saleor/graphql/warehouse/mutations.py@@ -0,0 +1,72 @@+import graphene++from saleor.account.models import Address+from saleor.graphql.core.mutations import ModelMutation+from saleor.graphql.warehouse.types import WarehouseCreateInput, WarehouseUpdateInput+from saleor.warehouse import models++ADDRESS_FIELDS = [+ "street_address_1",+ "street_address_2",+ "city",+ "city_area",+ "postal_code",+ "country",+ "country_area",+ "phone",+]+++class WarehouseCreate(ModelMutation):+ class Arguments:+ input = WarehouseCreateInput(+ required=True, description="Fields required to create warehouse."+ )++ class Meta:+ description = "Creates new warehouse."+ model = models.Warehouse+ permissions = ("warehouse.manage_warehouses",)++ @classmethod+ def create_address(cls, clean_data):+ address_data = {}+ for field in ADDRESS_FIELDS:+ field_value = clean_data.get(field)+ if field_value is not None:+ address_data[field] = field_value+ address = Address.objects.create(**address_data)+ return address++ @classmethod+ def clean_input(cls, info, instance, data, input_cls=None):+ cleaned_data = super().clean_input(info, instance, data, input_cls=input_cls)+ cleaned_data["address"] = cls.create_address(cleaned_data)+ return cleaned_data+++class WarehouseUpdate(ModelMutation):+ class Meta:+ model = models.Warehouse+ permissions = ("warehouse.manage_warehouses",)+ description = "Update given warehouse."++ class Arguments:+ id = graphene.ID(description="ID of a warehouse to update.", required=True)+ input = WarehouseUpdateInput()++ @classmethod+ def update_address(cls, instance, cleaned_data):+ address = instance.address+ for field in ADDRESS_FIELDS:+ field_value = cleaned_data.get(field)+ if field_value is not None:+ setattr(address, field, field_value)+ address.save()+ return address++ @classmethod+ def clean_input(cls, info, instance, data, input_cls=None):+ cleaned_data = super().clean_input(info, instance, data, input_cls=input_cls)+ cleaned_data["address"] = cls.update_address(instance, cleaned_data)+ return cleaned_datadiff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.pynew file mode 100644index 0000000000..25758dcbf5--- /dev/null+++ b/saleor/graphql/warehouse/schema.py@@ -0,0 +1,42 @@+import graphene+import graphene_django_optimizer as gql_optimizer+from graphql_jwt.decorators import login_required++from saleor.graphql.core.fields import PrefetchingConnectionField+from saleor.graphql.decorators import permission_required+from saleor.graphql.warehouse.mutations import WarehouseCreate, WarehouseUpdate+from saleor.graphql.warehouse.types import Warehouse+from saleor.warehouse import models+++class WarehouseQueries(graphene.ObjectType):+ warehouse = graphene.Field(+ Warehouse,+ description="Look up an order by ID.",+ id=graphene.Argument(+ graphene.ID, description="ID of an warehouse", required=True+ ),+ )+ warehouses = PrefetchingConnectionField(+ Warehouse, description="List of warehouses."+ )++ @login_required+ @permission_required("warehouse.manage_warehouses")+ def resolve_warehouse(self, _info, **data):+ warehouse_pk = data.get("id")+ warehouse = graphene.Node.get_node_from_global_id(+ _info, warehouse_pk, Warehouse+ )+ return warehouse++ @login_required+ @permission_required("warehouse.manage_warehouses")+ def resolve_warehouses(self, _info, **kwargs):+ qs = models.Warehouse.objects.all()+ return gql_optimizer.query(qs, _info)+++class WarehouseMutations(graphene.ObjectType):+ create_warehouse = WarehouseCreate.Field()+ update_warehouse = WarehouseUpdate.Field()diff --git a/saleor/graphql/warehouse/types.py b/saleor/graphql/warehouse/types.pynew file mode 100644index 0000000000..badf33b16f--- /dev/null+++ b/saleor/graphql/warehouse/types.py@@ -0,0 +1,44 @@+import graphene++from saleor.graphql.account.enums import CountryCodeEnum+from saleor.graphql.core.connection import CountableDjangoObjectType+from saleor.warehouse import models+++class WarehouseAddressInput(graphene.InputObjectType):+ street_address_1 = graphene.String(description="Address.", required=True)+ street_address_2 = graphene.String(description="Address.")+ city = graphene.String(description="City.", required=True)+ city_area = graphene.String(description="District.")+ postal_code = graphene.String(description="Postal code.")+ country = CountryCodeEnum(description="Country.", required=True)+ country_area = graphene.String(description="State or province.")+ phone = graphene.String(description="Phone number.")+++class WarehouseInput(graphene.InputObjectType):+ name = graphene.String(description="Warehouse name.", required=True)+ company_name = graphene.String(description="Company name.")+ shipping_zones_input = graphene.List(+ graphene.ID, description="Shipping zones supported by the warehouse."+ )+ email = graphene.String(description="The email address of the warehouse.")+++class WarehouseCreateInput(WarehouseInput):+ address = WarehouseAddressInput(+ description="Address of the warehouse.", required=True+ )+++class WarehouseUpdateInput(WarehouseInput):+ address = WarehouseAddressInput(+ description="Address of the warehouse.", required=False+ )+++class Warehouse(CountableDjangoObjectType):+ class Meta:+ description = "Represents warehouse."+ model = models.Warehouse+ interfaces = [graphene.relay.Node]