Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add GraphQL API for products
Marcin Gębalaa24f4a7a2785
diff --git a/requirements.in b/requirements.inindex e2e3a2edc4..5d1966140a 100644--- a/requirements.in+++ b/requirements.in@@ -24,6 +24,7 @@ elasticsearch>=2.0.0,<3.0.0fake-factory>=0.3.2google-measurement-protocol>=0.1.2,<0.2a0google-i18n-address>=2.0.1+graphene-django==1.2.0jsonfield>=1.0.3Markdown>=2.4maxminddb>=1.2.0,<1.3diff --git a/requirements.txt b/requirements.txtindex 18cb62f397..a2d9c56c6f 100644--- a/requirements.txt+++ b/requirements.txt@@ -34,10 +34,18 @@ django-webpack-loader==0.4.1django==1.10.3djangorestframework==3.5.2elasticsearch==2.4.0+enum34==1.1.6 # via cryptographyfake-factory==0.7.2google-i18n-address==2.0.1+funcsigs==1.0.2 # via mockgoogle-measurement-protocol==0.1.6+graphene-django==1.2.0+graphene==1.1.2 # via graphene-django+graphql-core==1.0.1 # via graphene, graphql-relay+graphql-relay==0.4.5 # via grapheneidna==2.1 # via cryptography+ipaddress==1.0.17 # via cryptography, fake-factory, maxminddb+iso8601==0.1.11 # via graphene-djangojsonfield==1.0.3lxml==3.6.4 # via premailermarkdown==2.6.7@@ -49,6 +57,7 @@ pbr==1.10.0 # via mockpillow==3.4.2 # via django-versatileimagefieldpremailer==3.0.1 # via django-emailitprices==0.5.8+promise==1.0.0 # via graphene, graphql-core, graphql-relaypsycopg2==2.6.2purl==1.3py==1.4.31 # via pytest@@ -64,9 +73,11 @@ redis==2.10.5 # via django-redisrequests-oauthlib==0.7.0 # via django-allauthrequests==2.11.1satchless==1.1.3-six==1.10.0 # via cryptography, fake-factory, mock, purl, python-dateutil+singledispatch==3.4.0.3 # via graphene-django+six==1.10.0 # via cryptography, fake-factory, graphene, graphene-django, graphql-core, graphql-relay, mock, purl, python-dateutil, singledispatchstripe==1.41.1 # via django-paymentssuds-jurko==0.6 # via django-payments+typing==3.5.2.2 # via promiseunidecode==0.4.19urllib3==1.19.1 # via elasticsearchuwsgi==2.0.14diff --git a/saleor/graphql/__init__.py b/saleor/graphql/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/graphql/product_schema.py b/saleor/graphql/product_schema.pynew file mode 100644index 0000000000..9018ac65b0--- /dev/null+++ b/saleor/graphql/product_schema.py@@ -0,0 +1,68 @@+import graphene++from graphene import relay+from graphene_django import DjangoObjectType++from ..product.models import Category, Product, ProductImage, ProductVariant+++class PriceType(graphene.ObjectType):+ gross = graphene.Float()+ net = graphene.Float()+ currency = graphene.String()+++class ProductImageType(DjangoObjectType):+ url = graphene.String(size=graphene.String())++ class Meta:+ model = ProductImage+ interfaces = (relay.Node, )++ def resolve_url(self, args, context, info):+ size = args.get('size')+ if size:+ return self.image.crop[size].url+ return self.image.url+++class ProductVariantType(DjangoObjectType):+ price_override = graphene.Field(PriceType)++ class Meta:+ model = ProductVariant+ interfaces = (relay.Node, )+++class ProductType(DjangoObjectType):+ pk = graphene.Int()+ url = graphene.String()+ price = graphene.Field(PriceType)+ images = relay.ConnectionField(ProductImageType)+ variants = relay.ConnectionField(ProductVariantType)++ class Meta:+ model = Product+ interfaces = (relay.Node, )++ @graphene.resolve_only_args+ def resolve_images(self):+ return self.images.all()++ @graphene.resolve_only_args+ def resolve_variants(self):+ return self.variants.all()++ @graphene.resolve_only_args+ def resolve_pk(self):+ return self.pk++ @graphene.resolve_only_args+ def resolve_url(self):+ return self.get_absolute_url()+++class CategoryType(DjangoObjectType):+ class Meta:+ model = Category+ interfaces = (relay.Node, )diff --git a/saleor/graphql/query.py b/saleor/graphql/query.pynew file mode 100644index 0000000000..57f40c8492--- /dev/null+++ b/saleor/graphql/query.py@@ -0,0 +1,38 @@+import graphene++from django.conf import settings+from graphene import relay+from graphene_django.debug import DjangoDebug++from ..product.models import Category, Product+++class ProductQuery(graphene.AbstractType):+ category = relay.Node.Field(CategoryType)+ categories = relay.ConnectionField(CategoryType)+ product = relay.Node.Field(ProductType)+ products = relay.ConnectionField(ProductType)+ variant = relay.Node.Field(ProductVariantType)++ @graphene.resolve_only_args+ def resolve_categories(self):+ return Category.objects.all()++ @graphene.resolve_only_args+ def resolve_products(self):+ return Product.objects.prefetch_related(+ 'categories', 'images', 'variants').all()+++class Query(ProductQuery, graphene.ObjectType):+ pass+++class DebugQuery(ProductQuery, graphene.ObjectType):+ debug = graphene.Field(DjangoDebug, name='__debug')+++if settings.DEBUG:+ schema = graphene.Schema(query=DebugQuery)+else:+ schema = graphene.Schema(Query)diff --git a/saleor/settings.py b/saleor/settings.pyindex 94ea5d650f..9460bbc257 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -151,6 +151,7 @@ INSTALLED_APPS = ['saleor.cart','saleor.checkout','saleor.core',+ 'saleor.graphql','saleor.order','saleor.dashboard','saleor.shipping',@@ -164,6 +165,7 @@ INSTALLED_APPS = ['django_prices','django_prices_openexchangerates','emailit',+ 'graphene_django','mptt','payments','selectable',@@ -348,3 +350,11 @@ if ES_URL:}else:SEARCH_BACKENDS = {}+++GRAPHENE = {+ 'SCHEMA': 'saleor.graphql.schema.schema',+ 'MIDDLEWARE': [+ 'graphene_django.debug.DjangoDebugMiddleware'+ ]+}diff --git a/saleor/urls.py b/saleor/urls.pyindex f4b8dac96c..ab636f98d9 100644--- a/saleor/urls.py+++ b/saleor/urls.py@@ -4,6 +4,7 @@ from django.conf.urls.static import staticfrom django.contrib import adminfrom django.contrib.sitemaps.views import sitemapfrom django.contrib.staticfiles.views import serve+from graphene_django.views import GraphQLViewfrom .cart.urls import urlpatterns as cart_urlsfrom .checkout.urls import urlpatterns as checkout_urls@@ -26,6 +27,7 @@ urlpatterns = [url(r'^cart/', include(cart_urls, namespace='cart')),url(r'^checkout/', include(checkout_urls, namespace='checkout')),url(r'^dashboard/', include(dashboard_urls, namespace='dashboard')),+ url(r'^graphql', GraphQLView.as_view(graphiql=True)),url(r'^order/', include(order_urls, namespace='order')),url(r'^products/', include(product_urls, namespace='product')),url(r'^profile/', include(userprofile_urls, namespace='profile')),