Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add GraphQL API for products

Marcin Gębalaa24f4a7a2785
diff --git a/requirements.in b/requirements.in
index e2e3a2edc4..5d1966140a 100644
--- a/requirements.in
+++ b/requirements.in
@@ -24,6 +24,7 @@ elasticsearch>=2.0.0,<3.0.0
fake-factory>=0.3.2
google-measurement-protocol>=0.1.2,<0.2a0
google-i18n-address>=2.0.1
+graphene-django==1.2.0
jsonfield>=1.0.3
Markdown>=2.4
maxminddb>=1.2.0,<1.3
diff --git a/requirements.txt b/requirements.txt
index 18cb62f397..a2d9c56c6f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -34,10 +34,18 @@ django-webpack-loader==0.4.1
django==1.10.3
djangorestframework==3.5.2
elasticsearch==2.4.0
+enum34==1.1.6 # via cryptography
fake-factory==0.7.2
google-i18n-address==2.0.1
+funcsigs==1.0.2 # via mock
google-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 graphene
idna==2.1 # via cryptography
+ipaddress==1.0.17 # via cryptography, fake-factory, maxminddb
+iso8601==0.1.11 # via graphene-django
jsonfield==1.0.3
lxml==3.6.4 # via premailer
markdown==2.6.7
@@ -49,6 +57,7 @@ pbr==1.10.0 # via mock
pillow==3.4.2 # via django-versatileimagefield
premailer==3.0.1 # via django-emailit
prices==0.5.8
+promise==1.0.0 # via graphene, graphql-core, graphql-relay
psycopg2==2.6.2
purl==1.3
py==1.4.31 # via pytest
@@ -64,9 +73,11 @@ redis==2.10.5 # via django-redis
requests-oauthlib==0.7.0 # via django-allauth
requests==2.11.1
satchless==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, singledispatch
stripe==1.41.1 # via django-payments
suds-jurko==0.6 # via django-payments
+typing==3.5.2.2 # via promise
unidecode==0.4.19
urllib3==1.19.1 # via elasticsearch
uwsgi==2.0.14
diff --git a/saleor/graphql/__init__.py b/saleor/graphql/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/product_schema.py b/saleor/graphql/product_schema.py
new file mode 100644
index 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.py
new file mode 100644
index 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.py
index 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.py
index f4b8dac96c..ab636f98d9 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -4,6 +4,7 @@ from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
from django.contrib.staticfiles.views import serve
+from graphene_django.views import GraphQLView
from .cart.urls import urlpatterns as cart_urls
from .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')),