Commit 99fed405 authored by Andrey Zarubin's avatar Andrey Zarubin

Add core app

Add orders app. Create models and simple list query.
parent 43867e03
import graphene
from graphene_django.debug import DjangoDebug
from graphqltest.orders.schema import OrderQuery
class Query(graphene.ObjectType):
class Query(graphene.ObjectType, OrderQuery):
debug = graphene.Field(DjangoDebug, name='__debug')
......
......@@ -55,6 +55,8 @@ THIRD_PARTY_APPS = [
LOCAL_APPS = [
# custom users app
'graphqltest.users.apps.UsersConfig',
'graphqltest.core.apps.CoreConfig',
'graphqltest.orders.apps.OrdersConfig',
'graphene_django'
# Your stuff: custom apps go here
]
......
No preview for this file type
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'graphqltest.core'
from django.db import models
# Create your models here.
from collections import OrderedDict
import six
from graphene import InputObjectType, InputField, ID
from graphene import Node
from graphene.types.dynamic import Dynamic
from graphene.types.field import Field
from graphene.types.inputobjecttype import InputObjectTypeMeta
from graphene.types.options import Options
from graphene.types.utils import merge, yank_fields_from_attrs, get_base_fields
from graphene.utils.is_base_type import is_base_type
from graphene.utils.trim_docstring import trim_docstring
from graphene_django.converter import convert_django_field_with_choices
from graphene_django.registry import get_global_registry
from graphene_django.utils import get_model_fields
def construct_input_fields(options):
_model_fields = get_model_fields(options.model)
only_fields = options.only_fields
exclude_fields = options.exclude_fields
all_not_required = options.all_not_required
fields = OrderedDict()
for name, field in _model_fields:
if all_not_required:
field.null = True
is_not_in_only = only_fields and name not in options.only_fields
is_already_created = name in options.fields
is_excluded = name in exclude_fields or is_already_created
# https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_query_name
is_no_backref = str(name).endswith('+')
if is_not_in_only or is_excluded or is_no_backref:
# We skip this field if we specify only_fields and is not
# in there. Or when we exclude this field in exclude_fields.
# Or when there is no back reference.
continue
choices = getattr(field, 'choices', None)
# Reuse Enum type of the object if already exists
object_type = options.registry.get_type_for_model(field.model)
if choices and object_type and name in object_type._meta.fields and \
not isinstance(object_type._meta.fields.get(name), Dynamic):
existing_field = object_type._meta.fields.get(name)
# Fetch real type from NonNull structure if the arguments are
# all non required
if all_not_required:
existing_field = Field(existing_field._type.of_type,
required=False)
fields[name] = existing_field
continue
converted = convert_django_field_with_choices(
field,
options.registry)
if isinstance(converted, Dynamic):
converted = ID(required=not field.null)
fields[name + '_id'] = converted
else:
fields[name] = converted
return fields
class DjangoInputObjectTypeMeta(InputObjectTypeMeta):
@staticmethod
def __new__(cls, name, bases, attrs):
if not is_base_type(bases, DjangoInputObjectTypeMeta):
return type.__new__(cls, name, bases, attrs)
options = Options(
attrs.pop('Meta', None),
name=name,
model=None,
all_not_required=False,
only_fields=(),
exclude_fields=(),
fields=(),
description=trim_docstring(attrs.get('__doc__')),
local_fields=None,
)
options.base_fields = get_base_fields(bases, _as=InputField)
if not options.local_fields:
options.local_fields = yank_fields_from_attrs(attrs,
_as=InputField)
options.registry = get_global_registry()
options.fields = merge(
options.base_fields,
options.local_fields,
)
options.django_fields = yank_fields_from_attrs(
construct_input_fields(options),
_as=InputField,
)
options.fields = merge(
options.base_fields,
options.local_fields,
options.django_fields
)
cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))
return cls
class DjangoInputObjectType(six.with_metaclass(DjangoInputObjectTypeMeta,
InputObjectType)):
pass
class DjangoNode(Node):
"""
This class removes base64 unique ids and operates with plain Integer IDs
"""
@classmethod
def get_node_from_global_id(cls, global_id, context, info, only_type=None):
node = super().get_node_from_global_id(global_id, context, info,
only_type)
if node:
return node
get_node = getattr(only_type, 'get_node', None)
if get_node:
return get_node(global_id, context, info)
@classmethod
def to_global_id(cls, type, id):
return id
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
# Create your views here.
from django.contrib import admin
# Register your models here.
from .models import Order
admin.site.register(Order)
......@@ -2,4 +2,4 @@ from django.apps import AppConfig
class OrdersConfig(AppConfig):
name = 'orders'
name = 'graphqltest.orders'
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-07-12 08:17
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Order',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('product_name', models.CharField(max_length=255)),
('price', models.DecimalField(decimal_places=2, max_digits=5)),
],
),
]
from django.db import models
# Create your models here.
class Order(models.Model):
product_name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5, decimal_places=2)
from graphene.types.abstracttype import AbstractType
from graphene_django.filter.fields import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
from graphqltest.core.schema import DjangoNode
from .models import Order
class OrderNode(DjangoObjectType):
class Meta:
model = Order
interfaces = (DjangoNode,)
class OrderMutation(AbstractType):
pass
class OrderQuery(AbstractType):
orders = DjangoFilterConnectionField(OrderNode)
......@@ -50,3 +50,4 @@ redis>=2.10.5
# Your custom requirements go here
graphene-django>=1.0
django-filter==1.0.4
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment